kernel: bump 4.19 to 4.19.108
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-4.19 / 950-0039-Add-dwc_otg-driver.patch
1 From e30ea17381a5381b1d764da094a56d5cb0c9d636 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 arch/arm/include/asm/irqflags.h | 16 +-
727 arch/arm/kernel/fiqasm.S | 4 +
728 drivers/usb/Makefile | 1 +
729 drivers/usb/core/generic.c | 1 +
730 drivers/usb/core/hub.c | 2 +-
731 drivers/usb/core/message.c | 79 +
732 drivers/usb/core/otg_whitelist.h | 114 +-
733 drivers/usb/gadget/file_storage.c | 3676 +++++++++
734 drivers/usb/host/Kconfig | 10 +
735 drivers/usb/host/Makefile | 2 +
736 drivers/usb/host/dwc_common_port/Makefile | 58 +
737 .../usb/host/dwc_common_port/Makefile.fbsd | 17 +
738 .../usb/host/dwc_common_port/Makefile.linux | 49 +
739 drivers/usb/host/dwc_common_port/changes.txt | 174 +
740 .../usb/host/dwc_common_port/doc/doxygen.cfg | 270 +
741 drivers/usb/host/dwc_common_port/dwc_cc.c | 532 ++
742 drivers/usb/host/dwc_common_port/dwc_cc.h | 224 +
743 .../host/dwc_common_port/dwc_common_fbsd.c | 1308 +++
744 .../host/dwc_common_port/dwc_common_linux.c | 1409 ++++
745 .../host/dwc_common_port/dwc_common_nbsd.c | 1275 +++
746 drivers/usb/host/dwc_common_port/dwc_crypto.c | 308 +
747 drivers/usb/host/dwc_common_port/dwc_crypto.h | 111 +
748 drivers/usb/host/dwc_common_port/dwc_dh.c | 291 +
749 drivers/usb/host/dwc_common_port/dwc_dh.h | 106 +
750 drivers/usb/host/dwc_common_port/dwc_list.h | 594 ++
751 drivers/usb/host/dwc_common_port/dwc_mem.c | 245 +
752 drivers/usb/host/dwc_common_port/dwc_modpow.c | 636 ++
753 drivers/usb/host/dwc_common_port/dwc_modpow.h | 34 +
754 .../usb/host/dwc_common_port/dwc_notifier.c | 319 +
755 .../usb/host/dwc_common_port/dwc_notifier.h | 122 +
756 drivers/usb/host/dwc_common_port/dwc_os.h | 1276 +++
757 drivers/usb/host/dwc_common_port/usb.h | 946 +++
758 drivers/usb/host/dwc_otg/Makefile | 82 +
759 drivers/usb/host/dwc_otg/doc/doxygen.cfg | 224 +
760 drivers/usb/host/dwc_otg/dummy_audio.c | 1574 ++++
761 drivers/usb/host/dwc_otg/dwc_cfi_common.h | 142 +
762 drivers/usb/host/dwc_otg/dwc_otg_adp.c | 854 ++
763 drivers/usb/host/dwc_otg/dwc_otg_adp.h | 80 +
764 drivers/usb/host/dwc_otg/dwc_otg_attr.c | 1212 +++
765 drivers/usb/host/dwc_otg/dwc_otg_attr.h | 89 +
766 drivers/usb/host/dwc_otg/dwc_otg_cfi.c | 1876 +++++
767 drivers/usb/host/dwc_otg/dwc_otg_cfi.h | 320 +
768 drivers/usb/host/dwc_otg/dwc_otg_cil.c | 7146 +++++++++++++++++
769 drivers/usb/host/dwc_otg/dwc_otg_cil.h | 1464 ++++
770 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c | 1596 ++++
771 drivers/usb/host/dwc_otg/dwc_otg_core_if.h | 705 ++
772 drivers/usb/host/dwc_otg/dwc_otg_dbg.h | 117 +
773 drivers/usb/host/dwc_otg/dwc_otg_driver.c | 1767 ++++
774 drivers/usb/host/dwc_otg/dwc_otg_driver.h | 86 +
775 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 1401 ++++
776 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 372 +
777 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S | 80 +
778 drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 4297 ++++++++++
779 drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 870 ++
780 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c | 1134 +++
781 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h | 417 +
782 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 2752 +++++++
783 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 1002 +++
784 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c | 971 +++
785 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h | 188 +
786 drivers/usb/host/dwc_otg/dwc_otg_pcd.c | 2725 +++++++
787 drivers/usb/host/dwc_otg/dwc_otg_pcd.h | 273 +
788 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h | 361 +
789 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c | 5148 ++++++++++++
790 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c | 1280 +++
791 drivers/usb/host/dwc_otg/dwc_otg_regs.h | 2550 ++++++
792 drivers/usb/host/dwc_otg/test/Makefile | 16 +
793 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm | 337 +
794 .../usb/host/dwc_otg/test/test_mod_param.pl | 133 +
795 drivers/usb/host/dwc_otg/test/test_sysfs.pl | 193 +
796 70 files changed, 60027 insertions(+), 16 deletions(-)
797 create mode 100644 drivers/usb/gadget/file_storage.c
798 create mode 100644 drivers/usb/host/dwc_common_port/Makefile
799 create mode 100644 drivers/usb/host/dwc_common_port/Makefile.fbsd
800 create mode 100644 drivers/usb/host/dwc_common_port/Makefile.linux
801 create mode 100644 drivers/usb/host/dwc_common_port/changes.txt
802 create mode 100644 drivers/usb/host/dwc_common_port/doc/doxygen.cfg
803 create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.c
804 create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.h
805 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
806 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_linux.c
807 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
808 create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.c
809 create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.h
810 create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.c
811 create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.h
812 create mode 100644 drivers/usb/host/dwc_common_port/dwc_list.h
813 create mode 100644 drivers/usb/host/dwc_common_port/dwc_mem.c
814 create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.c
815 create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.h
816 create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.c
817 create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.h
818 create mode 100644 drivers/usb/host/dwc_common_port/dwc_os.h
819 create mode 100644 drivers/usb/host/dwc_common_port/usb.h
820 create mode 100644 drivers/usb/host/dwc_otg/Makefile
821 create mode 100644 drivers/usb/host/dwc_otg/doc/doxygen.cfg
822 create mode 100644 drivers/usb/host/dwc_otg/dummy_audio.c
823 create mode 100644 drivers/usb/host/dwc_otg/dwc_cfi_common.h
824 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.c
825 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.h
826 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.c
827 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.h
828 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.c
829 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.h
830 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.c
831 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.h
832 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
833 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_core_if.h
834 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_dbg.h
835 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.c
836 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.h
837 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
838 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
839 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
840 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.c
841 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.h
842 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
843 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
844 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
845 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
846 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
847 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
848 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.c
849 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.h
850 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
851 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
852 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
853 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_regs.h
854 create mode 100644 drivers/usb/host/dwc_otg/test/Makefile
855 create mode 100644 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
856 create mode 100644 drivers/usb/host/dwc_otg/test/test_mod_param.pl
857 create mode 100644 drivers/usb/host/dwc_otg/test/test_sysfs.pl
858
859 --- a/arch/arm/include/asm/irqflags.h
860 +++ b/arch/arm/include/asm/irqflags.h
861 @@ -163,13 +163,23 @@ static inline unsigned long arch_local_s
862 }
863
864 /*
865 - * restore saved IRQ & FIQ state
866 + * restore saved IRQ state
867 */
868 #define arch_local_irq_restore arch_local_irq_restore
869 static inline void arch_local_irq_restore(unsigned long flags)
870 {
871 - asm volatile(
872 - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
873 + unsigned long temp = 0;
874 + flags &= ~(1 << 6);
875 + asm volatile (
876 + " mrs %0, cpsr"
877 + : "=r" (temp)
878 + :
879 + : "memory", "cc");
880 + /* Preserve FIQ bit */
881 + temp &= (1 << 6);
882 + flags = flags | temp;
883 + asm volatile (
884 + " msr cpsr_c, %0 @ local_irq_restore"
885 :
886 : "r" (flags)
887 : "memory", "cc");
888 --- a/arch/arm/kernel/fiqasm.S
889 +++ b/arch/arm/kernel/fiqasm.S
890 @@ -47,3 +47,7 @@ ENTRY(__get_fiq_regs)
891 mov r0, r0 @ avoid hazard prior to ARMv4
892 ret lr
893 ENDPROC(__get_fiq_regs)
894 +
895 +ENTRY(__FIQ_Branch)
896 + mov pc, r8
897 +ENDPROC(__FIQ_Branch)
898 --- a/drivers/usb/Makefile
899 +++ b/drivers/usb/Makefile
900 @@ -8,6 +8,7 @@
901 obj-$(CONFIG_USB) += core/
902 obj-$(CONFIG_USB_SUPPORT) += phy/
903
904 +obj-$(CONFIG_USB_DWCOTG) += host/
905 obj-$(CONFIG_USB_DWC3) += dwc3/
906 obj-$(CONFIG_USB_DWC2) += dwc2/
907 obj-$(CONFIG_USB_ISP1760) += isp1760/
908 --- a/drivers/usb/core/generic.c
909 +++ b/drivers/usb/core/generic.c
910 @@ -154,6 +154,7 @@ int usb_choose_configuration(struct usb_
911 dev_warn(&udev->dev,
912 "no configuration chosen from %d choice%s\n",
913 num_configs, plural(num_configs));
914 + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
915 }
916 return i;
917 }
918 --- a/drivers/usb/core/hub.c
919 +++ b/drivers/usb/core/hub.c
920 @@ -5228,7 +5228,7 @@ static void port_event(struct usb_hub *h
921 u16 status = 0, unused;
922 port_dev->over_current_count++;
923
924 - dev_dbg(&port_dev->dev, "over-current change #%u\n",
925 + dev_notice(&port_dev->dev, "over-current change #%u\n",
926 port_dev->over_current_count);
927 usb_clear_port_feature(hdev, port1,
928 USB_PORT_FEAT_C_OVER_CURRENT);
929 --- a/drivers/usb/core/message.c
930 +++ b/drivers/usb/core/message.c
931 @@ -1993,6 +1993,85 @@ free_interfaces:
932 if (cp->string == NULL &&
933 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
934 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
935 +/* Uncomment this define to enable the HS Electrical Test support */
936 +#define DWC_HS_ELECT_TST 1
937 +#ifdef DWC_HS_ELECT_TST
938 + /* Here we implement the HS Electrical Test support. The
939 + * tester uses a vendor ID of 0x1A0A to indicate we should
940 + * run a special test sequence. The product ID tells us
941 + * which sequence to run. We invoke the test sequence by
942 + * sending a non-standard SetFeature command to our root
943 + * hub port. Our dwc_otg_hcd_hub_control() routine will
944 + * recognize the command and perform the desired test
945 + * sequence.
946 + */
947 + if (dev->descriptor.idVendor == 0x1A0A) {
948 + /* HSOTG Electrical Test */
949 + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
950 +
951 + if (dev->bus && dev->bus->root_hub) {
952 + struct usb_device *hdev = dev->bus->root_hub;
953 + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
954 +
955 + switch (dev->descriptor.idProduct) {
956 + case 0x0101: /* TEST_SE0_NAK */
957 + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
958 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
959 + USB_REQ_SET_FEATURE, USB_RT_PORT,
960 + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
961 + break;
962 +
963 + case 0x0102: /* TEST_J */
964 + dev_warn(&dev->dev, "TEST_J\n");
965 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
966 + USB_REQ_SET_FEATURE, USB_RT_PORT,
967 + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
968 + break;
969 +
970 + case 0x0103: /* TEST_K */
971 + dev_warn(&dev->dev, "TEST_K\n");
972 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
973 + USB_REQ_SET_FEATURE, USB_RT_PORT,
974 + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
975 + break;
976 +
977 + case 0x0104: /* TEST_PACKET */
978 + dev_warn(&dev->dev, "TEST_PACKET\n");
979 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
980 + USB_REQ_SET_FEATURE, USB_RT_PORT,
981 + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
982 + break;
983 +
984 + case 0x0105: /* TEST_FORCE_ENABLE */
985 + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
986 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
987 + USB_REQ_SET_FEATURE, USB_RT_PORT,
988 + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
989 + break;
990 +
991 + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
992 + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
993 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
994 + USB_REQ_SET_FEATURE, USB_RT_PORT,
995 + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
996 + break;
997 +
998 + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
999 + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
1000 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1001 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1002 + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
1003 + break;
1004 +
1005 + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
1006 + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
1007 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1008 + USB_REQ_SET_FEATURE, USB_RT_PORT,
1009 + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
1010 + }
1011 + }
1012 + }
1013 +#endif /* DWC_HS_ELECT_TST */
1014
1015 /* Now that the interfaces are installed, re-enable LPM. */
1016 usb_unlocked_enable_lpm(dev);
1017 --- a/drivers/usb/core/otg_whitelist.h
1018 +++ b/drivers/usb/core/otg_whitelist.h
1019 @@ -15,33 +15,82 @@
1020 static struct usb_device_id whitelist_table[] = {
1021
1022 /* hubs are optional in OTG, but very handy ... */
1023 +#define CERT_WITHOUT_HUBS
1024 +#if defined(CERT_WITHOUT_HUBS)
1025 +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
1026 +#else
1027 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
1028 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
1029 +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
1030 +#endif
1031
1032 #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
1033 /* FIXME actually, printers are NOT supposed to use device classes;
1034 * they're supposed to use interface classes...
1035 */
1036 -{ USB_DEVICE_INFO(7, 1, 1) },
1037 -{ USB_DEVICE_INFO(7, 1, 2) },
1038 -{ USB_DEVICE_INFO(7, 1, 3) },
1039 +//{ USB_DEVICE_INFO(7, 1, 1) },
1040 +//{ USB_DEVICE_INFO(7, 1, 2) },
1041 +//{ USB_DEVICE_INFO(7, 1, 3) },
1042 #endif
1043
1044 #ifdef CONFIG_USB_NET_CDCETHER
1045 /* Linux-USB CDC Ethernet gadget */
1046 -{ USB_DEVICE(0x0525, 0xa4a1), },
1047 +//{ USB_DEVICE(0x0525, 0xa4a1), },
1048 /* Linux-USB CDC Ethernet + RNDIS gadget */
1049 -{ USB_DEVICE(0x0525, 0xa4a2), },
1050 +//{ USB_DEVICE(0x0525, 0xa4a2), },
1051 #endif
1052
1053 #if IS_ENABLED(CONFIG_USB_TEST)
1054 /* gadget zero, for testing */
1055 -{ USB_DEVICE(0x0525, 0xa4a0), },
1056 +//{ USB_DEVICE(0x0525, 0xa4a0), },
1057 #endif
1058
1059 +/* OPT Tester */
1060 +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
1061 +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
1062 +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
1063 +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
1064 +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
1065 +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
1066 +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
1067 +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
1068 +
1069 +/* Sony cameras */
1070 +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
1071 +
1072 +/* Memory Devices */
1073 +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
1074 +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
1075 +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
1076 +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
1077 +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
1078 +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
1079 +
1080 +/* HP Printers */
1081 +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
1082 +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
1083 +
1084 +/* Speakers */
1085 +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
1086 +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
1087 +
1088 { } /* Terminating entry */
1089 };
1090
1091 +static inline void report_errors(struct usb_device *dev)
1092 +{
1093 + /* OTG MESSAGE: report errors here, customize to match your product */
1094 + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
1095 + le16_to_cpu(dev->descriptor.idVendor),
1096 + le16_to_cpu(dev->descriptor.idProduct));
1097 + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
1098 + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
1099 + } else {
1100 + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
1101 + }
1102 +}
1103 +
1104 +
1105 static int is_targeted(struct usb_device *dev)
1106 {
1107 struct usb_device_id *id = whitelist_table;
1108 @@ -91,16 +140,57 @@ static int is_targeted(struct usb_device
1109 continue;
1110
1111 return 1;
1112 - }
1113 + /* NOTE: can't use usb_match_id() since interface caches
1114 + * aren't set up yet. this is cut/paste from that code.
1115 + */
1116 + for (id = whitelist_table; id->match_flags; id++) {
1117 +#ifdef DEBUG
1118 + dev_dbg(&dev->dev,
1119 + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
1120 + id->idVendor,
1121 + id->idProduct,
1122 + id->bDeviceClass,
1123 + id->bDeviceSubClass,
1124 + id->bDeviceProtocol);
1125 +#endif
1126
1127 - /* add other match criteria here ... */
1128 + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
1129 + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
1130 + continue;
1131 +
1132 + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
1133 + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
1134 + continue;
1135 +
1136 + /* No need to test id->bcdDevice_lo != 0, since 0 is never
1137 + greater than any unsigned number. */
1138 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
1139 + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
1140 + continue;
1141 +
1142 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
1143 + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
1144 + continue;
1145 +
1146 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
1147 + (id->bDeviceClass != dev->descriptor.bDeviceClass))
1148 + continue;
1149 +
1150 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
1151 + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
1152 + continue;
1153 +
1154 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
1155 + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
1156 + continue;
1157
1158 + return 1;
1159 + }
1160 + }
1161
1162 - /* OTG MESSAGE: report errors here, customize to match your product */
1163 - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
1164 - le16_to_cpu(dev->descriptor.idVendor),
1165 - le16_to_cpu(dev->descriptor.idProduct));
1166 + /* add other match criteria here ... */
1167
1168 + report_errors(dev);
1169 return 0;
1170 }
1171
1172 --- /dev/null
1173 +++ b/drivers/usb/gadget/file_storage.c
1174 @@ -0,0 +1,3676 @@
1175 +/*
1176 + * file_storage.c -- File-backed USB Storage Gadget, for USB development
1177 + *
1178 + * Copyright (C) 2003-2008 Alan Stern
1179 + * All rights reserved.
1180 + *
1181 + * Redistribution and use in source and binary forms, with or without
1182 + * modification, are permitted provided that the following conditions
1183 + * are met:
1184 + * 1. Redistributions of source code must retain the above copyright
1185 + * notice, this list of conditions, and the following disclaimer,
1186 + * without modification.
1187 + * 2. Redistributions in binary form must reproduce the above copyright
1188 + * notice, this list of conditions and the following disclaimer in the
1189 + * documentation and/or other materials provided with the distribution.
1190 + * 3. The names of the above-listed copyright holders may not be used
1191 + * to endorse or promote products derived from this software without
1192 + * specific prior written permission.
1193 + *
1194 + * ALTERNATIVELY, this software may be distributed under the terms of the
1195 + * GNU General Public License ("GPL") as published by the Free Software
1196 + * Foundation, either version 2 of that License or (at your option) any
1197 + * later version.
1198 + *
1199 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1200 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1201 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1202 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1203 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1204 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1205 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1206 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1207 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1208 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1209 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1210 + */
1211 +
1212 +
1213 +/*
1214 + * The File-backed Storage Gadget acts as a USB Mass Storage device,
1215 + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
1216 + * to providing an example of a genuinely useful gadget driver for a USB
1217 + * device, it also illustrates a technique of double-buffering for increased
1218 + * throughput. Last but not least, it gives an easy way to probe the
1219 + * behavior of the Mass Storage drivers in a USB host.
1220 + *
1221 + * Backing storage is provided by a regular file or a block device, specified
1222 + * by the "file" module parameter. Access can be limited to read-only by
1223 + * setting the optional "ro" module parameter. (For CD-ROM emulation,
1224 + * access is always read-only.) The gadget will indicate that it has
1225 + * removable media if the optional "removable" module parameter is set.
1226 + *
1227 + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
1228 + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
1229 + * by the optional "transport" module parameter. It also supports the
1230 + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
1231 + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
1232 + * the optional "protocol" module parameter. In addition, the default
1233 + * Vendor ID, Product ID, release number and serial number can be overridden.
1234 + *
1235 + * There is support for multiple logical units (LUNs), each of which has
1236 + * its own backing file. The number of LUNs can be set using the optional
1237 + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
1238 + * files are specified using comma-separated lists for "file" and "ro".
1239 + * The default number of LUNs is taken from the number of "file" elements;
1240 + * it is 1 if "file" is not given. If "removable" is not set then a backing
1241 + * file must be specified for each LUN. If it is set, then an unspecified
1242 + * or empty backing filename means the LUN's medium is not loaded. Ideally
1243 + * each LUN would be settable independently as a disk drive or a CD-ROM
1244 + * drive, but currently all LUNs have to be the same type. The CD-ROM
1245 + * emulation includes a single data track and no audio tracks; hence there
1246 + * need be only one backing file per LUN.
1247 + *
1248 + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
1249 + * needed (an interrupt-out endpoint is also needed for CBI). The memory
1250 + * requirement amounts to two 16K buffers, size configurable by a parameter.
1251 + * Support is included for both full-speed and high-speed operation.
1252 + *
1253 + * Note that the driver is slightly non-portable in that it assumes a
1254 + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
1255 + * interrupt-in endpoints. With most device controllers this isn't an
1256 + * issue, but there may be some with hardware restrictions that prevent
1257 + * a buffer from being used by more than one endpoint.
1258 + *
1259 + * Module options:
1260 + *
1261 + * file=filename[,filename...]
1262 + * Required if "removable" is not set, names of
1263 + * the files or block devices used for
1264 + * backing storage
1265 + * serial=HHHH... Required serial number (string of hex chars)
1266 + * ro=b[,b...] Default false, booleans for read-only access
1267 + * removable Default false, boolean for removable media
1268 + * luns=N Default N = number of filenames, number of
1269 + * LUNs to support
1270 + * nofua=b[,b...] Default false, booleans for ignore FUA flag
1271 + * in SCSI WRITE(10,12) commands
1272 + * stall Default determined according to the type of
1273 + * USB device controller (usually true),
1274 + * boolean to permit the driver to halt
1275 + * bulk endpoints
1276 + * cdrom Default false, boolean for whether to emulate
1277 + * a CD-ROM drive
1278 + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
1279 + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
1280 + * ATAPI, QIC, UFI, 8070, or SCSI;
1281 + * also 1 - 6)
1282 + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
1283 + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
1284 + * release=0xRRRR Override the USB release number (bcdDevice)
1285 + * buflen=N Default N=16384, buffer size used (will be
1286 + * rounded down to a multiple of
1287 + * PAGE_CACHE_SIZE)
1288 + *
1289 + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
1290 + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
1291 + * default values are used for everything else.
1292 + *
1293 + * The pathnames of the backing files and the ro settings are available in
1294 + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
1295 + * the gadget's sysfs directory. If the "removable" option is set, writing to
1296 + * these files will simulate ejecting/loading the medium (writing an empty
1297 + * line means eject) and adjusting a write-enable tab. Changes to the ro
1298 + * setting are not allowed when the medium is loaded or if CD-ROM emulation
1299 + * is being used.
1300 + *
1301 + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
1302 + * The driver's SCSI command interface was based on the "Information
1303 + * technology - Small Computer System Interface - 2" document from
1304 + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
1305 + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
1306 + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
1307 + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
1308 + * document, Revision 1.0, December 14, 1998, available at
1309 + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
1310 + */
1311 +
1312 +
1313 +/*
1314 + * Driver Design
1315 + *
1316 + * The FSG driver is fairly straightforward. There is a main kernel
1317 + * thread that handles most of the work. Interrupt routines field
1318 + * callbacks from the controller driver: bulk- and interrupt-request
1319 + * completion notifications, endpoint-0 events, and disconnect events.
1320 + * Completion events are passed to the main thread by wakeup calls. Many
1321 + * ep0 requests are handled at interrupt time, but SetInterface,
1322 + * SetConfiguration, and device reset requests are forwarded to the
1323 + * thread in the form of "exceptions" using SIGUSR1 signals (since they
1324 + * should interrupt any ongoing file I/O operations).
1325 + *
1326 + * The thread's main routine implements the standard command/data/status
1327 + * parts of a SCSI interaction. It and its subroutines are full of tests
1328 + * for pending signals/exceptions -- all this polling is necessary since
1329 + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
1330 + * indication that the driver really wants to be running in userspace.)
1331 + * An important point is that so long as the thread is alive it keeps an
1332 + * open reference to the backing file. This will prevent unmounting
1333 + * the backing file's underlying filesystem and could cause problems
1334 + * during system shutdown, for example. To prevent such problems, the
1335 + * thread catches INT, TERM, and KILL signals and converts them into
1336 + * an EXIT exception.
1337 + *
1338 + * In normal operation the main thread is started during the gadget's
1339 + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
1340 + * exit when it receives a signal, and there's no point leaving the
1341 + * gadget running when the thread is dead. So just before the thread
1342 + * exits, it deregisters the gadget driver. This makes things a little
1343 + * tricky: The driver is deregistered at two places, and the exiting
1344 + * thread can indirectly call fsg_unbind() which in turn can tell the
1345 + * thread to exit. The first problem is resolved through the use of the
1346 + * REGISTERED atomic bitflag; the driver will only be deregistered once.
1347 + * The second problem is resolved by having fsg_unbind() check
1348 + * fsg->state; it won't try to stop the thread if the state is already
1349 + * FSG_STATE_TERMINATED.
1350 + *
1351 + * To provide maximum throughput, the driver uses a circular pipeline of
1352 + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
1353 + * arbitrarily long; in practice the benefits don't justify having more
1354 + * than 2 stages (i.e., double buffering). But it helps to think of the
1355 + * pipeline as being a long one. Each buffer head contains a bulk-in and
1356 + * a bulk-out request pointer (since the buffer can be used for both
1357 + * output and input -- directions always are given from the host's
1358 + * point of view) as well as a pointer to the buffer and various state
1359 + * variables.
1360 + *
1361 + * Use of the pipeline follows a simple protocol. There is a variable
1362 + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
1363 + * At any time that buffer head may still be in use from an earlier
1364 + * request, so each buffer head has a state variable indicating whether
1365 + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
1366 + * buffer head to be EMPTY, filling the buffer either by file I/O or by
1367 + * USB I/O (during which the buffer head is BUSY), and marking the buffer
1368 + * head FULL when the I/O is complete. Then the buffer will be emptied
1369 + * (again possibly by USB I/O, during which it is marked BUSY) and
1370 + * finally marked EMPTY again (possibly by a completion routine).
1371 + *
1372 + * A module parameter tells the driver to avoid stalling the bulk
1373 + * endpoints wherever the transport specification allows. This is
1374 + * necessary for some UDCs like the SuperH, which cannot reliably clear a
1375 + * halt on a bulk endpoint. However, under certain circumstances the
1376 + * Bulk-only specification requires a stall. In such cases the driver
1377 + * will halt the endpoint and set a flag indicating that it should clear
1378 + * the halt in software during the next device reset. Hopefully this
1379 + * will permit everything to work correctly. Furthermore, although the
1380 + * specification allows the bulk-out endpoint to halt when the host sends
1381 + * too much data, implementing this would cause an unavoidable race.
1382 + * The driver will always use the "no-stall" approach for OUT transfers.
1383 + *
1384 + * One subtle point concerns sending status-stage responses for ep0
1385 + * requests. Some of these requests, such as device reset, can involve
1386 + * interrupting an ongoing file I/O operation, which might take an
1387 + * arbitrarily long time. During that delay the host might give up on
1388 + * the original ep0 request and issue a new one. When that happens the
1389 + * driver should not notify the host about completion of the original
1390 + * request, as the host will no longer be waiting for it. So the driver
1391 + * assigns to each ep0 request a unique tag, and it keeps track of the
1392 + * tag value of the request associated with a long-running exception
1393 + * (device-reset, interface-change, or configuration-change). When the
1394 + * exception handler is finished, the status-stage response is submitted
1395 + * only if the current ep0 request tag is equal to the exception request
1396 + * tag. Thus only the most recently received ep0 request will get a
1397 + * status-stage response.
1398 + *
1399 + * Warning: This driver source file is too long. It ought to be split up
1400 + * into a header file plus about 3 separate .c files, to handle the details
1401 + * of the Gadget, USB Mass Storage, and SCSI protocols.
1402 + */
1403 +
1404 +
1405 +/* #define VERBOSE_DEBUG */
1406 +/* #define DUMP_MSGS */
1407 +
1408 +
1409 +#include <linux/blkdev.h>
1410 +#include <linux/completion.h>
1411 +#include <linux/dcache.h>
1412 +#include <linux/delay.h>
1413 +#include <linux/device.h>
1414 +#include <linux/fcntl.h>
1415 +#include <linux/file.h>
1416 +#include <linux/fs.h>
1417 +#include <linux/kref.h>
1418 +#include <linux/kthread.h>
1419 +#include <linux/limits.h>
1420 +#include <linux/module.h>
1421 +#include <linux/rwsem.h>
1422 +#include <linux/slab.h>
1423 +#include <linux/spinlock.h>
1424 +#include <linux/string.h>
1425 +#include <linux/freezer.h>
1426 +#include <linux/utsname.h>
1427 +
1428 +#include <linux/usb/ch9.h>
1429 +#include <linux/usb/gadget.h>
1430 +
1431 +#include "gadget_chips.h"
1432 +
1433 +
1434 +
1435 +/*
1436 + * Kbuild is not very cooperative with respect to linking separately
1437 + * compiled library objects into one module. So for now we won't use
1438 + * separate compilation ... ensuring init/exit sections work to shrink
1439 + * the runtime footprint, and giving us at least some parts of what
1440 + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
1441 + */
1442 +#include "usbstring.c"
1443 +#include "config.c"
1444 +#include "epautoconf.c"
1445 +
1446 +/*-------------------------------------------------------------------------*/
1447 +
1448 +#define DRIVER_DESC "File-backed Storage Gadget"
1449 +#define DRIVER_NAME "g_file_storage"
1450 +#define DRIVER_VERSION "1 September 2010"
1451 +
1452 +static char fsg_string_manufacturer[64];
1453 +static const char fsg_string_product[] = DRIVER_DESC;
1454 +static const char fsg_string_config[] = "Self-powered";
1455 +static const char fsg_string_interface[] = "Mass Storage";
1456 +
1457 +
1458 +#include "storage_common.c"
1459 +
1460 +
1461 +MODULE_DESCRIPTION(DRIVER_DESC);
1462 +MODULE_AUTHOR("Alan Stern");
1463 +MODULE_LICENSE("Dual BSD/GPL");
1464 +
1465 +/*
1466 + * This driver assumes self-powered hardware and has no way for users to
1467 + * trigger remote wakeup. It uses autoconfiguration to select endpoints
1468 + * and endpoint addresses.
1469 + */
1470 +
1471 +
1472 +/*-------------------------------------------------------------------------*/
1473 +
1474 +
1475 +/* Encapsulate the module parameter settings */
1476 +
1477 +static struct {
1478 + char *file[FSG_MAX_LUNS];
1479 + char *serial;
1480 + bool ro[FSG_MAX_LUNS];
1481 + bool nofua[FSG_MAX_LUNS];
1482 + unsigned int num_filenames;
1483 + unsigned int num_ros;
1484 + unsigned int num_nofuas;
1485 + unsigned int nluns;
1486 +
1487 + bool removable;
1488 + bool can_stall;
1489 + bool cdrom;
1490 +
1491 + char *transport_parm;
1492 + char *protocol_parm;
1493 + unsigned short vendor;
1494 + unsigned short product;
1495 + unsigned short release;
1496 + unsigned int buflen;
1497 +
1498 + int transport_type;
1499 + char *transport_name;
1500 + int protocol_type;
1501 + char *protocol_name;
1502 +
1503 +} mod_data = { // Default values
1504 + .transport_parm = "BBB",
1505 + .protocol_parm = "SCSI",
1506 + .removable = 0,
1507 + .can_stall = 1,
1508 + .cdrom = 0,
1509 + .vendor = FSG_VENDOR_ID,
1510 + .product = FSG_PRODUCT_ID,
1511 + .release = 0xffff, // Use controller chip type
1512 + .buflen = 16384,
1513 + };
1514 +
1515 +
1516 +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
1517 + S_IRUGO);
1518 +MODULE_PARM_DESC(file, "names of backing files or devices");
1519 +
1520 +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
1521 +MODULE_PARM_DESC(serial, "USB serial number");
1522 +
1523 +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
1524 +MODULE_PARM_DESC(ro, "true to force read-only");
1525 +
1526 +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
1527 + S_IRUGO);
1528 +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
1529 +
1530 +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
1531 +MODULE_PARM_DESC(luns, "number of LUNs");
1532 +
1533 +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
1534 +MODULE_PARM_DESC(removable, "true to simulate removable media");
1535 +
1536 +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
1537 +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
1538 +
1539 +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
1540 +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
1541 +
1542 +/* In the non-TEST version, only the module parameters listed above
1543 + * are available. */
1544 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1545 +
1546 +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
1547 +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
1548 +
1549 +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
1550 +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
1551 + "8070, or SCSI)");
1552 +
1553 +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
1554 +MODULE_PARM_DESC(vendor, "USB Vendor ID");
1555 +
1556 +module_param_named(product, mod_data.product, ushort, S_IRUGO);
1557 +MODULE_PARM_DESC(product, "USB Product ID");
1558 +
1559 +module_param_named(release, mod_data.release, ushort, S_IRUGO);
1560 +MODULE_PARM_DESC(release, "USB release number");
1561 +
1562 +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
1563 +MODULE_PARM_DESC(buflen, "I/O buffer size");
1564 +
1565 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1566 +
1567 +
1568 +/*
1569 + * These definitions will permit the compiler to avoid generating code for
1570 + * parts of the driver that aren't used in the non-TEST version. Even gcc
1571 + * can recognize when a test of a constant expression yields a dead code
1572 + * path.
1573 + */
1574 +
1575 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1576 +
1577 +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
1578 +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
1579 +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
1580 +
1581 +#else
1582 +
1583 +#define transport_is_bbb() 1
1584 +#define transport_is_cbi() 0
1585 +#define protocol_is_scsi() 1
1586 +
1587 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1588 +
1589 +
1590 +/*-------------------------------------------------------------------------*/
1591 +
1592 +
1593 +struct fsg_dev {
1594 + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
1595 + spinlock_t lock;
1596 + struct usb_gadget *gadget;
1597 +
1598 + /* filesem protects: backing files in use */
1599 + struct rw_semaphore filesem;
1600 +
1601 + /* reference counting: wait until all LUNs are released */
1602 + struct kref ref;
1603 +
1604 + struct usb_ep *ep0; // Handy copy of gadget->ep0
1605 + struct usb_request *ep0req; // For control responses
1606 + unsigned int ep0_req_tag;
1607 + const char *ep0req_name;
1608 +
1609 + struct usb_request *intreq; // For interrupt responses
1610 + int intreq_busy;
1611 + struct fsg_buffhd *intr_buffhd;
1612 +
1613 + unsigned int bulk_out_maxpacket;
1614 + enum fsg_state state; // For exception handling
1615 + unsigned int exception_req_tag;
1616 +
1617 + u8 config, new_config;
1618 +
1619 + unsigned int running : 1;
1620 + unsigned int bulk_in_enabled : 1;
1621 + unsigned int bulk_out_enabled : 1;
1622 + unsigned int intr_in_enabled : 1;
1623 + unsigned int phase_error : 1;
1624 + unsigned int short_packet_received : 1;
1625 + unsigned int bad_lun_okay : 1;
1626 +
1627 + unsigned long atomic_bitflags;
1628 +#define REGISTERED 0
1629 +#define IGNORE_BULK_OUT 1
1630 +#define SUSPENDED 2
1631 +
1632 + struct usb_ep *bulk_in;
1633 + struct usb_ep *bulk_out;
1634 + struct usb_ep *intr_in;
1635 +
1636 + struct fsg_buffhd *next_buffhd_to_fill;
1637 + struct fsg_buffhd *next_buffhd_to_drain;
1638 +
1639 + int thread_wakeup_needed;
1640 + struct completion thread_notifier;
1641 + struct task_struct *thread_task;
1642 +
1643 + int cmnd_size;
1644 + u8 cmnd[MAX_COMMAND_SIZE];
1645 + enum data_direction data_dir;
1646 + u32 data_size;
1647 + u32 data_size_from_cmnd;
1648 + u32 tag;
1649 + unsigned int lun;
1650 + u32 residue;
1651 + u32 usb_amount_left;
1652 +
1653 + /* The CB protocol offers no way for a host to know when a command
1654 + * has completed. As a result the next command may arrive early,
1655 + * and we will still have to handle it. For that reason we need
1656 + * a buffer to store new commands when using CB (or CBI, which
1657 + * does not oblige a host to wait for command completion either). */
1658 + int cbbuf_cmnd_size;
1659 + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
1660 +
1661 + unsigned int nluns;
1662 + struct fsg_lun *luns;
1663 + struct fsg_lun *curlun;
1664 + /* Must be the last entry */
1665 + struct fsg_buffhd buffhds[];
1666 +};
1667 +
1668 +typedef void (*fsg_routine_t)(struct fsg_dev *);
1669 +
1670 +static int exception_in_progress(struct fsg_dev *fsg)
1671 +{
1672 + return (fsg->state > FSG_STATE_IDLE);
1673 +}
1674 +
1675 +/* Make bulk-out requests be divisible by the maxpacket size */
1676 +static void set_bulk_out_req_length(struct fsg_dev *fsg,
1677 + struct fsg_buffhd *bh, unsigned int length)
1678 +{
1679 + unsigned int rem;
1680 +
1681 + bh->bulk_out_intended_length = length;
1682 + rem = length % fsg->bulk_out_maxpacket;
1683 + if (rem > 0)
1684 + length += fsg->bulk_out_maxpacket - rem;
1685 + bh->outreq->length = length;
1686 +}
1687 +
1688 +static struct fsg_dev *the_fsg;
1689 +static struct usb_gadget_driver fsg_driver;
1690 +
1691 +
1692 +/*-------------------------------------------------------------------------*/
1693 +
1694 +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
1695 +{
1696 + const char *name;
1697 +
1698 + if (ep == fsg->bulk_in)
1699 + name = "bulk-in";
1700 + else if (ep == fsg->bulk_out)
1701 + name = "bulk-out";
1702 + else
1703 + name = ep->name;
1704 + DBG(fsg, "%s set halt\n", name);
1705 + return usb_ep_set_halt(ep);
1706 +}
1707 +
1708 +
1709 +/*-------------------------------------------------------------------------*/
1710 +
1711 +/*
1712 + * DESCRIPTORS ... most are static, but strings and (full) configuration
1713 + * descriptors are built on demand. Also the (static) config and interface
1714 + * descriptors are adjusted during fsg_bind().
1715 + */
1716 +
1717 +/* There is only one configuration. */
1718 +#define CONFIG_VALUE 1
1719 +
1720 +static struct usb_device_descriptor
1721 +device_desc = {
1722 + .bLength = sizeof device_desc,
1723 + .bDescriptorType = USB_DT_DEVICE,
1724 +
1725 + .bcdUSB = cpu_to_le16(0x0200),
1726 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
1727 +
1728 + /* The next three values can be overridden by module parameters */
1729 + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
1730 + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
1731 + .bcdDevice = cpu_to_le16(0xffff),
1732 +
1733 + .iManufacturer = FSG_STRING_MANUFACTURER,
1734 + .iProduct = FSG_STRING_PRODUCT,
1735 + .iSerialNumber = FSG_STRING_SERIAL,
1736 + .bNumConfigurations = 1,
1737 +};
1738 +
1739 +static struct usb_config_descriptor
1740 +config_desc = {
1741 + .bLength = sizeof config_desc,
1742 + .bDescriptorType = USB_DT_CONFIG,
1743 +
1744 + /* wTotalLength computed by usb_gadget_config_buf() */
1745 + .bNumInterfaces = 1,
1746 + .bConfigurationValue = CONFIG_VALUE,
1747 + .iConfiguration = FSG_STRING_CONFIG,
1748 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1749 + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
1750 +};
1751 +
1752 +
1753 +static struct usb_qualifier_descriptor
1754 +dev_qualifier = {
1755 + .bLength = sizeof dev_qualifier,
1756 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
1757 +
1758 + .bcdUSB = cpu_to_le16(0x0200),
1759 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
1760 +
1761 + .bNumConfigurations = 1,
1762 +};
1763 +
1764 +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
1765 +{
1766 + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
1767 + buf += USB_DT_BOS_SIZE;
1768 +
1769 + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
1770 + buf += USB_DT_USB_EXT_CAP_SIZE;
1771 +
1772 + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
1773 +
1774 + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
1775 + + USB_DT_USB_EXT_CAP_SIZE;
1776 +}
1777 +
1778 +/*
1779 + * Config descriptors must agree with the code that sets configurations
1780 + * and with code managing interfaces and their altsettings. They must
1781 + * also handle different speeds and other-speed requests.
1782 + */
1783 +static int populate_config_buf(struct usb_gadget *gadget,
1784 + u8 *buf, u8 type, unsigned index)
1785 +{
1786 + enum usb_device_speed speed = gadget->speed;
1787 + int len;
1788 + const struct usb_descriptor_header **function;
1789 +
1790 + if (index > 0)
1791 + return -EINVAL;
1792 +
1793 + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
1794 + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
1795 + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
1796 + ? (const struct usb_descriptor_header **)fsg_hs_function
1797 + : (const struct usb_descriptor_header **)fsg_fs_function;
1798 +
1799 + /* for now, don't advertise srp-only devices */
1800 + if (!gadget_is_otg(gadget))
1801 + function++;
1802 +
1803 + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
1804 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1805 + return len;
1806 +}
1807 +
1808 +
1809 +/*-------------------------------------------------------------------------*/
1810 +
1811 +/* These routines may be called in process context or in_irq */
1812 +
1813 +/* Caller must hold fsg->lock */
1814 +static void wakeup_thread(struct fsg_dev *fsg)
1815 +{
1816 + /* Tell the main thread that something has happened */
1817 + fsg->thread_wakeup_needed = 1;
1818 + if (fsg->thread_task)
1819 + wake_up_process(fsg->thread_task);
1820 +}
1821 +
1822 +
1823 +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
1824 +{
1825 + unsigned long flags;
1826 +
1827 + /* Do nothing if a higher-priority exception is already in progress.
1828 + * If a lower-or-equal priority exception is in progress, preempt it
1829 + * and notify the main thread by sending it a signal. */
1830 + spin_lock_irqsave(&fsg->lock, flags);
1831 + if (fsg->state <= new_state) {
1832 + fsg->exception_req_tag = fsg->ep0_req_tag;
1833 + fsg->state = new_state;
1834 + if (fsg->thread_task)
1835 + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
1836 + fsg->thread_task);
1837 + }
1838 + spin_unlock_irqrestore(&fsg->lock, flags);
1839 +}
1840 +
1841 +
1842 +/*-------------------------------------------------------------------------*/
1843 +
1844 +/* The disconnect callback and ep0 routines. These always run in_irq,
1845 + * except that ep0_queue() is called in the main thread to acknowledge
1846 + * completion of various requests: set config, set interface, and
1847 + * Bulk-only device reset. */
1848 +
1849 +static void fsg_disconnect(struct usb_gadget *gadget)
1850 +{
1851 + struct fsg_dev *fsg = get_gadget_data(gadget);
1852 +
1853 + DBG(fsg, "disconnect or port reset\n");
1854 + raise_exception(fsg, FSG_STATE_DISCONNECT);
1855 +}
1856 +
1857 +
1858 +static int ep0_queue(struct fsg_dev *fsg)
1859 +{
1860 + int rc;
1861 +
1862 + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
1863 + if (rc != 0 && rc != -ESHUTDOWN) {
1864 +
1865 + /* We can't do much more than wait for a reset */
1866 + WARNING(fsg, "error in submission: %s --> %d\n",
1867 + fsg->ep0->name, rc);
1868 + }
1869 + return rc;
1870 +}
1871 +
1872 +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
1873 +{
1874 + struct fsg_dev *fsg = ep->driver_data;
1875 +
1876 + if (req->actual > 0)
1877 + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
1878 + if (req->status || req->actual != req->length)
1879 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1880 + req->status, req->actual, req->length);
1881 + if (req->status == -ECONNRESET) // Request was cancelled
1882 + usb_ep_fifo_flush(ep);
1883 +
1884 + if (req->status == 0 && req->context)
1885 + ((fsg_routine_t) (req->context))(fsg);
1886 +}
1887 +
1888 +
1889 +/*-------------------------------------------------------------------------*/
1890 +
1891 +/* Bulk and interrupt endpoint completion handlers.
1892 + * These always run in_irq. */
1893 +
1894 +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
1895 +{
1896 + struct fsg_dev *fsg = ep->driver_data;
1897 + struct fsg_buffhd *bh = req->context;
1898 +
1899 + if (req->status || req->actual != req->length)
1900 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1901 + req->status, req->actual, req->length);
1902 + if (req->status == -ECONNRESET) // Request was cancelled
1903 + usb_ep_fifo_flush(ep);
1904 +
1905 + /* Hold the lock while we update the request and buffer states */
1906 + smp_wmb();
1907 + spin_lock(&fsg->lock);
1908 + bh->inreq_busy = 0;
1909 + bh->state = BUF_STATE_EMPTY;
1910 + wakeup_thread(fsg);
1911 + spin_unlock(&fsg->lock);
1912 +}
1913 +
1914 +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
1915 +{
1916 + struct fsg_dev *fsg = ep->driver_data;
1917 + struct fsg_buffhd *bh = req->context;
1918 +
1919 + dump_msg(fsg, "bulk-out", req->buf, req->actual);
1920 + if (req->status || req->actual != bh->bulk_out_intended_length)
1921 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1922 + req->status, req->actual,
1923 + bh->bulk_out_intended_length);
1924 + if (req->status == -ECONNRESET) // Request was cancelled
1925 + usb_ep_fifo_flush(ep);
1926 +
1927 + /* Hold the lock while we update the request and buffer states */
1928 + smp_wmb();
1929 + spin_lock(&fsg->lock);
1930 + bh->outreq_busy = 0;
1931 + bh->state = BUF_STATE_FULL;
1932 + wakeup_thread(fsg);
1933 + spin_unlock(&fsg->lock);
1934 +}
1935 +
1936 +
1937 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1938 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
1939 +{
1940 + struct fsg_dev *fsg = ep->driver_data;
1941 + struct fsg_buffhd *bh = req->context;
1942 +
1943 + if (req->status || req->actual != req->length)
1944 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1945 + req->status, req->actual, req->length);
1946 + if (req->status == -ECONNRESET) // Request was cancelled
1947 + usb_ep_fifo_flush(ep);
1948 +
1949 + /* Hold the lock while we update the request and buffer states */
1950 + smp_wmb();
1951 + spin_lock(&fsg->lock);
1952 + fsg->intreq_busy = 0;
1953 + bh->state = BUF_STATE_EMPTY;
1954 + wakeup_thread(fsg);
1955 + spin_unlock(&fsg->lock);
1956 +}
1957 +
1958 +#else
1959 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
1960 +{}
1961 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1962 +
1963 +
1964 +/*-------------------------------------------------------------------------*/
1965 +
1966 +/* Ep0 class-specific handlers. These always run in_irq. */
1967 +
1968 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1969 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1970 +{
1971 + struct usb_request *req = fsg->ep0req;
1972 + static u8 cbi_reset_cmnd[6] = {
1973 + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
1974 +
1975 + /* Error in command transfer? */
1976 + if (req->status || req->length != req->actual ||
1977 + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
1978 +
1979 + /* Not all controllers allow a protocol stall after
1980 + * receiving control-out data, but we'll try anyway. */
1981 + fsg_set_halt(fsg, fsg->ep0);
1982 + return; // Wait for reset
1983 + }
1984 +
1985 + /* Is it the special reset command? */
1986 + if (req->actual >= sizeof cbi_reset_cmnd &&
1987 + memcmp(req->buf, cbi_reset_cmnd,
1988 + sizeof cbi_reset_cmnd) == 0) {
1989 +
1990 + /* Raise an exception to stop the current operation
1991 + * and reinitialize our state. */
1992 + DBG(fsg, "cbi reset request\n");
1993 + raise_exception(fsg, FSG_STATE_RESET);
1994 + return;
1995 + }
1996 +
1997 + VDBG(fsg, "CB[I] accept device-specific command\n");
1998 + spin_lock(&fsg->lock);
1999 +
2000 + /* Save the command for later */
2001 + if (fsg->cbbuf_cmnd_size)
2002 + WARNING(fsg, "CB[I] overwriting previous command\n");
2003 + fsg->cbbuf_cmnd_size = req->actual;
2004 + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
2005 +
2006 + wakeup_thread(fsg);
2007 + spin_unlock(&fsg->lock);
2008 +}
2009 +
2010 +#else
2011 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2012 +{}
2013 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
2014 +
2015 +
2016 +static int class_setup_req(struct fsg_dev *fsg,
2017 + const struct usb_ctrlrequest *ctrl)
2018 +{
2019 + struct usb_request *req = fsg->ep0req;
2020 + int value = -EOPNOTSUPP;
2021 + u16 w_index = le16_to_cpu(ctrl->wIndex);
2022 + u16 w_value = le16_to_cpu(ctrl->wValue);
2023 + u16 w_length = le16_to_cpu(ctrl->wLength);
2024 +
2025 + if (!fsg->config)
2026 + return value;
2027 +
2028 + /* Handle Bulk-only class-specific requests */
2029 + if (transport_is_bbb()) {
2030 + switch (ctrl->bRequest) {
2031 +
2032 + case US_BULK_RESET_REQUEST:
2033 + if (ctrl->bRequestType != (USB_DIR_OUT |
2034 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
2035 + break;
2036 + if (w_index != 0 || w_value != 0 || w_length != 0) {
2037 + value = -EDOM;
2038 + break;
2039 + }
2040 +
2041 + /* Raise an exception to stop the current operation
2042 + * and reinitialize our state. */
2043 + DBG(fsg, "bulk reset request\n");
2044 + raise_exception(fsg, FSG_STATE_RESET);
2045 + value = DELAYED_STATUS;
2046 + break;
2047 +
2048 + case US_BULK_GET_MAX_LUN:
2049 + if (ctrl->bRequestType != (USB_DIR_IN |
2050 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
2051 + break;
2052 + if (w_index != 0 || w_value != 0 || w_length != 1) {
2053 + value = -EDOM;
2054 + break;
2055 + }
2056 + VDBG(fsg, "get max LUN\n");
2057 + *(u8 *) req->buf = fsg->nluns - 1;
2058 + value = 1;
2059 + break;
2060 + }
2061 + }
2062 +
2063 + /* Handle CBI class-specific requests */
2064 + else {
2065 + switch (ctrl->bRequest) {
2066 +
2067 + case USB_CBI_ADSC_REQUEST:
2068 + if (ctrl->bRequestType != (USB_DIR_OUT |
2069 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
2070 + break;
2071 + if (w_index != 0 || w_value != 0) {
2072 + value = -EDOM;
2073 + break;
2074 + }
2075 + if (w_length > MAX_COMMAND_SIZE) {
2076 + value = -EOVERFLOW;
2077 + break;
2078 + }
2079 + value = w_length;
2080 + fsg->ep0req->context = received_cbi_adsc;
2081 + break;
2082 + }
2083 + }
2084 +
2085 + if (value == -EOPNOTSUPP)
2086 + VDBG(fsg,
2087 + "unknown class-specific control req "
2088 + "%02x.%02x v%04x i%04x l%u\n",
2089 + ctrl->bRequestType, ctrl->bRequest,
2090 + le16_to_cpu(ctrl->wValue), w_index, w_length);
2091 + return value;
2092 +}
2093 +
2094 +
2095 +/*-------------------------------------------------------------------------*/
2096 +
2097 +/* Ep0 standard request handlers. These always run in_irq. */
2098 +
2099 +static int standard_setup_req(struct fsg_dev *fsg,
2100 + const struct usb_ctrlrequest *ctrl)
2101 +{
2102 + struct usb_request *req = fsg->ep0req;
2103 + int value = -EOPNOTSUPP;
2104 + u16 w_index = le16_to_cpu(ctrl->wIndex);
2105 + u16 w_value = le16_to_cpu(ctrl->wValue);
2106 +
2107 + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
2108 + * but config change events will also reconfigure hardware. */
2109 + switch (ctrl->bRequest) {
2110 +
2111 + case USB_REQ_GET_DESCRIPTOR:
2112 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2113 + USB_RECIP_DEVICE))
2114 + break;
2115 + switch (w_value >> 8) {
2116 +
2117 + case USB_DT_DEVICE:
2118 + VDBG(fsg, "get device descriptor\n");
2119 + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
2120 + value = sizeof device_desc;
2121 + memcpy(req->buf, &device_desc, value);
2122 + break;
2123 + case USB_DT_DEVICE_QUALIFIER:
2124 + VDBG(fsg, "get device qualifier\n");
2125 + if (!gadget_is_dualspeed(fsg->gadget) ||
2126 + fsg->gadget->speed == USB_SPEED_SUPER)
2127 + break;
2128 + /*
2129 + * Assume ep0 uses the same maxpacket value for both
2130 + * speeds
2131 + */
2132 + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
2133 + value = sizeof dev_qualifier;
2134 + memcpy(req->buf, &dev_qualifier, value);
2135 + break;
2136 +
2137 + case USB_DT_OTHER_SPEED_CONFIG:
2138 + VDBG(fsg, "get other-speed config descriptor\n");
2139 + if (!gadget_is_dualspeed(fsg->gadget) ||
2140 + fsg->gadget->speed == USB_SPEED_SUPER)
2141 + break;
2142 + goto get_config;
2143 + case USB_DT_CONFIG:
2144 + VDBG(fsg, "get configuration descriptor\n");
2145 +get_config:
2146 + value = populate_config_buf(fsg->gadget,
2147 + req->buf,
2148 + w_value >> 8,
2149 + w_value & 0xff);
2150 + break;
2151 +
2152 + case USB_DT_STRING:
2153 + VDBG(fsg, "get string descriptor\n");
2154 +
2155 + /* wIndex == language code */
2156 + value = usb_gadget_get_string(&fsg_stringtab,
2157 + w_value & 0xff, req->buf);
2158 + break;
2159 +
2160 + case USB_DT_BOS:
2161 + VDBG(fsg, "get bos descriptor\n");
2162 +
2163 + if (gadget_is_superspeed(fsg->gadget))
2164 + value = populate_bos(fsg, req->buf);
2165 + break;
2166 + }
2167 +
2168 + break;
2169 +
2170 + /* One config, two speeds */
2171 + case USB_REQ_SET_CONFIGURATION:
2172 + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
2173 + USB_RECIP_DEVICE))
2174 + break;
2175 + VDBG(fsg, "set configuration\n");
2176 + if (w_value == CONFIG_VALUE || w_value == 0) {
2177 + fsg->new_config = w_value;
2178 +
2179 + /* Raise an exception to wipe out previous transaction
2180 + * state (queued bufs, etc) and set the new config. */
2181 + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
2182 + value = DELAYED_STATUS;
2183 + }
2184 + break;
2185 + case USB_REQ_GET_CONFIGURATION:
2186 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2187 + USB_RECIP_DEVICE))
2188 + break;
2189 + VDBG(fsg, "get configuration\n");
2190 + *(u8 *) req->buf = fsg->config;
2191 + value = 1;
2192 + break;
2193 +
2194 + case USB_REQ_SET_INTERFACE:
2195 + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
2196 + USB_RECIP_INTERFACE))
2197 + break;
2198 + if (fsg->config && w_index == 0) {
2199 +
2200 + /* Raise an exception to wipe out previous transaction
2201 + * state (queued bufs, etc) and install the new
2202 + * interface altsetting. */
2203 + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
2204 + value = DELAYED_STATUS;
2205 + }
2206 + break;
2207 + case USB_REQ_GET_INTERFACE:
2208 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2209 + USB_RECIP_INTERFACE))
2210 + break;
2211 + if (!fsg->config)
2212 + break;
2213 + if (w_index != 0) {
2214 + value = -EDOM;
2215 + break;
2216 + }
2217 + VDBG(fsg, "get interface\n");
2218 + *(u8 *) req->buf = 0;
2219 + value = 1;
2220 + break;
2221 +
2222 + default:
2223 + VDBG(fsg,
2224 + "unknown control req %02x.%02x v%04x i%04x l%u\n",
2225 + ctrl->bRequestType, ctrl->bRequest,
2226 + w_value, w_index, le16_to_cpu(ctrl->wLength));
2227 + }
2228 +
2229 + return value;
2230 +}
2231 +
2232 +
2233 +static int fsg_setup(struct usb_gadget *gadget,
2234 + const struct usb_ctrlrequest *ctrl)
2235 +{
2236 + struct fsg_dev *fsg = get_gadget_data(gadget);
2237 + int rc;
2238 + int w_length = le16_to_cpu(ctrl->wLength);
2239 +
2240 + ++fsg->ep0_req_tag; // Record arrival of a new request
2241 + fsg->ep0req->context = NULL;
2242 + fsg->ep0req->length = 0;
2243 + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
2244 +
2245 + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
2246 + rc = class_setup_req(fsg, ctrl);
2247 + else
2248 + rc = standard_setup_req(fsg, ctrl);
2249 +
2250 + /* Respond with data/status or defer until later? */
2251 + if (rc >= 0 && rc != DELAYED_STATUS) {
2252 + rc = min(rc, w_length);
2253 + fsg->ep0req->length = rc;
2254 + fsg->ep0req->zero = rc < w_length;
2255 + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
2256 + "ep0-in" : "ep0-out");
2257 + rc = ep0_queue(fsg);
2258 + }
2259 +
2260 + /* Device either stalls (rc < 0) or reports success */
2261 + return rc;
2262 +}
2263 +
2264 +
2265 +/*-------------------------------------------------------------------------*/
2266 +
2267 +/* All the following routines run in process context */
2268 +
2269 +
2270 +/* Use this for bulk or interrupt transfers, not ep0 */
2271 +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
2272 + struct usb_request *req, int *pbusy,
2273 + enum fsg_buffer_state *state)
2274 +{
2275 + int rc;
2276 +
2277 + if (ep == fsg->bulk_in)
2278 + dump_msg(fsg, "bulk-in", req->buf, req->length);
2279 + else if (ep == fsg->intr_in)
2280 + dump_msg(fsg, "intr-in", req->buf, req->length);
2281 +
2282 + spin_lock_irq(&fsg->lock);
2283 + *pbusy = 1;
2284 + *state = BUF_STATE_BUSY;
2285 + spin_unlock_irq(&fsg->lock);
2286 + rc = usb_ep_queue(ep, req, GFP_KERNEL);
2287 + if (rc != 0) {
2288 + *pbusy = 0;
2289 + *state = BUF_STATE_EMPTY;
2290 +
2291 + /* We can't do much more than wait for a reset */
2292 +
2293 + /* Note: currently the net2280 driver fails zero-length
2294 + * submissions if DMA is enabled. */
2295 + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
2296 + req->length == 0))
2297 + WARNING(fsg, "error in submission: %s --> %d\n",
2298 + ep->name, rc);
2299 + }
2300 +}
2301 +
2302 +
2303 +static int sleep_thread(struct fsg_dev *fsg)
2304 +{
2305 + int rc = 0;
2306 +
2307 + /* Wait until a signal arrives or we are woken up */
2308 + for (;;) {
2309 + try_to_freeze();
2310 + set_current_state(TASK_INTERRUPTIBLE);
2311 + if (signal_pending(current)) {
2312 + rc = -EINTR;
2313 + break;
2314 + }
2315 + if (fsg->thread_wakeup_needed)
2316 + break;
2317 + schedule();
2318 + }
2319 + __set_current_state(TASK_RUNNING);
2320 + fsg->thread_wakeup_needed = 0;
2321 + return rc;
2322 +}
2323 +
2324 +
2325 +/*-------------------------------------------------------------------------*/
2326 +
2327 +static int do_read(struct fsg_dev *fsg)
2328 +{
2329 + struct fsg_lun *curlun = fsg->curlun;
2330 + u32 lba;
2331 + struct fsg_buffhd *bh;
2332 + int rc;
2333 + u32 amount_left;
2334 + loff_t file_offset, file_offset_tmp;
2335 + unsigned int amount;
2336 + ssize_t nread;
2337 +
2338 + /* Get the starting Logical Block Address and check that it's
2339 + * not too big */
2340 + if (fsg->cmnd[0] == READ_6)
2341 + lba = get_unaligned_be24(&fsg->cmnd[1]);
2342 + else {
2343 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2344 +
2345 + /* We allow DPO (Disable Page Out = don't save data in the
2346 + * cache) and FUA (Force Unit Access = don't read from the
2347 + * cache), but we don't implement them. */
2348 + if ((fsg->cmnd[1] & ~0x18) != 0) {
2349 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2350 + return -EINVAL;
2351 + }
2352 + }
2353 + if (lba >= curlun->num_sectors) {
2354 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2355 + return -EINVAL;
2356 + }
2357 + file_offset = ((loff_t) lba) << curlun->blkbits;
2358 +
2359 + /* Carry out the file reads */
2360 + amount_left = fsg->data_size_from_cmnd;
2361 + if (unlikely(amount_left == 0))
2362 + return -EIO; // No default reply
2363 +
2364 + for (;;) {
2365 +
2366 + /* Figure out how much we need to read:
2367 + * Try to read the remaining amount.
2368 + * But don't read more than the buffer size.
2369 + * And don't try to read past the end of the file.
2370 + */
2371 + amount = min((unsigned int) amount_left, mod_data.buflen);
2372 + amount = min((loff_t) amount,
2373 + curlun->file_length - file_offset);
2374 +
2375 + /* Wait for the next buffer to become available */
2376 + bh = fsg->next_buffhd_to_fill;
2377 + while (bh->state != BUF_STATE_EMPTY) {
2378 + rc = sleep_thread(fsg);
2379 + if (rc)
2380 + return rc;
2381 + }
2382 +
2383 + /* If we were asked to read past the end of file,
2384 + * end with an empty buffer. */
2385 + if (amount == 0) {
2386 + curlun->sense_data =
2387 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2388 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2389 + curlun->info_valid = 1;
2390 + bh->inreq->length = 0;
2391 + bh->state = BUF_STATE_FULL;
2392 + break;
2393 + }
2394 +
2395 + /* Perform the read */
2396 + file_offset_tmp = file_offset;
2397 + nread = vfs_read(curlun->filp,
2398 + (char __user *) bh->buf,
2399 + amount, &file_offset_tmp);
2400 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
2401 + (unsigned long long) file_offset,
2402 + (int) nread);
2403 + if (signal_pending(current))
2404 + return -EINTR;
2405 +
2406 + if (nread < 0) {
2407 + LDBG(curlun, "error in file read: %d\n",
2408 + (int) nread);
2409 + nread = 0;
2410 + } else if (nread < amount) {
2411 + LDBG(curlun, "partial file read: %d/%u\n",
2412 + (int) nread, amount);
2413 + nread = round_down(nread, curlun->blksize);
2414 + }
2415 + file_offset += nread;
2416 + amount_left -= nread;
2417 + fsg->residue -= nread;
2418 +
2419 + /* Except at the end of the transfer, nread will be
2420 + * equal to the buffer size, which is divisible by the
2421 + * bulk-in maxpacket size.
2422 + */
2423 + bh->inreq->length = nread;
2424 + bh->state = BUF_STATE_FULL;
2425 +
2426 + /* If an error occurred, report it and its position */
2427 + if (nread < amount) {
2428 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
2429 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2430 + curlun->info_valid = 1;
2431 + break;
2432 + }
2433 +
2434 + if (amount_left == 0)
2435 + break; // No more left to read
2436 +
2437 + /* Send this buffer and go read some more */
2438 + bh->inreq->zero = 0;
2439 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
2440 + &bh->inreq_busy, &bh->state);
2441 + fsg->next_buffhd_to_fill = bh->next;
2442 + }
2443 +
2444 + return -EIO; // No default reply
2445 +}
2446 +
2447 +
2448 +/*-------------------------------------------------------------------------*/
2449 +
2450 +static int do_write(struct fsg_dev *fsg)
2451 +{
2452 + struct fsg_lun *curlun = fsg->curlun;
2453 + u32 lba;
2454 + struct fsg_buffhd *bh;
2455 + int get_some_more;
2456 + u32 amount_left_to_req, amount_left_to_write;
2457 + loff_t usb_offset, file_offset, file_offset_tmp;
2458 + unsigned int amount;
2459 + ssize_t nwritten;
2460 + int rc;
2461 +
2462 + if (curlun->ro) {
2463 + curlun->sense_data = SS_WRITE_PROTECTED;
2464 + return -EINVAL;
2465 + }
2466 + spin_lock(&curlun->filp->f_lock);
2467 + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
2468 + spin_unlock(&curlun->filp->f_lock);
2469 +
2470 + /* Get the starting Logical Block Address and check that it's
2471 + * not too big */
2472 + if (fsg->cmnd[0] == WRITE_6)
2473 + lba = get_unaligned_be24(&fsg->cmnd[1]);
2474 + else {
2475 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2476 +
2477 + /* We allow DPO (Disable Page Out = don't save data in the
2478 + * cache) and FUA (Force Unit Access = write directly to the
2479 + * medium). We don't implement DPO; we implement FUA by
2480 + * performing synchronous output. */
2481 + if ((fsg->cmnd[1] & ~0x18) != 0) {
2482 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2483 + return -EINVAL;
2484 + }
2485 + /* FUA */
2486 + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
2487 + spin_lock(&curlun->filp->f_lock);
2488 + curlun->filp->f_flags |= O_DSYNC;
2489 + spin_unlock(&curlun->filp->f_lock);
2490 + }
2491 + }
2492 + if (lba >= curlun->num_sectors) {
2493 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2494 + return -EINVAL;
2495 + }
2496 +
2497 + /* Carry out the file writes */
2498 + get_some_more = 1;
2499 + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
2500 + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
2501 +
2502 + while (amount_left_to_write > 0) {
2503 +
2504 + /* Queue a request for more data from the host */
2505 + bh = fsg->next_buffhd_to_fill;
2506 + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
2507 +
2508 + /* Figure out how much we want to get:
2509 + * Try to get the remaining amount,
2510 + * but not more than the buffer size.
2511 + */
2512 + amount = min(amount_left_to_req, mod_data.buflen);
2513 +
2514 + /* Beyond the end of the backing file? */
2515 + if (usb_offset >= curlun->file_length) {
2516 + get_some_more = 0;
2517 + curlun->sense_data =
2518 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2519 + curlun->sense_data_info = usb_offset >> curlun->blkbits;
2520 + curlun->info_valid = 1;
2521 + continue;
2522 + }
2523 +
2524 + /* Get the next buffer */
2525 + usb_offset += amount;
2526 + fsg->usb_amount_left -= amount;
2527 + amount_left_to_req -= amount;
2528 + if (amount_left_to_req == 0)
2529 + get_some_more = 0;
2530 +
2531 + /* Except at the end of the transfer, amount will be
2532 + * equal to the buffer size, which is divisible by
2533 + * the bulk-out maxpacket size.
2534 + */
2535 + set_bulk_out_req_length(fsg, bh, amount);
2536 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
2537 + &bh->outreq_busy, &bh->state);
2538 + fsg->next_buffhd_to_fill = bh->next;
2539 + continue;
2540 + }
2541 +
2542 + /* Write the received data to the backing file */
2543 + bh = fsg->next_buffhd_to_drain;
2544 + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
2545 + break; // We stopped early
2546 + if (bh->state == BUF_STATE_FULL) {
2547 + smp_rmb();
2548 + fsg->next_buffhd_to_drain = bh->next;
2549 + bh->state = BUF_STATE_EMPTY;
2550 +
2551 + /* Did something go wrong with the transfer? */
2552 + if (bh->outreq->status != 0) {
2553 + curlun->sense_data = SS_COMMUNICATION_FAILURE;
2554 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2555 + curlun->info_valid = 1;
2556 + break;
2557 + }
2558 +
2559 + amount = bh->outreq->actual;
2560 + if (curlun->file_length - file_offset < amount) {
2561 + LERROR(curlun,
2562 + "write %u @ %llu beyond end %llu\n",
2563 + amount, (unsigned long long) file_offset,
2564 + (unsigned long long) curlun->file_length);
2565 + amount = curlun->file_length - file_offset;
2566 + }
2567 +
2568 + /* Don't accept excess data. The spec doesn't say
2569 + * what to do in this case. We'll ignore the error.
2570 + */
2571 + amount = min(amount, bh->bulk_out_intended_length);
2572 +
2573 + /* Don't write a partial block */
2574 + amount = round_down(amount, curlun->blksize);
2575 + if (amount == 0)
2576 + goto empty_write;
2577 +
2578 + /* Perform the write */
2579 + file_offset_tmp = file_offset;
2580 + nwritten = vfs_write(curlun->filp,
2581 + (char __user *) bh->buf,
2582 + amount, &file_offset_tmp);
2583 + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
2584 + (unsigned long long) file_offset,
2585 + (int) nwritten);
2586 + if (signal_pending(current))
2587 + return -EINTR; // Interrupted!
2588 +
2589 + if (nwritten < 0) {
2590 + LDBG(curlun, "error in file write: %d\n",
2591 + (int) nwritten);
2592 + nwritten = 0;
2593 + } else if (nwritten < amount) {
2594 + LDBG(curlun, "partial file write: %d/%u\n",
2595 + (int) nwritten, amount);
2596 + nwritten = round_down(nwritten, curlun->blksize);
2597 + }
2598 + file_offset += nwritten;
2599 + amount_left_to_write -= nwritten;
2600 + fsg->residue -= nwritten;
2601 +
2602 + /* If an error occurred, report it and its position */
2603 + if (nwritten < amount) {
2604 + curlun->sense_data = SS_WRITE_ERROR;
2605 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2606 + curlun->info_valid = 1;
2607 + break;
2608 + }
2609 +
2610 + empty_write:
2611 + /* Did the host decide to stop early? */
2612 + if (bh->outreq->actual < bh->bulk_out_intended_length) {
2613 + fsg->short_packet_received = 1;
2614 + break;
2615 + }
2616 + continue;
2617 + }
2618 +
2619 + /* Wait for something to happen */
2620 + rc = sleep_thread(fsg);
2621 + if (rc)
2622 + return rc;
2623 + }
2624 +
2625 + return -EIO; // No default reply
2626 +}
2627 +
2628 +
2629 +/*-------------------------------------------------------------------------*/
2630 +
2631 +static int do_synchronize_cache(struct fsg_dev *fsg)
2632 +{
2633 + struct fsg_lun *curlun = fsg->curlun;
2634 + int rc;
2635 +
2636 + /* We ignore the requested LBA and write out all file's
2637 + * dirty data buffers. */
2638 + rc = fsg_lun_fsync_sub(curlun);
2639 + if (rc)
2640 + curlun->sense_data = SS_WRITE_ERROR;
2641 + return 0;
2642 +}
2643 +
2644 +
2645 +/*-------------------------------------------------------------------------*/
2646 +
2647 +static void invalidate_sub(struct fsg_lun *curlun)
2648 +{
2649 + struct file *filp = curlun->filp;
2650 + struct inode *inode = filp->f_path.dentry->d_inode;
2651 + unsigned long rc;
2652 +
2653 + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
2654 + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
2655 +}
2656 +
2657 +static int do_verify(struct fsg_dev *fsg)
2658 +{
2659 + struct fsg_lun *curlun = fsg->curlun;
2660 + u32 lba;
2661 + u32 verification_length;
2662 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
2663 + loff_t file_offset, file_offset_tmp;
2664 + u32 amount_left;
2665 + unsigned int amount;
2666 + ssize_t nread;
2667 +
2668 + /* Get the starting Logical Block Address and check that it's
2669 + * not too big */
2670 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2671 + if (lba >= curlun->num_sectors) {
2672 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2673 + return -EINVAL;
2674 + }
2675 +
2676 + /* We allow DPO (Disable Page Out = don't save data in the
2677 + * cache) but we don't implement it. */
2678 + if ((fsg->cmnd[1] & ~0x10) != 0) {
2679 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2680 + return -EINVAL;
2681 + }
2682 +
2683 + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
2684 + if (unlikely(verification_length == 0))
2685 + return -EIO; // No default reply
2686 +
2687 + /* Prepare to carry out the file verify */
2688 + amount_left = verification_length << curlun->blkbits;
2689 + file_offset = ((loff_t) lba) << curlun->blkbits;
2690 +
2691 + /* Write out all the dirty buffers before invalidating them */
2692 + fsg_lun_fsync_sub(curlun);
2693 + if (signal_pending(current))
2694 + return -EINTR;
2695 +
2696 + invalidate_sub(curlun);
2697 + if (signal_pending(current))
2698 + return -EINTR;
2699 +
2700 + /* Just try to read the requested blocks */
2701 + while (amount_left > 0) {
2702 +
2703 + /* Figure out how much we need to read:
2704 + * Try to read the remaining amount, but not more than
2705 + * the buffer size.
2706 + * And don't try to read past the end of the file.
2707 + */
2708 + amount = min((unsigned int) amount_left, mod_data.buflen);
2709 + amount = min((loff_t) amount,
2710 + curlun->file_length - file_offset);
2711 + if (amount == 0) {
2712 + curlun->sense_data =
2713 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2714 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2715 + curlun->info_valid = 1;
2716 + break;
2717 + }
2718 +
2719 + /* Perform the read */
2720 + file_offset_tmp = file_offset;
2721 + nread = vfs_read(curlun->filp,
2722 + (char __user *) bh->buf,
2723 + amount, &file_offset_tmp);
2724 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
2725 + (unsigned long long) file_offset,
2726 + (int) nread);
2727 + if (signal_pending(current))
2728 + return -EINTR;
2729 +
2730 + if (nread < 0) {
2731 + LDBG(curlun, "error in file verify: %d\n",
2732 + (int) nread);
2733 + nread = 0;
2734 + } else if (nread < amount) {
2735 + LDBG(curlun, "partial file verify: %d/%u\n",
2736 + (int) nread, amount);
2737 + nread = round_down(nread, curlun->blksize);
2738 + }
2739 + if (nread == 0) {
2740 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
2741 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2742 + curlun->info_valid = 1;
2743 + break;
2744 + }
2745 + file_offset += nread;
2746 + amount_left -= nread;
2747 + }
2748 + return 0;
2749 +}
2750 +
2751 +
2752 +/*-------------------------------------------------------------------------*/
2753 +
2754 +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2755 +{
2756 + u8 *buf = (u8 *) bh->buf;
2757 +
2758 + static char vendor_id[] = "Linux ";
2759 + static char product_disk_id[] = "File-Stor Gadget";
2760 + static char product_cdrom_id[] = "File-CD Gadget ";
2761 +
2762 + if (!fsg->curlun) { // Unsupported LUNs are okay
2763 + fsg->bad_lun_okay = 1;
2764 + memset(buf, 0, 36);
2765 + buf[0] = 0x7f; // Unsupported, no device-type
2766 + buf[4] = 31; // Additional length
2767 + return 36;
2768 + }
2769 +
2770 + memset(buf, 0, 8);
2771 + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
2772 + if (mod_data.removable)
2773 + buf[1] = 0x80;
2774 + buf[2] = 2; // ANSI SCSI level 2
2775 + buf[3] = 2; // SCSI-2 INQUIRY data format
2776 + buf[4] = 31; // Additional length
2777 + // No special options
2778 + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
2779 + (mod_data.cdrom ? product_cdrom_id :
2780 + product_disk_id),
2781 + mod_data.release);
2782 + return 36;
2783 +}
2784 +
2785 +
2786 +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2787 +{
2788 + struct fsg_lun *curlun = fsg->curlun;
2789 + u8 *buf = (u8 *) bh->buf;
2790 + u32 sd, sdinfo;
2791 + int valid;
2792 +
2793 + /*
2794 + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
2795 + *
2796 + * If a REQUEST SENSE command is received from an initiator
2797 + * with a pending unit attention condition (before the target
2798 + * generates the contingent allegiance condition), then the
2799 + * target shall either:
2800 + * a) report any pending sense data and preserve the unit
2801 + * attention condition on the logical unit, or,
2802 + * b) report the unit attention condition, may discard any
2803 + * pending sense data, and clear the unit attention
2804 + * condition on the logical unit for that initiator.
2805 + *
2806 + * FSG normally uses option a); enable this code to use option b).
2807 + */
2808 +#if 0
2809 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
2810 + curlun->sense_data = curlun->unit_attention_data;
2811 + curlun->unit_attention_data = SS_NO_SENSE;
2812 + }
2813 +#endif
2814 +
2815 + if (!curlun) { // Unsupported LUNs are okay
2816 + fsg->bad_lun_okay = 1;
2817 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2818 + sdinfo = 0;
2819 + valid = 0;
2820 + } else {
2821 + sd = curlun->sense_data;
2822 + sdinfo = curlun->sense_data_info;
2823 + valid = curlun->info_valid << 7;
2824 + curlun->sense_data = SS_NO_SENSE;
2825 + curlun->sense_data_info = 0;
2826 + curlun->info_valid = 0;
2827 + }
2828 +
2829 + memset(buf, 0, 18);
2830 + buf[0] = valid | 0x70; // Valid, current error
2831 + buf[2] = SK(sd);
2832 + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
2833 + buf[7] = 18 - 8; // Additional sense length
2834 + buf[12] = ASC(sd);
2835 + buf[13] = ASCQ(sd);
2836 + return 18;
2837 +}
2838 +
2839 +
2840 +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2841 +{
2842 + struct fsg_lun *curlun = fsg->curlun;
2843 + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2844 + int pmi = fsg->cmnd[8];
2845 + u8 *buf = (u8 *) bh->buf;
2846 +
2847 + /* Check the PMI and LBA fields */
2848 + if (pmi > 1 || (pmi == 0 && lba != 0)) {
2849 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2850 + return -EINVAL;
2851 + }
2852 +
2853 + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
2854 + /* Max logical block */
2855 + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
2856 + return 8;
2857 +}
2858 +
2859 +
2860 +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2861 +{
2862 + struct fsg_lun *curlun = fsg->curlun;
2863 + int msf = fsg->cmnd[1] & 0x02;
2864 + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2865 + u8 *buf = (u8 *) bh->buf;
2866 +
2867 + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
2868 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2869 + return -EINVAL;
2870 + }
2871 + if (lba >= curlun->num_sectors) {
2872 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2873 + return -EINVAL;
2874 + }
2875 +
2876 + memset(buf, 0, 8);
2877 + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
2878 + store_cdrom_address(&buf[4], msf, lba);
2879 + return 8;
2880 +}
2881 +
2882 +
2883 +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2884 +{
2885 + struct fsg_lun *curlun = fsg->curlun;
2886 + int msf = fsg->cmnd[1] & 0x02;
2887 + int start_track = fsg->cmnd[6];
2888 + u8 *buf = (u8 *) bh->buf;
2889 +
2890 + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
2891 + start_track > 1) {
2892 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2893 + return -EINVAL;
2894 + }
2895 +
2896 + memset(buf, 0, 20);
2897 + buf[1] = (20-2); /* TOC data length */
2898 + buf[2] = 1; /* First track number */
2899 + buf[3] = 1; /* Last track number */
2900 + buf[5] = 0x16; /* Data track, copying allowed */
2901 + buf[6] = 0x01; /* Only track is number 1 */
2902 + store_cdrom_address(&buf[8], msf, 0);
2903 +
2904 + buf[13] = 0x16; /* Lead-out track is data */
2905 + buf[14] = 0xAA; /* Lead-out track number */
2906 + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
2907 + return 20;
2908 +}
2909 +
2910 +
2911 +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2912 +{
2913 + struct fsg_lun *curlun = fsg->curlun;
2914 + int mscmnd = fsg->cmnd[0];
2915 + u8 *buf = (u8 *) bh->buf;
2916 + u8 *buf0 = buf;
2917 + int pc, page_code;
2918 + int changeable_values, all_pages;
2919 + int valid_page = 0;
2920 + int len, limit;
2921 +
2922 + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
2923 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2924 + return -EINVAL;
2925 + }
2926 + pc = fsg->cmnd[2] >> 6;
2927 + page_code = fsg->cmnd[2] & 0x3f;
2928 + if (pc == 3) {
2929 + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
2930 + return -EINVAL;
2931 + }
2932 + changeable_values = (pc == 1);
2933 + all_pages = (page_code == 0x3f);
2934 +
2935 + /* Write the mode parameter header. Fixed values are: default
2936 + * medium type, no cache control (DPOFUA), and no block descriptors.
2937 + * The only variable value is the WriteProtect bit. We will fill in
2938 + * the mode data length later. */
2939 + memset(buf, 0, 8);
2940 + if (mscmnd == MODE_SENSE) {
2941 + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
2942 + buf += 4;
2943 + limit = 255;
2944 + } else { // MODE_SENSE_10
2945 + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
2946 + buf += 8;
2947 + limit = 65535; // Should really be mod_data.buflen
2948 + }
2949 +
2950 + /* No block descriptors */
2951 +
2952 + /* The mode pages, in numerical order. The only page we support
2953 + * is the Caching page. */
2954 + if (page_code == 0x08 || all_pages) {
2955 + valid_page = 1;
2956 + buf[0] = 0x08; // Page code
2957 + buf[1] = 10; // Page length
2958 + memset(buf+2, 0, 10); // None of the fields are changeable
2959 +
2960 + if (!changeable_values) {
2961 + buf[2] = 0x04; // Write cache enable,
2962 + // Read cache not disabled
2963 + // No cache retention priorities
2964 + put_unaligned_be16(0xffff, &buf[4]);
2965 + /* Don't disable prefetch */
2966 + /* Minimum prefetch = 0 */
2967 + put_unaligned_be16(0xffff, &buf[8]);
2968 + /* Maximum prefetch */
2969 + put_unaligned_be16(0xffff, &buf[10]);
2970 + /* Maximum prefetch ceiling */
2971 + }
2972 + buf += 12;
2973 + }
2974 +
2975 + /* Check that a valid page was requested and the mode data length
2976 + * isn't too long. */
2977 + len = buf - buf0;
2978 + if (!valid_page || len > limit) {
2979 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2980 + return -EINVAL;
2981 + }
2982 +
2983 + /* Store the mode data length */
2984 + if (mscmnd == MODE_SENSE)
2985 + buf0[0] = len - 1;
2986 + else
2987 + put_unaligned_be16(len - 2, buf0);
2988 + return len;
2989 +}
2990 +
2991 +
2992 +static int do_start_stop(struct fsg_dev *fsg)
2993 +{
2994 + struct fsg_lun *curlun = fsg->curlun;
2995 + int loej, start;
2996 +
2997 + if (!mod_data.removable) {
2998 + curlun->sense_data = SS_INVALID_COMMAND;
2999 + return -EINVAL;
3000 + }
3001 +
3002 + // int immed = fsg->cmnd[1] & 0x01;
3003 + loej = fsg->cmnd[4] & 0x02;
3004 + start = fsg->cmnd[4] & 0x01;
3005 +
3006 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
3007 + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
3008 + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
3009 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3010 + return -EINVAL;
3011 + }
3012 +
3013 + if (!start) {
3014 +
3015 + /* Are we allowed to unload the media? */
3016 + if (curlun->prevent_medium_removal) {
3017 + LDBG(curlun, "unload attempt prevented\n");
3018 + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
3019 + return -EINVAL;
3020 + }
3021 + if (loej) { // Simulate an unload/eject
3022 + up_read(&fsg->filesem);
3023 + down_write(&fsg->filesem);
3024 + fsg_lun_close(curlun);
3025 + up_write(&fsg->filesem);
3026 + down_read(&fsg->filesem);
3027 + }
3028 + } else {
3029 +
3030 + /* Our emulation doesn't support mounting; the medium is
3031 + * available for use as soon as it is loaded. */
3032 + if (!fsg_lun_is_open(curlun)) {
3033 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
3034 + return -EINVAL;
3035 + }
3036 + }
3037 +#endif
3038 + return 0;
3039 +}
3040 +
3041 +
3042 +static int do_prevent_allow(struct fsg_dev *fsg)
3043 +{
3044 + struct fsg_lun *curlun = fsg->curlun;
3045 + int prevent;
3046 +
3047 + if (!mod_data.removable) {
3048 + curlun->sense_data = SS_INVALID_COMMAND;
3049 + return -EINVAL;
3050 + }
3051 +
3052 + prevent = fsg->cmnd[4] & 0x01;
3053 + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
3054 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3055 + return -EINVAL;
3056 + }
3057 +
3058 + if (curlun->prevent_medium_removal && !prevent)
3059 + fsg_lun_fsync_sub(curlun);
3060 + curlun->prevent_medium_removal = prevent;
3061 + return 0;
3062 +}
3063 +
3064 +
3065 +static int do_read_format_capacities(struct fsg_dev *fsg,
3066 + struct fsg_buffhd *bh)
3067 +{
3068 + struct fsg_lun *curlun = fsg->curlun;
3069 + u8 *buf = (u8 *) bh->buf;
3070 +
3071 + buf[0] = buf[1] = buf[2] = 0;
3072 + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
3073 + buf += 4;
3074 +
3075 + put_unaligned_be32(curlun->num_sectors, &buf[0]);
3076 + /* Number of blocks */
3077 + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
3078 + buf[4] = 0x02; /* Current capacity */
3079 + return 12;
3080 +}
3081 +
3082 +
3083 +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3084 +{
3085 + struct fsg_lun *curlun = fsg->curlun;
3086 +
3087 + /* We don't support MODE SELECT */
3088 + curlun->sense_data = SS_INVALID_COMMAND;
3089 + return -EINVAL;
3090 +}
3091 +
3092 +
3093 +/*-------------------------------------------------------------------------*/
3094 +
3095 +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
3096 +{
3097 + int rc;
3098 +
3099 + rc = fsg_set_halt(fsg, fsg->bulk_in);
3100 + if (rc == -EAGAIN)
3101 + VDBG(fsg, "delayed bulk-in endpoint halt\n");
3102 + while (rc != 0) {
3103 + if (rc != -EAGAIN) {
3104 + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
3105 + rc = 0;
3106 + break;
3107 + }
3108 +
3109 + /* Wait for a short time and then try again */
3110 + if (msleep_interruptible(100) != 0)
3111 + return -EINTR;
3112 + rc = usb_ep_set_halt(fsg->bulk_in);
3113 + }
3114 + return rc;
3115 +}
3116 +
3117 +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
3118 +{
3119 + int rc;
3120 +
3121 + DBG(fsg, "bulk-in set wedge\n");
3122 + rc = usb_ep_set_wedge(fsg->bulk_in);
3123 + if (rc == -EAGAIN)
3124 + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
3125 + while (rc != 0) {
3126 + if (rc != -EAGAIN) {
3127 + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
3128 + rc = 0;
3129 + break;
3130 + }
3131 +
3132 + /* Wait for a short time and then try again */
3133 + if (msleep_interruptible(100) != 0)
3134 + return -EINTR;
3135 + rc = usb_ep_set_wedge(fsg->bulk_in);
3136 + }
3137 + return rc;
3138 +}
3139 +
3140 +static int throw_away_data(struct fsg_dev *fsg)
3141 +{
3142 + struct fsg_buffhd *bh;
3143 + u32 amount;
3144 + int rc;
3145 +
3146 + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
3147 + fsg->usb_amount_left > 0) {
3148 +
3149 + /* Throw away the data in a filled buffer */
3150 + if (bh->state == BUF_STATE_FULL) {
3151 + smp_rmb();
3152 + bh->state = BUF_STATE_EMPTY;
3153 + fsg->next_buffhd_to_drain = bh->next;
3154 +
3155 + /* A short packet or an error ends everything */
3156 + if (bh->outreq->actual < bh->bulk_out_intended_length ||
3157 + bh->outreq->status != 0) {
3158 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3159 + return -EINTR;
3160 + }
3161 + continue;
3162 + }
3163 +
3164 + /* Try to submit another request if we need one */
3165 + bh = fsg->next_buffhd_to_fill;
3166 + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
3167 + amount = min(fsg->usb_amount_left,
3168 + (u32) mod_data.buflen);
3169 +
3170 + /* Except at the end of the transfer, amount will be
3171 + * equal to the buffer size, which is divisible by
3172 + * the bulk-out maxpacket size.
3173 + */
3174 + set_bulk_out_req_length(fsg, bh, amount);
3175 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
3176 + &bh->outreq_busy, &bh->state);
3177 + fsg->next_buffhd_to_fill = bh->next;
3178 + fsg->usb_amount_left -= amount;
3179 + continue;
3180 + }
3181 +
3182 + /* Otherwise wait for something to happen */
3183 + rc = sleep_thread(fsg);
3184 + if (rc)
3185 + return rc;
3186 + }
3187 + return 0;
3188 +}
3189 +
3190 +
3191 +static int finish_reply(struct fsg_dev *fsg)
3192 +{
3193 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
3194 + int rc = 0;
3195 +
3196 + switch (fsg->data_dir) {
3197 + case DATA_DIR_NONE:
3198 + break; // Nothing to send
3199 +
3200 + /* If we don't know whether the host wants to read or write,
3201 + * this must be CB or CBI with an unknown command. We mustn't
3202 + * try to send or receive any data. So stall both bulk pipes
3203 + * if we can and wait for a reset. */
3204 + case DATA_DIR_UNKNOWN:
3205 + if (mod_data.can_stall) {
3206 + fsg_set_halt(fsg, fsg->bulk_out);
3207 + rc = halt_bulk_in_endpoint(fsg);
3208 + }
3209 + break;
3210 +
3211 + /* All but the last buffer of data must have already been sent */
3212 + case DATA_DIR_TO_HOST:
3213 + if (fsg->data_size == 0)
3214 + ; // Nothing to send
3215 +
3216 + /* If there's no residue, simply send the last buffer */
3217 + else if (fsg->residue == 0) {
3218 + bh->inreq->zero = 0;
3219 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3220 + &bh->inreq_busy, &bh->state);
3221 + fsg->next_buffhd_to_fill = bh->next;
3222 + }
3223 +
3224 + /* There is a residue. For CB and CBI, simply mark the end
3225 + * of the data with a short packet. However, if we are
3226 + * allowed to stall, there was no data at all (residue ==
3227 + * data_size), and the command failed (invalid LUN or
3228 + * sense data is set), then halt the bulk-in endpoint
3229 + * instead. */
3230 + else if (!transport_is_bbb()) {
3231 + if (mod_data.can_stall &&
3232 + fsg->residue == fsg->data_size &&
3233 + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
3234 + bh->state = BUF_STATE_EMPTY;
3235 + rc = halt_bulk_in_endpoint(fsg);
3236 + } else {
3237 + bh->inreq->zero = 1;
3238 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3239 + &bh->inreq_busy, &bh->state);
3240 + fsg->next_buffhd_to_fill = bh->next;
3241 + }
3242 + }
3243 +
3244 + /*
3245 + * For Bulk-only, mark the end of the data with a short
3246 + * packet. If we are allowed to stall, halt the bulk-in
3247 + * endpoint. (Note: This violates the Bulk-Only Transport
3248 + * specification, which requires us to pad the data if we
3249 + * don't halt the endpoint. Presumably nobody will mind.)
3250 + */
3251 + else {
3252 + bh->inreq->zero = 1;
3253 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3254 + &bh->inreq_busy, &bh->state);
3255 + fsg->next_buffhd_to_fill = bh->next;
3256 + if (mod_data.can_stall)
3257 + rc = halt_bulk_in_endpoint(fsg);
3258 + }
3259 + break;
3260 +
3261 + /* We have processed all we want from the data the host has sent.
3262 + * There may still be outstanding bulk-out requests. */
3263 + case DATA_DIR_FROM_HOST:
3264 + if (fsg->residue == 0)
3265 + ; // Nothing to receive
3266 +
3267 + /* Did the host stop sending unexpectedly early? */
3268 + else if (fsg->short_packet_received) {
3269 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3270 + rc = -EINTR;
3271 + }
3272 +
3273 + /* We haven't processed all the incoming data. Even though
3274 + * we may be allowed to stall, doing so would cause a race.
3275 + * The controller may already have ACK'ed all the remaining
3276 + * bulk-out packets, in which case the host wouldn't see a
3277 + * STALL. Not realizing the endpoint was halted, it wouldn't
3278 + * clear the halt -- leading to problems later on. */
3279 +#if 0
3280 + else if (mod_data.can_stall) {
3281 + fsg_set_halt(fsg, fsg->bulk_out);
3282 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3283 + rc = -EINTR;
3284 + }
3285 +#endif
3286 +
3287 + /* We can't stall. Read in the excess data and throw it
3288 + * all away. */
3289 + else
3290 + rc = throw_away_data(fsg);
3291 + break;
3292 + }
3293 + return rc;
3294 +}
3295 +
3296 +
3297 +static int send_status(struct fsg_dev *fsg)
3298 +{
3299 + struct fsg_lun *curlun = fsg->curlun;
3300 + struct fsg_buffhd *bh;
3301 + int rc;
3302 + u8 status = US_BULK_STAT_OK;
3303 + u32 sd, sdinfo = 0;
3304 +
3305 + /* Wait for the next buffer to become available */
3306 + bh = fsg->next_buffhd_to_fill;
3307 + while (bh->state != BUF_STATE_EMPTY) {
3308 + rc = sleep_thread(fsg);
3309 + if (rc)
3310 + return rc;
3311 + }
3312 +
3313 + if (curlun) {
3314 + sd = curlun->sense_data;
3315 + sdinfo = curlun->sense_data_info;
3316 + } else if (fsg->bad_lun_okay)
3317 + sd = SS_NO_SENSE;
3318 + else
3319 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
3320 +
3321 + if (fsg->phase_error) {
3322 + DBG(fsg, "sending phase-error status\n");
3323 + status = US_BULK_STAT_PHASE;
3324 + sd = SS_INVALID_COMMAND;
3325 + } else if (sd != SS_NO_SENSE) {
3326 + DBG(fsg, "sending command-failure status\n");
3327 + status = US_BULK_STAT_FAIL;
3328 + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
3329 + " info x%x\n",
3330 + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
3331 + }
3332 +
3333 + if (transport_is_bbb()) {
3334 + struct bulk_cs_wrap *csw = bh->buf;
3335 +
3336 + /* Store and send the Bulk-only CSW */
3337 + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
3338 + csw->Tag = fsg->tag;
3339 + csw->Residue = cpu_to_le32(fsg->residue);
3340 + csw->Status = status;
3341 +
3342 + bh->inreq->length = US_BULK_CS_WRAP_LEN;
3343 + bh->inreq->zero = 0;
3344 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3345 + &bh->inreq_busy, &bh->state);
3346 +
3347 + } else if (mod_data.transport_type == USB_PR_CB) {
3348 +
3349 + /* Control-Bulk transport has no status phase! */
3350 + return 0;
3351 +
3352 + } else { // USB_PR_CBI
3353 + struct interrupt_data *buf = bh->buf;
3354 +
3355 + /* Store and send the Interrupt data. UFI sends the ASC
3356 + * and ASCQ bytes. Everything else sends a Type (which
3357 + * is always 0) and the status Value. */
3358 + if (mod_data.protocol_type == USB_SC_UFI) {
3359 + buf->bType = ASC(sd);
3360 + buf->bValue = ASCQ(sd);
3361 + } else {
3362 + buf->bType = 0;
3363 + buf->bValue = status;
3364 + }
3365 + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
3366 +
3367 + fsg->intr_buffhd = bh; // Point to the right buffhd
3368 + fsg->intreq->buf = bh->inreq->buf;
3369 + fsg->intreq->context = bh;
3370 + start_transfer(fsg, fsg->intr_in, fsg->intreq,
3371 + &fsg->intreq_busy, &bh->state);
3372 + }
3373 +
3374 + fsg->next_buffhd_to_fill = bh->next;
3375 + return 0;
3376 +}
3377 +
3378 +
3379 +/*-------------------------------------------------------------------------*/
3380 +
3381 +/* Check whether the command is properly formed and whether its data size
3382 + * and direction agree with the values we already have. */
3383 +static int check_command(struct fsg_dev *fsg, int cmnd_size,
3384 + enum data_direction data_dir, unsigned int mask,
3385 + int needs_medium, const char *name)
3386 +{
3387 + int i;
3388 + int lun = fsg->cmnd[1] >> 5;
3389 + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
3390 + char hdlen[20];
3391 + struct fsg_lun *curlun;
3392 +
3393 + /* Adjust the expected cmnd_size for protocol encapsulation padding.
3394 + * Transparent SCSI doesn't pad. */
3395 + if (protocol_is_scsi())
3396 + ;
3397 +
3398 + /* There's some disagreement as to whether RBC pads commands or not.
3399 + * We'll play it safe and accept either form. */
3400 + else if (mod_data.protocol_type == USB_SC_RBC) {
3401 + if (fsg->cmnd_size == 12)
3402 + cmnd_size = 12;
3403 +
3404 + /* All the other protocols pad to 12 bytes */
3405 + } else
3406 + cmnd_size = 12;
3407 +
3408 + hdlen[0] = 0;
3409 + if (fsg->data_dir != DATA_DIR_UNKNOWN)
3410 + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
3411 + fsg->data_size);
3412 + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
3413 + name, cmnd_size, dirletter[(int) data_dir],
3414 + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
3415 +
3416 + /* We can't reply at all until we know the correct data direction
3417 + * and size. */
3418 + if (fsg->data_size_from_cmnd == 0)
3419 + data_dir = DATA_DIR_NONE;
3420 + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
3421 + fsg->data_dir = data_dir;
3422 + fsg->data_size = fsg->data_size_from_cmnd;
3423 +
3424 + } else { // Bulk-only
3425 + if (fsg->data_size < fsg->data_size_from_cmnd) {
3426 +
3427 + /* Host data size < Device data size is a phase error.
3428 + * Carry out the command, but only transfer as much
3429 + * as we are allowed. */
3430 + fsg->data_size_from_cmnd = fsg->data_size;
3431 + fsg->phase_error = 1;
3432 + }
3433 + }
3434 + fsg->residue = fsg->usb_amount_left = fsg->data_size;
3435 +
3436 + /* Conflicting data directions is a phase error */
3437 + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
3438 + fsg->phase_error = 1;
3439 + return -EINVAL;
3440 + }
3441 +
3442 + /* Verify the length of the command itself */
3443 + if (cmnd_size != fsg->cmnd_size) {
3444 +
3445 + /* Special case workaround: There are plenty of buggy SCSI
3446 + * implementations. Many have issues with cbw->Length
3447 + * field passing a wrong command size. For those cases we
3448 + * always try to work around the problem by using the length
3449 + * sent by the host side provided it is at least as large
3450 + * as the correct command length.
3451 + * Examples of such cases would be MS-Windows, which issues
3452 + * REQUEST SENSE with cbw->Length == 12 where it should
3453 + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
3454 + * REQUEST SENSE with cbw->Length == 10 where it should
3455 + * be 6 as well.
3456 + */
3457 + if (cmnd_size <= fsg->cmnd_size) {
3458 + DBG(fsg, "%s is buggy! Expected length %d "
3459 + "but we got %d\n", name,
3460 + cmnd_size, fsg->cmnd_size);
3461 + cmnd_size = fsg->cmnd_size;
3462 + } else {
3463 + fsg->phase_error = 1;
3464 + return -EINVAL;
3465 + }
3466 + }
3467 +
3468 + /* Check that the LUN values are consistent */
3469 + if (transport_is_bbb()) {
3470 + if (fsg->lun != lun)
3471 + DBG(fsg, "using LUN %d from CBW, "
3472 + "not LUN %d from CDB\n",
3473 + fsg->lun, lun);
3474 + }
3475 +
3476 + /* Check the LUN */
3477 + curlun = fsg->curlun;
3478 + if (curlun) {
3479 + if (fsg->cmnd[0] != REQUEST_SENSE) {
3480 + curlun->sense_data = SS_NO_SENSE;
3481 + curlun->sense_data_info = 0;
3482 + curlun->info_valid = 0;
3483 + }
3484 + } else {
3485 + fsg->bad_lun_okay = 0;
3486 +
3487 + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
3488 + * to use unsupported LUNs; all others may not. */
3489 + if (fsg->cmnd[0] != INQUIRY &&
3490 + fsg->cmnd[0] != REQUEST_SENSE) {
3491 + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
3492 + return -EINVAL;
3493 + }
3494 + }
3495 +
3496 + /* If a unit attention condition exists, only INQUIRY and
3497 + * REQUEST SENSE commands are allowed; anything else must fail. */
3498 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
3499 + fsg->cmnd[0] != INQUIRY &&
3500 + fsg->cmnd[0] != REQUEST_SENSE) {
3501 + curlun->sense_data = curlun->unit_attention_data;
3502 + curlun->unit_attention_data = SS_NO_SENSE;
3503 + return -EINVAL;
3504 + }
3505 +
3506 + /* Check that only command bytes listed in the mask are non-zero */
3507 + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
3508 + for (i = 1; i < cmnd_size; ++i) {
3509 + if (fsg->cmnd[i] && !(mask & (1 << i))) {
3510 + if (curlun)
3511 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3512 + return -EINVAL;
3513 + }
3514 + }
3515 +
3516 + /* If the medium isn't mounted and the command needs to access
3517 + * it, return an error. */
3518 + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
3519 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
3520 + return -EINVAL;
3521 + }
3522 +
3523 + return 0;
3524 +}
3525 +
3526 +/* wrapper of check_command for data size in blocks handling */
3527 +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
3528 + enum data_direction data_dir, unsigned int mask,
3529 + int needs_medium, const char *name)
3530 +{
3531 + if (fsg->curlun)
3532 + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
3533 + return check_command(fsg, cmnd_size, data_dir,
3534 + mask, needs_medium, name);
3535 +}
3536 +
3537 +static int do_scsi_command(struct fsg_dev *fsg)
3538 +{
3539 + struct fsg_buffhd *bh;
3540 + int rc;
3541 + int reply = -EINVAL;
3542 + int i;
3543 + static char unknown[16];
3544 +
3545 + dump_cdb(fsg);
3546 +
3547 + /* Wait for the next buffer to become available for data or status */
3548 + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
3549 + while (bh->state != BUF_STATE_EMPTY) {
3550 + rc = sleep_thread(fsg);
3551 + if (rc)
3552 + return rc;
3553 + }
3554 + fsg->phase_error = 0;
3555 + fsg->short_packet_received = 0;
3556 +
3557 + down_read(&fsg->filesem); // We're using the backing file
3558 + switch (fsg->cmnd[0]) {
3559 +
3560 + case INQUIRY:
3561 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3562 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3563 + (1<<4), 0,
3564 + "INQUIRY")) == 0)
3565 + reply = do_inquiry(fsg, bh);
3566 + break;
3567 +
3568 + case MODE_SELECT:
3569 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3570 + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
3571 + (1<<1) | (1<<4), 0,
3572 + "MODE SELECT(6)")) == 0)
3573 + reply = do_mode_select(fsg, bh);
3574 + break;
3575 +
3576 + case MODE_SELECT_10:
3577 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3578 + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
3579 + (1<<1) | (3<<7), 0,
3580 + "MODE SELECT(10)")) == 0)
3581 + reply = do_mode_select(fsg, bh);
3582 + break;
3583 +
3584 + case MODE_SENSE:
3585 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3586 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3587 + (1<<1) | (1<<2) | (1<<4), 0,
3588 + "MODE SENSE(6)")) == 0)
3589 + reply = do_mode_sense(fsg, bh);
3590 + break;
3591 +
3592 + case MODE_SENSE_10:
3593 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3594 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3595 + (1<<1) | (1<<2) | (3<<7), 0,
3596 + "MODE SENSE(10)")) == 0)
3597 + reply = do_mode_sense(fsg, bh);
3598 + break;
3599 +
3600 + case ALLOW_MEDIUM_REMOVAL:
3601 + fsg->data_size_from_cmnd = 0;
3602 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
3603 + (1<<4), 0,
3604 + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
3605 + reply = do_prevent_allow(fsg);
3606 + break;
3607 +
3608 + case READ_6:
3609 + i = fsg->cmnd[4];
3610 + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
3611 + if ((reply = check_command_size_in_blocks(fsg, 6,
3612 + DATA_DIR_TO_HOST,
3613 + (7<<1) | (1<<4), 1,
3614 + "READ(6)")) == 0)
3615 + reply = do_read(fsg);
3616 + break;
3617 +
3618 + case READ_10:
3619 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3620 + if ((reply = check_command_size_in_blocks(fsg, 10,
3621 + DATA_DIR_TO_HOST,
3622 + (1<<1) | (0xf<<2) | (3<<7), 1,
3623 + "READ(10)")) == 0)
3624 + reply = do_read(fsg);
3625 + break;
3626 +
3627 + case READ_12:
3628 + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
3629 + if ((reply = check_command_size_in_blocks(fsg, 12,
3630 + DATA_DIR_TO_HOST,
3631 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
3632 + "READ(12)")) == 0)
3633 + reply = do_read(fsg);
3634 + break;
3635 +
3636 + case READ_CAPACITY:
3637 + fsg->data_size_from_cmnd = 8;
3638 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3639 + (0xf<<2) | (1<<8), 1,
3640 + "READ CAPACITY")) == 0)
3641 + reply = do_read_capacity(fsg, bh);
3642 + break;
3643 +
3644 + case READ_HEADER:
3645 + if (!mod_data.cdrom)
3646 + goto unknown_cmnd;
3647 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3648 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3649 + (3<<7) | (0x1f<<1), 1,
3650 + "READ HEADER")) == 0)
3651 + reply = do_read_header(fsg, bh);
3652 + break;
3653 +
3654 + case READ_TOC:
3655 + if (!mod_data.cdrom)
3656 + goto unknown_cmnd;
3657 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3658 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3659 + (7<<6) | (1<<1), 1,
3660 + "READ TOC")) == 0)
3661 + reply = do_read_toc(fsg, bh);
3662 + break;
3663 +
3664 + case READ_FORMAT_CAPACITIES:
3665 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3666 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3667 + (3<<7), 1,
3668 + "READ FORMAT CAPACITIES")) == 0)
3669 + reply = do_read_format_capacities(fsg, bh);
3670 + break;
3671 +
3672 + case REQUEST_SENSE:
3673 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3674 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3675 + (1<<4), 0,
3676 + "REQUEST SENSE")) == 0)
3677 + reply = do_request_sense(fsg, bh);
3678 + break;
3679 +
3680 + case START_STOP:
3681 + fsg->data_size_from_cmnd = 0;
3682 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
3683 + (1<<1) | (1<<4), 0,
3684 + "START-STOP UNIT")) == 0)
3685 + reply = do_start_stop(fsg);
3686 + break;
3687 +
3688 + case SYNCHRONIZE_CACHE:
3689 + fsg->data_size_from_cmnd = 0;
3690 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3691 + (0xf<<2) | (3<<7), 1,
3692 + "SYNCHRONIZE CACHE")) == 0)
3693 + reply = do_synchronize_cache(fsg);
3694 + break;
3695 +
3696 + case TEST_UNIT_READY:
3697 + fsg->data_size_from_cmnd = 0;
3698 + reply = check_command(fsg, 6, DATA_DIR_NONE,
3699 + 0, 1,
3700 + "TEST UNIT READY");
3701 + break;
3702 +
3703 + /* Although optional, this command is used by MS-Windows. We
3704 + * support a minimal version: BytChk must be 0. */
3705 + case VERIFY:
3706 + fsg->data_size_from_cmnd = 0;
3707 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3708 + (1<<1) | (0xf<<2) | (3<<7), 1,
3709 + "VERIFY")) == 0)
3710 + reply = do_verify(fsg);
3711 + break;
3712 +
3713 + case WRITE_6:
3714 + i = fsg->cmnd[4];
3715 + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
3716 + if ((reply = check_command_size_in_blocks(fsg, 6,
3717 + DATA_DIR_FROM_HOST,
3718 + (7<<1) | (1<<4), 1,
3719 + "WRITE(6)")) == 0)
3720 + reply = do_write(fsg);
3721 + break;
3722 +
3723 + case WRITE_10:
3724 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3725 + if ((reply = check_command_size_in_blocks(fsg, 10,
3726 + DATA_DIR_FROM_HOST,
3727 + (1<<1) | (0xf<<2) | (3<<7), 1,
3728 + "WRITE(10)")) == 0)
3729 + reply = do_write(fsg);
3730 + break;
3731 +
3732 + case WRITE_12:
3733 + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
3734 + if ((reply = check_command_size_in_blocks(fsg, 12,
3735 + DATA_DIR_FROM_HOST,
3736 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
3737 + "WRITE(12)")) == 0)
3738 + reply = do_write(fsg);
3739 + break;
3740 +
3741 + /* Some mandatory commands that we recognize but don't implement.
3742 + * They don't mean much in this setting. It's left as an exercise
3743 + * for anyone interested to implement RESERVE and RELEASE in terms
3744 + * of Posix locks. */
3745 + case FORMAT_UNIT:
3746 + case RELEASE:
3747 + case RESERVE:
3748 + case SEND_DIAGNOSTIC:
3749 + // Fall through
3750 +
3751 + default:
3752 + unknown_cmnd:
3753 + fsg->data_size_from_cmnd = 0;
3754 + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
3755 + if ((reply = check_command(fsg, fsg->cmnd_size,
3756 + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
3757 + fsg->curlun->sense_data = SS_INVALID_COMMAND;
3758 + reply = -EINVAL;
3759 + }
3760 + break;
3761 + }
3762 + up_read(&fsg->filesem);
3763 +
3764 + if (reply == -EINTR || signal_pending(current))
3765 + return -EINTR;
3766 +
3767 + /* Set up the single reply buffer for finish_reply() */
3768 + if (reply == -EINVAL)
3769 + reply = 0; // Error reply length
3770 + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
3771 + reply = min((u32) reply, fsg->data_size_from_cmnd);
3772 + bh->inreq->length = reply;
3773 + bh->state = BUF_STATE_FULL;
3774 + fsg->residue -= reply;
3775 + } // Otherwise it's already set
3776 +
3777 + return 0;
3778 +}
3779 +
3780 +
3781 +/*-------------------------------------------------------------------------*/
3782 +
3783 +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3784 +{
3785 + struct usb_request *req = bh->outreq;
3786 + struct bulk_cb_wrap *cbw = req->buf;
3787 +
3788 + /* Was this a real packet? Should it be ignored? */
3789 + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
3790 + return -EINVAL;
3791 +
3792 + /* Is the CBW valid? */
3793 + if (req->actual != US_BULK_CB_WRAP_LEN ||
3794 + cbw->Signature != cpu_to_le32(
3795 + US_BULK_CB_SIGN)) {
3796 + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
3797 + req->actual,
3798 + le32_to_cpu(cbw->Signature));
3799 +
3800 + /* The Bulk-only spec says we MUST stall the IN endpoint
3801 + * (6.6.1), so it's unavoidable. It also says we must
3802 + * retain this state until the next reset, but there's
3803 + * no way to tell the controller driver it should ignore
3804 + * Clear-Feature(HALT) requests.
3805 + *
3806 + * We aren't required to halt the OUT endpoint; instead
3807 + * we can simply accept and discard any data received
3808 + * until the next reset. */
3809 + wedge_bulk_in_endpoint(fsg);
3810 + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
3811 + return -EINVAL;
3812 + }
3813 +
3814 + /* Is the CBW meaningful? */
3815 + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
3816 + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
3817 + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
3818 + "cmdlen %u\n",
3819 + cbw->Lun, cbw->Flags, cbw->Length);
3820 +
3821 + /* We can do anything we want here, so let's stall the
3822 + * bulk pipes if we are allowed to. */
3823 + if (mod_data.can_stall) {
3824 + fsg_set_halt(fsg, fsg->bulk_out);
3825 + halt_bulk_in_endpoint(fsg);
3826 + }
3827 + return -EINVAL;
3828 + }
3829 +
3830 + /* Save the command for later */
3831 + fsg->cmnd_size = cbw->Length;
3832 + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
3833 + if (cbw->Flags & US_BULK_FLAG_IN)
3834 + fsg->data_dir = DATA_DIR_TO_HOST;
3835 + else
3836 + fsg->data_dir = DATA_DIR_FROM_HOST;
3837 + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
3838 + if (fsg->data_size == 0)
3839 + fsg->data_dir = DATA_DIR_NONE;
3840 + fsg->lun = cbw->Lun;
3841 + fsg->tag = cbw->Tag;
3842 + return 0;
3843 +}
3844 +
3845 +
3846 +static int get_next_command(struct fsg_dev *fsg)
3847 +{
3848 + struct fsg_buffhd *bh;
3849 + int rc = 0;
3850 +
3851 + if (transport_is_bbb()) {
3852 +
3853 + /* Wait for the next buffer to become available */
3854 + bh = fsg->next_buffhd_to_fill;
3855 + while (bh->state != BUF_STATE_EMPTY) {
3856 + rc = sleep_thread(fsg);
3857 + if (rc)
3858 + return rc;
3859 + }
3860 +
3861 + /* Queue a request to read a Bulk-only CBW */
3862 + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
3863 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
3864 + &bh->outreq_busy, &bh->state);
3865 +
3866 + /* We will drain the buffer in software, which means we
3867 + * can reuse it for the next filling. No need to advance
3868 + * next_buffhd_to_fill. */
3869 +
3870 + /* Wait for the CBW to arrive */
3871 + while (bh->state != BUF_STATE_FULL) {
3872 + rc = sleep_thread(fsg);
3873 + if (rc)
3874 + return rc;
3875 + }
3876 + smp_rmb();
3877 + rc = received_cbw(fsg, bh);
3878 + bh->state = BUF_STATE_EMPTY;
3879 +
3880 + } else { // USB_PR_CB or USB_PR_CBI
3881 +
3882 + /* Wait for the next command to arrive */
3883 + while (fsg->cbbuf_cmnd_size == 0) {
3884 + rc = sleep_thread(fsg);
3885 + if (rc)
3886 + return rc;
3887 + }
3888 +
3889 + /* Is the previous status interrupt request still busy?
3890 + * The host is allowed to skip reading the status,
3891 + * so we must cancel it. */
3892 + if (fsg->intreq_busy)
3893 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
3894 +
3895 + /* Copy the command and mark the buffer empty */
3896 + fsg->data_dir = DATA_DIR_UNKNOWN;
3897 + spin_lock_irq(&fsg->lock);
3898 + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
3899 + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
3900 + fsg->cbbuf_cmnd_size = 0;
3901 + spin_unlock_irq(&fsg->lock);
3902 +
3903 + /* Use LUN from the command */
3904 + fsg->lun = fsg->cmnd[1] >> 5;
3905 + }
3906 +
3907 + /* Update current lun */
3908 + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
3909 + fsg->curlun = &fsg->luns[fsg->lun];
3910 + else
3911 + fsg->curlun = NULL;
3912 +
3913 + return rc;
3914 +}
3915 +
3916 +
3917 +/*-------------------------------------------------------------------------*/
3918 +
3919 +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
3920 + const struct usb_endpoint_descriptor *d)
3921 +{
3922 + int rc;
3923 +
3924 + ep->driver_data = fsg;
3925 + ep->desc = d;
3926 + rc = usb_ep_enable(ep);
3927 + if (rc)
3928 + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
3929 + return rc;
3930 +}
3931 +
3932 +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
3933 + struct usb_request **preq)
3934 +{
3935 + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
3936 + if (*preq)
3937 + return 0;
3938 + ERROR(fsg, "can't allocate request for %s\n", ep->name);
3939 + return -ENOMEM;
3940 +}
3941 +
3942 +/*
3943 + * Reset interface setting and re-init endpoint state (toggle etc).
3944 + * Call with altsetting < 0 to disable the interface. The only other
3945 + * available altsetting is 0, which enables the interface.
3946 + */
3947 +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
3948 +{
3949 + int rc = 0;
3950 + int i;
3951 + const struct usb_endpoint_descriptor *d;
3952 +
3953 + if (fsg->running)
3954 + DBG(fsg, "reset interface\n");
3955 +
3956 +reset:
3957 + /* Deallocate the requests */
3958 + for (i = 0; i < fsg_num_buffers; ++i) {
3959 + struct fsg_buffhd *bh = &fsg->buffhds[i];
3960 +
3961 + if (bh->inreq) {
3962 + usb_ep_free_request(fsg->bulk_in, bh->inreq);
3963 + bh->inreq = NULL;
3964 + }
3965 + if (bh->outreq) {
3966 + usb_ep_free_request(fsg->bulk_out, bh->outreq);
3967 + bh->outreq = NULL;
3968 + }
3969 + }
3970 + if (fsg->intreq) {
3971 + usb_ep_free_request(fsg->intr_in, fsg->intreq);
3972 + fsg->intreq = NULL;
3973 + }
3974 +
3975 + /* Disable the endpoints */
3976 + if (fsg->bulk_in_enabled) {
3977 + usb_ep_disable(fsg->bulk_in);
3978 + fsg->bulk_in_enabled = 0;
3979 + }
3980 + if (fsg->bulk_out_enabled) {
3981 + usb_ep_disable(fsg->bulk_out);
3982 + fsg->bulk_out_enabled = 0;
3983 + }
3984 + if (fsg->intr_in_enabled) {
3985 + usb_ep_disable(fsg->intr_in);
3986 + fsg->intr_in_enabled = 0;
3987 + }
3988 +
3989 + fsg->running = 0;
3990 + if (altsetting < 0 || rc != 0)
3991 + return rc;
3992 +
3993 + DBG(fsg, "set interface %d\n", altsetting);
3994 +
3995 + /* Enable the endpoints */
3996 + d = fsg_ep_desc(fsg->gadget,
3997 + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
3998 + &fsg_ss_bulk_in_desc);
3999 + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
4000 + goto reset;
4001 + fsg->bulk_in_enabled = 1;
4002 +
4003 + d = fsg_ep_desc(fsg->gadget,
4004 + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
4005 + &fsg_ss_bulk_out_desc);
4006 + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
4007 + goto reset;
4008 + fsg->bulk_out_enabled = 1;
4009 + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
4010 + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
4011 +
4012 + if (transport_is_cbi()) {
4013 + d = fsg_ep_desc(fsg->gadget,
4014 + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
4015 + &fsg_ss_intr_in_desc);
4016 + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
4017 + goto reset;
4018 + fsg->intr_in_enabled = 1;
4019 + }
4020 +
4021 + /* Allocate the requests */
4022 + for (i = 0; i < fsg_num_buffers; ++i) {
4023 + struct fsg_buffhd *bh = &fsg->buffhds[i];
4024 +
4025 + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
4026 + goto reset;
4027 + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
4028 + goto reset;
4029 + bh->inreq->buf = bh->outreq->buf = bh->buf;
4030 + bh->inreq->context = bh->outreq->context = bh;
4031 + bh->inreq->complete = bulk_in_complete;
4032 + bh->outreq->complete = bulk_out_complete;
4033 + }
4034 + if (transport_is_cbi()) {
4035 + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
4036 + goto reset;
4037 + fsg->intreq->complete = intr_in_complete;
4038 + }
4039 +
4040 + fsg->running = 1;
4041 + for (i = 0; i < fsg->nluns; ++i)
4042 + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
4043 + return rc;
4044 +}
4045 +
4046 +
4047 +/*
4048 + * Change our operational configuration. This code must agree with the code
4049 + * that returns config descriptors, and with interface altsetting code.
4050 + *
4051 + * It's also responsible for power management interactions. Some
4052 + * configurations might not work with our current power sources.
4053 + * For now we just assume the gadget is always self-powered.
4054 + */
4055 +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
4056 +{
4057 + int rc = 0;
4058 +
4059 + /* Disable the single interface */
4060 + if (fsg->config != 0) {
4061 + DBG(fsg, "reset config\n");
4062 + fsg->config = 0;
4063 + rc = do_set_interface(fsg, -1);
4064 + }
4065 +
4066 + /* Enable the interface */
4067 + if (new_config != 0) {
4068 + fsg->config = new_config;
4069 + if ((rc = do_set_interface(fsg, 0)) != 0)
4070 + fsg->config = 0; // Reset on errors
4071 + else
4072 + INFO(fsg, "%s config #%d\n",
4073 + usb_speed_string(fsg->gadget->speed),
4074 + fsg->config);
4075 + }
4076 + return rc;
4077 +}
4078 +
4079 +
4080 +/*-------------------------------------------------------------------------*/
4081 +
4082 +static void handle_exception(struct fsg_dev *fsg)
4083 +{
4084 + siginfo_t info;
4085 + int sig;
4086 + int i;
4087 + int num_active;
4088 + struct fsg_buffhd *bh;
4089 + enum fsg_state old_state;
4090 + u8 new_config;
4091 + struct fsg_lun *curlun;
4092 + unsigned int exception_req_tag;
4093 + int rc;
4094 +
4095 + /* Clear the existing signals. Anything but SIGUSR1 is converted
4096 + * into a high-priority EXIT exception. */
4097 + for (;;) {
4098 + sig = dequeue_signal_lock(current, &current->blocked, &info);
4099 + if (!sig)
4100 + break;
4101 + if (sig != SIGUSR1) {
4102 + if (fsg->state < FSG_STATE_EXIT)
4103 + DBG(fsg, "Main thread exiting on signal\n");
4104 + raise_exception(fsg, FSG_STATE_EXIT);
4105 + }
4106 + }
4107 +
4108 + /* Cancel all the pending transfers */
4109 + if (fsg->intreq_busy)
4110 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
4111 + for (i = 0; i < fsg_num_buffers; ++i) {
4112 + bh = &fsg->buffhds[i];
4113 + if (bh->inreq_busy)
4114 + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
4115 + if (bh->outreq_busy)
4116 + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
4117 + }
4118 +
4119 + /* Wait until everything is idle */
4120 + for (;;) {
4121 + num_active = fsg->intreq_busy;
4122 + for (i = 0; i < fsg_num_buffers; ++i) {
4123 + bh = &fsg->buffhds[i];
4124 + num_active += bh->inreq_busy + bh->outreq_busy;
4125 + }
4126 + if (num_active == 0)
4127 + break;
4128 + if (sleep_thread(fsg))
4129 + return;
4130 + }
4131 +
4132 + /* Clear out the controller's fifos */
4133 + if (fsg->bulk_in_enabled)
4134 + usb_ep_fifo_flush(fsg->bulk_in);
4135 + if (fsg->bulk_out_enabled)
4136 + usb_ep_fifo_flush(fsg->bulk_out);
4137 + if (fsg->intr_in_enabled)
4138 + usb_ep_fifo_flush(fsg->intr_in);
4139 +
4140 + /* Reset the I/O buffer states and pointers, the SCSI
4141 + * state, and the exception. Then invoke the handler. */
4142 + spin_lock_irq(&fsg->lock);
4143 +
4144 + for (i = 0; i < fsg_num_buffers; ++i) {
4145 + bh = &fsg->buffhds[i];
4146 + bh->state = BUF_STATE_EMPTY;
4147 + }
4148 + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
4149 + &fsg->buffhds[0];
4150 +
4151 + exception_req_tag = fsg->exception_req_tag;
4152 + new_config = fsg->new_config;
4153 + old_state = fsg->state;
4154 +
4155 + if (old_state == FSG_STATE_ABORT_BULK_OUT)
4156 + fsg->state = FSG_STATE_STATUS_PHASE;
4157 + else {
4158 + for (i = 0; i < fsg->nluns; ++i) {
4159 + curlun = &fsg->luns[i];
4160 + curlun->prevent_medium_removal = 0;
4161 + curlun->sense_data = curlun->unit_attention_data =
4162 + SS_NO_SENSE;
4163 + curlun->sense_data_info = 0;
4164 + curlun->info_valid = 0;
4165 + }
4166 + fsg->state = FSG_STATE_IDLE;
4167 + }
4168 + spin_unlock_irq(&fsg->lock);
4169 +
4170 + /* Carry out any extra actions required for the exception */
4171 + switch (old_state) {
4172 + default:
4173 + break;
4174 +
4175 + case FSG_STATE_ABORT_BULK_OUT:
4176 + send_status(fsg);
4177 + spin_lock_irq(&fsg->lock);
4178 + if (fsg->state == FSG_STATE_STATUS_PHASE)
4179 + fsg->state = FSG_STATE_IDLE;
4180 + spin_unlock_irq(&fsg->lock);
4181 + break;
4182 +
4183 + case FSG_STATE_RESET:
4184 + /* In case we were forced against our will to halt a
4185 + * bulk endpoint, clear the halt now. (The SuperH UDC
4186 + * requires this.) */
4187 + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
4188 + usb_ep_clear_halt(fsg->bulk_in);
4189 +
4190 + if (transport_is_bbb()) {
4191 + if (fsg->ep0_req_tag == exception_req_tag)
4192 + ep0_queue(fsg); // Complete the status stage
4193 +
4194 + } else if (transport_is_cbi())
4195 + send_status(fsg); // Status by interrupt pipe
4196 +
4197 + /* Technically this should go here, but it would only be
4198 + * a waste of time. Ditto for the INTERFACE_CHANGE and
4199 + * CONFIG_CHANGE cases. */
4200 + // for (i = 0; i < fsg->nluns; ++i)
4201 + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
4202 + break;
4203 +
4204 + case FSG_STATE_INTERFACE_CHANGE:
4205 + rc = do_set_interface(fsg, 0);
4206 + if (fsg->ep0_req_tag != exception_req_tag)
4207 + break;
4208 + if (rc != 0) // STALL on errors
4209 + fsg_set_halt(fsg, fsg->ep0);
4210 + else // Complete the status stage
4211 + ep0_queue(fsg);
4212 + break;
4213 +
4214 + case FSG_STATE_CONFIG_CHANGE:
4215 + rc = do_set_config(fsg, new_config);
4216 + if (fsg->ep0_req_tag != exception_req_tag)
4217 + break;
4218 + if (rc != 0) // STALL on errors
4219 + fsg_set_halt(fsg, fsg->ep0);
4220 + else // Complete the status stage
4221 + ep0_queue(fsg);
4222 + break;
4223 +
4224 + case FSG_STATE_DISCONNECT:
4225 + for (i = 0; i < fsg->nluns; ++i)
4226 + fsg_lun_fsync_sub(fsg->luns + i);
4227 + do_set_config(fsg, 0); // Unconfigured state
4228 + break;
4229 +
4230 + case FSG_STATE_EXIT:
4231 + case FSG_STATE_TERMINATED:
4232 + do_set_config(fsg, 0); // Free resources
4233 + spin_lock_irq(&fsg->lock);
4234 + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
4235 + spin_unlock_irq(&fsg->lock);
4236 + break;
4237 + }
4238 +}
4239 +
4240 +
4241 +/*-------------------------------------------------------------------------*/
4242 +
4243 +static int fsg_main_thread(void *fsg_)
4244 +{
4245 + struct fsg_dev *fsg = fsg_;
4246 +
4247 + /* Allow the thread to be killed by a signal, but set the signal mask
4248 + * to block everything but INT, TERM, KILL, and USR1. */
4249 + allow_signal(SIGINT);
4250 + allow_signal(SIGTERM);
4251 + allow_signal(SIGKILL);
4252 + allow_signal(SIGUSR1);
4253 +
4254 + /* Allow the thread to be frozen */
4255 + set_freezable();
4256 +
4257 + /* Arrange for userspace references to be interpreted as kernel
4258 + * pointers. That way we can pass a kernel pointer to a routine
4259 + * that expects a __user pointer and it will work okay. */
4260 + set_fs(get_ds());
4261 +
4262 + /* The main loop */
4263 + while (fsg->state != FSG_STATE_TERMINATED) {
4264 + if (exception_in_progress(fsg) || signal_pending(current)) {
4265 + handle_exception(fsg);
4266 + continue;
4267 + }
4268 +
4269 + if (!fsg->running) {
4270 + sleep_thread(fsg);
4271 + continue;
4272 + }
4273 +
4274 + if (get_next_command(fsg))
4275 + continue;
4276 +
4277 + spin_lock_irq(&fsg->lock);
4278 + if (!exception_in_progress(fsg))
4279 + fsg->state = FSG_STATE_DATA_PHASE;
4280 + spin_unlock_irq(&fsg->lock);
4281 +
4282 + if (do_scsi_command(fsg) || finish_reply(fsg))
4283 + continue;
4284 +
4285 + spin_lock_irq(&fsg->lock);
4286 + if (!exception_in_progress(fsg))
4287 + fsg->state = FSG_STATE_STATUS_PHASE;
4288 + spin_unlock_irq(&fsg->lock);
4289 +
4290 + if (send_status(fsg))
4291 + continue;
4292 +
4293 + spin_lock_irq(&fsg->lock);
4294 + if (!exception_in_progress(fsg))
4295 + fsg->state = FSG_STATE_IDLE;
4296 + spin_unlock_irq(&fsg->lock);
4297 + }
4298 +
4299 + spin_lock_irq(&fsg->lock);
4300 + fsg->thread_task = NULL;
4301 + spin_unlock_irq(&fsg->lock);
4302 +
4303 + /* If we are exiting because of a signal, unregister the
4304 + * gadget driver. */
4305 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
4306 + usb_gadget_unregister_driver(&fsg_driver);
4307 +
4308 + /* Let the unbind and cleanup routines know the thread has exited */
4309 + complete_and_exit(&fsg->thread_notifier, 0);
4310 +}
4311 +
4312 +
4313 +/*-------------------------------------------------------------------------*/
4314 +
4315 +
4316 +/* The write permissions and store_xxx pointers are set in fsg_bind() */
4317 +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
4318 +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
4319 +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
4320 +
4321 +
4322 +/*-------------------------------------------------------------------------*/
4323 +
4324 +static void fsg_release(struct kref *ref)
4325 +{
4326 + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
4327 +
4328 + kfree(fsg->luns);
4329 + kfree(fsg);
4330 +}
4331 +
4332 +static void lun_release(struct device *dev)
4333 +{
4334 + struct rw_semaphore *filesem = dev_get_drvdata(dev);
4335 + struct fsg_dev *fsg =
4336 + container_of(filesem, struct fsg_dev, filesem);
4337 +
4338 + kref_put(&fsg->ref, fsg_release);
4339 +}
4340 +
4341 +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
4342 +{
4343 + struct fsg_dev *fsg = get_gadget_data(gadget);
4344 + int i;
4345 + struct fsg_lun *curlun;
4346 + struct usb_request *req = fsg->ep0req;
4347 +
4348 + DBG(fsg, "unbind\n");
4349 + clear_bit(REGISTERED, &fsg->atomic_bitflags);
4350 +
4351 + /* If the thread isn't already dead, tell it to exit now */
4352 + if (fsg->state != FSG_STATE_TERMINATED) {
4353 + raise_exception(fsg, FSG_STATE_EXIT);
4354 + wait_for_completion(&fsg->thread_notifier);
4355 +
4356 + /* The cleanup routine waits for this completion also */
4357 + complete(&fsg->thread_notifier);
4358 + }
4359 +
4360 + /* Unregister the sysfs attribute files and the LUNs */
4361 + for (i = 0; i < fsg->nluns; ++i) {
4362 + curlun = &fsg->luns[i];
4363 + if (curlun->registered) {
4364 + device_remove_file(&curlun->dev, &dev_attr_nofua);
4365 + device_remove_file(&curlun->dev, &dev_attr_ro);
4366 + device_remove_file(&curlun->dev, &dev_attr_file);
4367 + fsg_lun_close(curlun);
4368 + device_unregister(&curlun->dev);
4369 + curlun->registered = 0;
4370 + }
4371 + }
4372 +
4373 + /* Free the data buffers */
4374 + for (i = 0; i < fsg_num_buffers; ++i)
4375 + kfree(fsg->buffhds[i].buf);
4376 +
4377 + /* Free the request and buffer for endpoint 0 */
4378 + if (req) {
4379 + kfree(req->buf);
4380 + usb_ep_free_request(fsg->ep0, req);
4381 + }
4382 +
4383 + set_gadget_data(gadget, NULL);
4384 +}
4385 +
4386 +
4387 +static int __init check_parameters(struct fsg_dev *fsg)
4388 +{
4389 + int prot;
4390 + int gcnum;
4391 +
4392 + /* Store the default values */
4393 + mod_data.transport_type = USB_PR_BULK;
4394 + mod_data.transport_name = "Bulk-only";
4395 + mod_data.protocol_type = USB_SC_SCSI;
4396 + mod_data.protocol_name = "Transparent SCSI";
4397 +
4398 + /* Some peripheral controllers are known not to be able to
4399 + * halt bulk endpoints correctly. If one of them is present,
4400 + * disable stalls.
4401 + */
4402 + if (gadget_is_at91(fsg->gadget))
4403 + mod_data.can_stall = 0;
4404 +
4405 + if (mod_data.release == 0xffff) { // Parameter wasn't set
4406 + gcnum = usb_gadget_controller_number(fsg->gadget);
4407 + if (gcnum >= 0)
4408 + mod_data.release = 0x0300 + gcnum;
4409 + else {
4410 + WARNING(fsg, "controller '%s' not recognized\n",
4411 + fsg->gadget->name);
4412 + mod_data.release = 0x0399;
4413 + }
4414 + }
4415 +
4416 + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
4417 +
4418 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
4419 + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
4420 + ; // Use default setting
4421 + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
4422 + mod_data.transport_type = USB_PR_CB;
4423 + mod_data.transport_name = "Control-Bulk";
4424 + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
4425 + mod_data.transport_type = USB_PR_CBI;
4426 + mod_data.transport_name = "Control-Bulk-Interrupt";
4427 + } else {
4428 + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
4429 + return -EINVAL;
4430 + }
4431 +
4432 + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
4433 + prot == USB_SC_SCSI) {
4434 + ; // Use default setting
4435 + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
4436 + prot == USB_SC_RBC) {
4437 + mod_data.protocol_type = USB_SC_RBC;
4438 + mod_data.protocol_name = "RBC";
4439 + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
4440 + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
4441 + prot == USB_SC_8020) {
4442 + mod_data.protocol_type = USB_SC_8020;
4443 + mod_data.protocol_name = "8020i (ATAPI)";
4444 + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
4445 + prot == USB_SC_QIC) {
4446 + mod_data.protocol_type = USB_SC_QIC;
4447 + mod_data.protocol_name = "QIC-157";
4448 + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
4449 + prot == USB_SC_UFI) {
4450 + mod_data.protocol_type = USB_SC_UFI;
4451 + mod_data.protocol_name = "UFI";
4452 + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
4453 + prot == USB_SC_8070) {
4454 + mod_data.protocol_type = USB_SC_8070;
4455 + mod_data.protocol_name = "8070i";
4456 + } else {
4457 + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
4458 + return -EINVAL;
4459 + }
4460 +
4461 + mod_data.buflen &= PAGE_CACHE_MASK;
4462 + if (mod_data.buflen <= 0) {
4463 + ERROR(fsg, "invalid buflen\n");
4464 + return -ETOOSMALL;
4465 + }
4466 +
4467 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
4468 +
4469 + /* Serial string handling.
4470 + * On a real device, the serial string would be loaded
4471 + * from permanent storage. */
4472 + if (mod_data.serial) {
4473 + const char *ch;
4474 + unsigned len = 0;
4475 +
4476 + /* Sanity check :
4477 + * The CB[I] specification limits the serial string to
4478 + * 12 uppercase hexadecimal characters.
4479 + * BBB need at least 12 uppercase hexadecimal characters,
4480 + * with a maximum of 126. */
4481 + for (ch = mod_data.serial; *ch; ++ch) {
4482 + ++len;
4483 + if ((*ch < '0' || *ch > '9') &&
4484 + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
4485 + WARNING(fsg,
4486 + "Invalid serial string character: %c\n",
4487 + *ch);
4488 + goto no_serial;
4489 + }
4490 + }
4491 + if (len > 126 ||
4492 + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
4493 + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
4494 + WARNING(fsg, "Invalid serial string length!\n");
4495 + goto no_serial;
4496 + }
4497 + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
4498 + } else {
4499 + WARNING(fsg, "No serial-number string provided!\n");
4500 + no_serial:
4501 + device_desc.iSerialNumber = 0;
4502 + }
4503 +
4504 + return 0;
4505 +}
4506 +
4507 +
4508 +static int __init fsg_bind(struct usb_gadget *gadget)
4509 +{
4510 + struct fsg_dev *fsg = the_fsg;
4511 + int rc;
4512 + int i;
4513 + struct fsg_lun *curlun;
4514 + struct usb_ep *ep;
4515 + struct usb_request *req;
4516 + char *pathbuf, *p;
4517 +
4518 + fsg->gadget = gadget;
4519 + set_gadget_data(gadget, fsg);
4520 + fsg->ep0 = gadget->ep0;
4521 + fsg->ep0->driver_data = fsg;
4522 +
4523 + if ((rc = check_parameters(fsg)) != 0)
4524 + goto out;
4525 +
4526 + if (mod_data.removable) { // Enable the store_xxx attributes
4527 + dev_attr_file.attr.mode = 0644;
4528 + dev_attr_file.store = fsg_store_file;
4529 + if (!mod_data.cdrom) {
4530 + dev_attr_ro.attr.mode = 0644;
4531 + dev_attr_ro.store = fsg_store_ro;
4532 + }
4533 + }
4534 +
4535 + /* Only for removable media? */
4536 + dev_attr_nofua.attr.mode = 0644;
4537 + dev_attr_nofua.store = fsg_store_nofua;
4538 +
4539 + /* Find out how many LUNs there should be */
4540 + i = mod_data.nluns;
4541 + if (i == 0)
4542 + i = max(mod_data.num_filenames, 1u);
4543 + if (i > FSG_MAX_LUNS) {
4544 + ERROR(fsg, "invalid number of LUNs: %d\n", i);
4545 + rc = -EINVAL;
4546 + goto out;
4547 + }
4548 +
4549 + /* Create the LUNs, open their backing files, and register the
4550 + * LUN devices in sysfs. */
4551 + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
4552 + if (!fsg->luns) {
4553 + rc = -ENOMEM;
4554 + goto out;
4555 + }
4556 + fsg->nluns = i;
4557 +
4558 + for (i = 0; i < fsg->nluns; ++i) {
4559 + curlun = &fsg->luns[i];
4560 + curlun->cdrom = !!mod_data.cdrom;
4561 + curlun->ro = mod_data.cdrom || mod_data.ro[i];
4562 + curlun->initially_ro = curlun->ro;
4563 + curlun->removable = mod_data.removable;
4564 + curlun->nofua = mod_data.nofua[i];
4565 + curlun->dev.release = lun_release;
4566 + curlun->dev.parent = &gadget->dev;
4567 + curlun->dev.driver = &fsg_driver.driver;
4568 + dev_set_drvdata(&curlun->dev, &fsg->filesem);
4569 + dev_set_name(&curlun->dev,"%s-lun%d",
4570 + dev_name(&gadget->dev), i);
4571 +
4572 + kref_get(&fsg->ref);
4573 + rc = device_register(&curlun->dev);
4574 + if (rc) {
4575 + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
4576 + put_device(&curlun->dev);
4577 + goto out;
4578 + }
4579 + curlun->registered = 1;
4580 +
4581 + rc = device_create_file(&curlun->dev, &dev_attr_ro);
4582 + if (rc)
4583 + goto out;
4584 + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
4585 + if (rc)
4586 + goto out;
4587 + rc = device_create_file(&curlun->dev, &dev_attr_file);
4588 + if (rc)
4589 + goto out;
4590 +
4591 + if (mod_data.file[i] && *mod_data.file[i]) {
4592 + rc = fsg_lun_open(curlun, mod_data.file[i]);
4593 + if (rc)
4594 + goto out;
4595 + } else if (!mod_data.removable) {
4596 + ERROR(fsg, "no file given for LUN%d\n", i);
4597 + rc = -EINVAL;
4598 + goto out;
4599 + }
4600 + }
4601 +
4602 + /* Find all the endpoints we will use */
4603 + usb_ep_autoconfig_reset(gadget);
4604 + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
4605 + if (!ep)
4606 + goto autoconf_fail;
4607 + ep->driver_data = fsg; // claim the endpoint
4608 + fsg->bulk_in = ep;
4609 +
4610 + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
4611 + if (!ep)
4612 + goto autoconf_fail;
4613 + ep->driver_data = fsg; // claim the endpoint
4614 + fsg->bulk_out = ep;
4615 +
4616 + if (transport_is_cbi()) {
4617 + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
4618 + if (!ep)
4619 + goto autoconf_fail;
4620 + ep->driver_data = fsg; // claim the endpoint
4621 + fsg->intr_in = ep;
4622 + }
4623 +
4624 + /* Fix up the descriptors */
4625 + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
4626 + device_desc.idProduct = cpu_to_le16(mod_data.product);
4627 + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
4628 +
4629 + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
4630 + fsg_intf_desc.bNumEndpoints = i;
4631 + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
4632 + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
4633 + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4634 +
4635 + if (gadget_is_dualspeed(gadget)) {
4636 + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4637 +
4638 + /* Assume endpoint addresses are the same for both speeds */
4639 + fsg_hs_bulk_in_desc.bEndpointAddress =
4640 + fsg_fs_bulk_in_desc.bEndpointAddress;
4641 + fsg_hs_bulk_out_desc.bEndpointAddress =
4642 + fsg_fs_bulk_out_desc.bEndpointAddress;
4643 + fsg_hs_intr_in_desc.bEndpointAddress =
4644 + fsg_fs_intr_in_desc.bEndpointAddress;
4645 + }
4646 +
4647 + if (gadget_is_superspeed(gadget)) {
4648 + unsigned max_burst;
4649 +
4650 + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4651 +
4652 + /* Calculate bMaxBurst, we know packet size is 1024 */
4653 + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
4654 +
4655 + /* Assume endpoint addresses are the same for both speeds */
4656 + fsg_ss_bulk_in_desc.bEndpointAddress =
4657 + fsg_fs_bulk_in_desc.bEndpointAddress;
4658 + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
4659 +
4660 + fsg_ss_bulk_out_desc.bEndpointAddress =
4661 + fsg_fs_bulk_out_desc.bEndpointAddress;
4662 + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
4663 + }
4664 +
4665 + if (gadget_is_otg(gadget))
4666 + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
4667 +
4668 + rc = -ENOMEM;
4669 +
4670 + /* Allocate the request and buffer for endpoint 0 */
4671 + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
4672 + if (!req)
4673 + goto out;
4674 + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
4675 + if (!req->buf)
4676 + goto out;
4677 + req->complete = ep0_complete;
4678 +
4679 + /* Allocate the data buffers */
4680 + for (i = 0; i < fsg_num_buffers; ++i) {
4681 + struct fsg_buffhd *bh = &fsg->buffhds[i];
4682 +
4683 + /* Allocate for the bulk-in endpoint. We assume that
4684 + * the buffer will also work with the bulk-out (and
4685 + * interrupt-in) endpoint. */
4686 + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
4687 + if (!bh->buf)
4688 + goto out;
4689 + bh->next = bh + 1;
4690 + }
4691 + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
4692 +
4693 + /* This should reflect the actual gadget power source */
4694 + usb_gadget_set_selfpowered(gadget);
4695 +
4696 + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
4697 + "%s %s with %s",
4698 + init_utsname()->sysname, init_utsname()->release,
4699 + gadget->name);
4700 +
4701 + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
4702 + "file-storage-gadget");
4703 + if (IS_ERR(fsg->thread_task)) {
4704 + rc = PTR_ERR(fsg->thread_task);
4705 + goto out;
4706 + }
4707 +
4708 + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
4709 + INFO(fsg, "NOTE: This driver is deprecated. "
4710 + "Consider using g_mass_storage instead.\n");
4711 + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
4712 +
4713 + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
4714 + for (i = 0; i < fsg->nluns; ++i) {
4715 + curlun = &fsg->luns[i];
4716 + if (fsg_lun_is_open(curlun)) {
4717 + p = NULL;
4718 + if (pathbuf) {
4719 + p = d_path(&curlun->filp->f_path,
4720 + pathbuf, PATH_MAX);
4721 + if (IS_ERR(p))
4722 + p = NULL;
4723 + }
4724 + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
4725 + curlun->ro, curlun->nofua, (p ? p : "(error)"));
4726 + }
4727 + }
4728 + kfree(pathbuf);
4729 +
4730 + DBG(fsg, "transport=%s (x%02x)\n",
4731 + mod_data.transport_name, mod_data.transport_type);
4732 + DBG(fsg, "protocol=%s (x%02x)\n",
4733 + mod_data.protocol_name, mod_data.protocol_type);
4734 + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
4735 + mod_data.vendor, mod_data.product, mod_data.release);
4736 + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
4737 + mod_data.removable, mod_data.can_stall,
4738 + mod_data.cdrom, mod_data.buflen);
4739 + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
4740 +
4741 + set_bit(REGISTERED, &fsg->atomic_bitflags);
4742 +
4743 + /* Tell the thread to start working */
4744 + wake_up_process(fsg->thread_task);
4745 + return 0;
4746 +
4747 +autoconf_fail:
4748 + ERROR(fsg, "unable to autoconfigure all endpoints\n");
4749 + rc = -ENOTSUPP;
4750 +
4751 +out:
4752 + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
4753 + fsg_unbind(gadget);
4754 + complete(&fsg->thread_notifier);
4755 + return rc;
4756 +}
4757 +
4758 +
4759 +/*-------------------------------------------------------------------------*/
4760 +
4761 +static void fsg_suspend(struct usb_gadget *gadget)
4762 +{
4763 + struct fsg_dev *fsg = get_gadget_data(gadget);
4764 +
4765 + DBG(fsg, "suspend\n");
4766 + set_bit(SUSPENDED, &fsg->atomic_bitflags);
4767 +}
4768 +
4769 +static void fsg_resume(struct usb_gadget *gadget)
4770 +{
4771 + struct fsg_dev *fsg = get_gadget_data(gadget);
4772 +
4773 + DBG(fsg, "resume\n");
4774 + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
4775 +}
4776 +
4777 +
4778 +/*-------------------------------------------------------------------------*/
4779 +
4780 +static struct usb_gadget_driver fsg_driver = {
4781 + .max_speed = USB_SPEED_SUPER,
4782 + .function = (char *) fsg_string_product,
4783 + .unbind = fsg_unbind,
4784 + .disconnect = fsg_disconnect,
4785 + .setup = fsg_setup,
4786 + .suspend = fsg_suspend,
4787 + .resume = fsg_resume,
4788 +
4789 + .driver = {
4790 + .name = DRIVER_NAME,
4791 + .owner = THIS_MODULE,
4792 + // .release = ...
4793 + // .suspend = ...
4794 + // .resume = ...
4795 + },
4796 +};
4797 +
4798 +
4799 +static int __init fsg_alloc(void)
4800 +{
4801 + struct fsg_dev *fsg;
4802 +
4803 + fsg = kzalloc(sizeof *fsg +
4804 + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
4805 +
4806 + if (!fsg)
4807 + return -ENOMEM;
4808 + spin_lock_init(&fsg->lock);
4809 + init_rwsem(&fsg->filesem);
4810 + kref_init(&fsg->ref);
4811 + init_completion(&fsg->thread_notifier);
4812 +
4813 + the_fsg = fsg;
4814 + return 0;
4815 +}
4816 +
4817 +
4818 +static int __init fsg_init(void)
4819 +{
4820 + int rc;
4821 + struct fsg_dev *fsg;
4822 +
4823 + rc = fsg_num_buffers_validate();
4824 + if (rc != 0)
4825 + return rc;
4826 +
4827 + if ((rc = fsg_alloc()) != 0)
4828 + return rc;
4829 + fsg = the_fsg;
4830 + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
4831 + kref_put(&fsg->ref, fsg_release);
4832 + return rc;
4833 +}
4834 +module_init(fsg_init);
4835 +
4836 +
4837 +static void __exit fsg_cleanup(void)
4838 +{
4839 + struct fsg_dev *fsg = the_fsg;
4840 +
4841 + /* Unregister the driver iff the thread hasn't already done so */
4842 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
4843 + usb_gadget_unregister_driver(&fsg_driver);
4844 +
4845 + /* Wait for the thread to finish up */
4846 + wait_for_completion(&fsg->thread_notifier);
4847 +
4848 + kref_put(&fsg->ref, fsg_release);
4849 +}
4850 +module_exit(fsg_cleanup);
4851 --- a/drivers/usb/host/Kconfig
4852 +++ b/drivers/usb/host/Kconfig
4853 @@ -748,6 +748,16 @@ config USB_HWA_HCD
4854 To compile this driver a module, choose M here: the module
4855 will be called "hwa-hc".
4856
4857 +config USB_DWCOTG
4858 + bool "Synopsis DWC host support"
4859 + depends on USB && (FIQ || ARM64)
4860 + help
4861 + The Synopsis DWC controller is a dual-role
4862 + host/peripheral/OTG ("On The Go") USB controllers.
4863 +
4864 + Enable this option to support this IP in host controller mode.
4865 + If unsure, say N.
4866 +
4867 config USB_IMX21_HCD
4868 tristate "i.MX21 HCD support"
4869 depends on ARM && ARCH_MXC
4870 --- a/drivers/usb/host/Makefile
4871 +++ b/drivers/usb/host/Makefile
4872 @@ -83,6 +83,8 @@ obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
4873 obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
4874 obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
4875 obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
4876 +
4877 +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
4878 obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
4879 obj-$(CONFIG_USB_FSL_USB2) += fsl-mph-dr-of.o
4880 obj-$(CONFIG_USB_EHCI_FSL) += fsl-mph-dr-of.o
4881 --- /dev/null
4882 +++ b/drivers/usb/host/dwc_common_port/Makefile
4883 @@ -0,0 +1,58 @@
4884 +#
4885 +# Makefile for DWC_common library
4886 +#
4887 +
4888 +ifneq ($(KERNELRELEASE),)
4889 +
4890 +ccflags-y += -DDWC_LINUX
4891 +#ccflags-y += -DDEBUG
4892 +#ccflags-y += -DDWC_DEBUG_REGS
4893 +#ccflags-y += -DDWC_DEBUG_MEMORY
4894 +
4895 +ccflags-y += -DDWC_LIBMODULE
4896 +ccflags-y += -DDWC_CCLIB
4897 +#ccflags-y += -DDWC_CRYPTOLIB
4898 +ccflags-y += -DDWC_NOTIFYLIB
4899 +ccflags-y += -DDWC_UTFLIB
4900 +
4901 +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
4902 +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
4903 + dwc_crypto.o dwc_notifier.o \
4904 + dwc_common_linux.o dwc_mem.o
4905 +
4906 +kernrelwd := $(subst ., ,$(KERNELRELEASE))
4907 +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
4908 +
4909 +ifneq ($(kernrel3),2.6.20)
4910 +# grayg - I only know that we use ccflags-y in 2.6.31 actually
4911 +ccflags-y += $(CPPFLAGS)
4912 +endif
4913 +
4914 +else
4915 +
4916 +#ifeq ($(KDIR),)
4917 +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
4918 +#endif
4919 +
4920 +ifeq ($(ARCH),)
4921 +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
4922 + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
4923 +endif
4924 +
4925 +ifeq ($(DOXYGEN),)
4926 +DOXYGEN := doxygen
4927 +endif
4928 +
4929 +default:
4930 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
4931 +
4932 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
4933 + $(DOXYGEN) doc/doxygen.cfg
4934 +
4935 +tags: $(wildcard *.[hc])
4936 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
4937 +
4938 +endif
4939 +
4940 +clean:
4941 + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
4942 --- /dev/null
4943 +++ b/drivers/usb/host/dwc_common_port/Makefile.fbsd
4944 @@ -0,0 +1,17 @@
4945 +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
4946 +CFLAGS += -DDWC_FREEBSD
4947 +CFLAGS += -DDEBUG
4948 +#CFLAGS += -DDWC_DEBUG_REGS
4949 +#CFLAGS += -DDWC_DEBUG_MEMORY
4950 +
4951 +#CFLAGS += -DDWC_LIBMODULE
4952 +#CFLAGS += -DDWC_CCLIB
4953 +#CFLAGS += -DDWC_CRYPTOLIB
4954 +#CFLAGS += -DDWC_NOTIFYLIB
4955 +#CFLAGS += -DDWC_UTFLIB
4956 +
4957 +KMOD = dwc_common_port_lib
4958 +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
4959 + dwc_common_fbsd.c dwc_mem.c
4960 +
4961 +.include <bsd.kmod.mk>
4962 --- /dev/null
4963 +++ b/drivers/usb/host/dwc_common_port/Makefile.linux
4964 @@ -0,0 +1,49 @@
4965 +#
4966 +# Makefile for DWC_common library
4967 +#
4968 +ifneq ($(KERNELRELEASE),)
4969 +
4970 +ccflags-y += -DDWC_LINUX
4971 +#ccflags-y += -DDEBUG
4972 +#ccflags-y += -DDWC_DEBUG_REGS
4973 +#ccflags-y += -DDWC_DEBUG_MEMORY
4974 +
4975 +ccflags-y += -DDWC_LIBMODULE
4976 +ccflags-y += -DDWC_CCLIB
4977 +ccflags-y += -DDWC_CRYPTOLIB
4978 +ccflags-y += -DDWC_NOTIFYLIB
4979 +ccflags-y += -DDWC_UTFLIB
4980 +
4981 +obj-m := dwc_common_port_lib.o
4982 +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
4983 + dwc_crypto.o dwc_notifier.o \
4984 + dwc_common_linux.o dwc_mem.o
4985 +
4986 +else
4987 +
4988 +ifeq ($(KDIR),)
4989 +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
4990 +endif
4991 +
4992 +ifeq ($(ARCH),)
4993 +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
4994 + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
4995 +endif
4996 +
4997 +ifeq ($(DOXYGEN),)
4998 +DOXYGEN := doxygen
4999 +endif
5000 +
5001 +default:
5002 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
5003 +
5004 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
5005 + $(DOXYGEN) doc/doxygen.cfg
5006 +
5007 +tags: $(wildcard *.[hc])
5008 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
5009 +
5010 +endif
5011 +
5012 +clean:
5013 + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
5014 --- /dev/null
5015 +++ b/drivers/usb/host/dwc_common_port/changes.txt
5016 @@ -0,0 +1,174 @@
5017 +
5018 +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
5019 +IO context struct. The IO context struct should live in an os-dependent struct
5020 +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
5021 +named 'os_dep' embedded in the main device struct. So there these calls look
5022 +like this:
5023 +
5024 + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
5025 +
5026 + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
5027 + &pcd->dev_global_regs->dcfg, 0);
5028 +
5029 +Note that for the existing Linux driver ports, it is not necessary to actually
5030 +define the 'ioctx' member in the os-dependent struct. Since Linux does not
5031 +require an IO context, its macros for dwc_read_reg32() and friends do not
5032 +use the context pointer, so it is optimized away by the compiler. But it is
5033 +necessary to add the pointer parameter to all of the call sites, to be ready
5034 +for any future ports (such as FreeBSD) which do require an IO context.
5035 +
5036 +
5037 +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
5038 +take an additional parameter, a pointer to a memory context. Examples:
5039 +
5040 + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
5041 +
5042 + dwc_free(&usb3_dev->os_dep.memctx, addr);
5043 +
5044 +Again, for the Linux ports, it is not necessary to actually define the memctx
5045 +member, but it is necessary to add the pointer parameter to all of the call
5046 +sites.
5047 +
5048 +
5049 +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
5050 +
5051 + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
5052 +
5053 + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
5054 +
5055 +
5056 +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
5057 +
5058 + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
5059 +
5060 + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
5061 +
5062 +
5063 +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
5064 +
5065 + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
5066 +
5067 + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
5068 +
5069 +
5070 +Same for dwc_timer_alloc(). Example:
5071 +
5072 + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
5073 + cb_func, cb_data);
5074 +
5075 +
5076 +Same for dwc_waitq_alloc(). Example:
5077 +
5078 + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
5079 +
5080 +
5081 +Same for dwc_thread_run(). Example:
5082 +
5083 + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
5084 + "dwc_usb3_thd1", data);
5085 +
5086 +
5087 +Same for dwc_workq_alloc(). Example:
5088 +
5089 + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
5090 +
5091 +
5092 +Same for dwc_task_alloc(). Example:
5093 +
5094 + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
5095 + cb_func, cb_data);
5096 +
5097 +
5098 +In addition to the context pointer additions, a few core functions have had
5099 +other changes made to their parameters:
5100 +
5101 +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
5102 +has been changed from a uint64_t to a dwc_irqflags_t.
5103 +
5104 +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
5105 +FreeBSD equivalent of that function requires it.
5106 +
5107 +And, in addition to the context pointer, dwc_task_alloc() also adds a
5108 +'char *name' parameter, to be consistent with dwc_thread_run() and
5109 +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
5110 +requires a unique name.
5111 +
5112 +
5113 +Here is a complete list of the core functions that now take a pointer to a
5114 +context as their first parameter:
5115 +
5116 + dwc_read_reg32
5117 + dwc_read_reg64
5118 + dwc_write_reg32
5119 + dwc_write_reg64
5120 + dwc_modify_reg32
5121 + dwc_modify_reg64
5122 + dwc_alloc
5123 + dwc_alloc_atomic
5124 + dwc_strdup
5125 + dwc_free
5126 + dwc_dma_alloc
5127 + dwc_dma_free
5128 + dwc_mutex_alloc
5129 + dwc_mutex_free
5130 + dwc_spinlock_alloc
5131 + dwc_spinlock_free
5132 + dwc_timer_alloc
5133 + dwc_waitq_alloc
5134 + dwc_thread_run
5135 + dwc_workq_alloc
5136 + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
5137 +
5138 +And here are the core functions that have other changes to their parameters:
5139 +
5140 + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
5141 + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
5142 + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
5143 +
5144 +
5145 +
5146 +The changes to the core functions also require some of the other library
5147 +functions to change:
5148 +
5149 + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
5150 + (for memory allocation) as the 1st param and a 'void *mtxctx'
5151 + (for mutex allocation) as the 2nd param.
5152 +
5153 + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
5154 + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
5155 + 'void *memctx' as the 1st param.
5156 +
5157 + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
5158 + 'void *memctx' as the 1st param.
5159 +
5160 + dwc_modpow() now takes a 'void *memctx' as the 1st param.
5161 +
5162 + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
5163 + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
5164 + param, and also now returns an integer value that is non-zero if
5165 + allocation of its data structures or work queue fails.
5166 +
5167 + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
5168 +
5169 + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
5170 + param, and also now returns an integer value that is non-zero if
5171 + allocation of its data structures fails.
5172 +
5173 +
5174 +
5175 +Other miscellaneous changes:
5176 +
5177 +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
5178 +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
5179 +
5180 +The following #define's have been added to allow selectively compiling library
5181 +features:
5182 +
5183 + DWC_CCLIB
5184 + DWC_CRYPTOLIB
5185 + DWC_NOTIFYLIB
5186 + DWC_UTFLIB
5187 +
5188 +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
5189 +module code in dwc_common_linux.c is not compiled in. This allows linking the
5190 +library code directly into a driver module, instead of as a standalone module.
5191 --- /dev/null
5192 +++ b/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
5193 @@ -0,0 +1,270 @@
5194 +# Doxyfile 1.4.5
5195 +
5196 +#---------------------------------------------------------------------------
5197 +# Project related configuration options
5198 +#---------------------------------------------------------------------------
5199 +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
5200 +PROJECT_NUMBER =
5201 +OUTPUT_DIRECTORY = doc
5202 +CREATE_SUBDIRS = NO
5203 +OUTPUT_LANGUAGE = English
5204 +BRIEF_MEMBER_DESC = YES
5205 +REPEAT_BRIEF = YES
5206 +ABBREVIATE_BRIEF = "The $name class" \
5207 + "The $name widget" \
5208 + "The $name file" \
5209 + is \
5210 + provides \
5211 + specifies \
5212 + contains \
5213 + represents \
5214 + a \
5215 + an \
5216 + the
5217 +ALWAYS_DETAILED_SEC = YES
5218 +INLINE_INHERITED_MEMB = NO
5219 +FULL_PATH_NAMES = NO
5220 +STRIP_FROM_PATH = ..
5221 +STRIP_FROM_INC_PATH =
5222 +SHORT_NAMES = NO
5223 +JAVADOC_AUTOBRIEF = YES
5224 +MULTILINE_CPP_IS_BRIEF = NO
5225 +DETAILS_AT_TOP = YES
5226 +INHERIT_DOCS = YES
5227 +SEPARATE_MEMBER_PAGES = NO
5228 +TAB_SIZE = 8
5229 +ALIASES =
5230 +OPTIMIZE_OUTPUT_FOR_C = YES
5231 +OPTIMIZE_OUTPUT_JAVA = NO
5232 +BUILTIN_STL_SUPPORT = NO
5233 +DISTRIBUTE_GROUP_DOC = NO
5234 +SUBGROUPING = NO
5235 +#---------------------------------------------------------------------------
5236 +# Build related configuration options
5237 +#---------------------------------------------------------------------------
5238 +EXTRACT_ALL = NO
5239 +EXTRACT_PRIVATE = NO
5240 +EXTRACT_STATIC = YES
5241 +EXTRACT_LOCAL_CLASSES = NO
5242 +EXTRACT_LOCAL_METHODS = NO
5243 +HIDE_UNDOC_MEMBERS = NO
5244 +HIDE_UNDOC_CLASSES = NO
5245 +HIDE_FRIEND_COMPOUNDS = NO
5246 +HIDE_IN_BODY_DOCS = NO
5247 +INTERNAL_DOCS = NO
5248 +CASE_SENSE_NAMES = YES
5249 +HIDE_SCOPE_NAMES = NO
5250 +SHOW_INCLUDE_FILES = NO
5251 +INLINE_INFO = YES
5252 +SORT_MEMBER_DOCS = NO
5253 +SORT_BRIEF_DOCS = NO
5254 +SORT_BY_SCOPE_NAME = NO
5255 +GENERATE_TODOLIST = YES
5256 +GENERATE_TESTLIST = YES
5257 +GENERATE_BUGLIST = YES
5258 +GENERATE_DEPRECATEDLIST= YES
5259 +ENABLED_SECTIONS =
5260 +MAX_INITIALIZER_LINES = 30
5261 +SHOW_USED_FILES = YES
5262 +SHOW_DIRECTORIES = YES
5263 +FILE_VERSION_FILTER =
5264 +#---------------------------------------------------------------------------
5265 +# configuration options related to warning and progress messages
5266 +#---------------------------------------------------------------------------
5267 +QUIET = YES
5268 +WARNINGS = YES
5269 +WARN_IF_UNDOCUMENTED = NO
5270 +WARN_IF_DOC_ERROR = YES
5271 +WARN_NO_PARAMDOC = YES
5272 +WARN_FORMAT = "$file:$line: $text"
5273 +WARN_LOGFILE =
5274 +#---------------------------------------------------------------------------
5275 +# configuration options related to the input files
5276 +#---------------------------------------------------------------------------
5277 +INPUT = .
5278 +FILE_PATTERNS = *.c \
5279 + *.cc \
5280 + *.cxx \
5281 + *.cpp \
5282 + *.c++ \
5283 + *.d \
5284 + *.java \
5285 + *.ii \
5286 + *.ixx \
5287 + *.ipp \
5288 + *.i++ \
5289 + *.inl \
5290 + *.h \
5291 + *.hh \
5292 + *.hxx \
5293 + *.hpp \
5294 + *.h++ \
5295 + *.idl \
5296 + *.odl \
5297 + *.cs \
5298 + *.php \
5299 + *.php3 \
5300 + *.inc \
5301 + *.m \
5302 + *.mm \
5303 + *.dox \
5304 + *.py \
5305 + *.C \
5306 + *.CC \
5307 + *.C++ \
5308 + *.II \
5309 + *.I++ \
5310 + *.H \
5311 + *.HH \
5312 + *.H++ \
5313 + *.CS \
5314 + *.PHP \
5315 + *.PHP3 \
5316 + *.M \
5317 + *.MM \
5318 + *.PY
5319 +RECURSIVE = NO
5320 +EXCLUDE =
5321 +EXCLUDE_SYMLINKS = NO
5322 +EXCLUDE_PATTERNS =
5323 +EXAMPLE_PATH =
5324 +EXAMPLE_PATTERNS = *
5325 +EXAMPLE_RECURSIVE = NO
5326 +IMAGE_PATH =
5327 +INPUT_FILTER =
5328 +FILTER_PATTERNS =
5329 +FILTER_SOURCE_FILES = NO
5330 +#---------------------------------------------------------------------------
5331 +# configuration options related to source browsing
5332 +#---------------------------------------------------------------------------
5333 +SOURCE_BROWSER = NO
5334 +INLINE_SOURCES = NO
5335 +STRIP_CODE_COMMENTS = YES
5336 +REFERENCED_BY_RELATION = YES
5337 +REFERENCES_RELATION = YES
5338 +USE_HTAGS = NO
5339 +VERBATIM_HEADERS = NO
5340 +#---------------------------------------------------------------------------
5341 +# configuration options related to the alphabetical class index
5342 +#---------------------------------------------------------------------------
5343 +ALPHABETICAL_INDEX = NO
5344 +COLS_IN_ALPHA_INDEX = 5
5345 +IGNORE_PREFIX =
5346 +#---------------------------------------------------------------------------
5347 +# configuration options related to the HTML output
5348 +#---------------------------------------------------------------------------
5349 +GENERATE_HTML = YES
5350 +HTML_OUTPUT = html
5351 +HTML_FILE_EXTENSION = .html
5352 +HTML_HEADER =
5353 +HTML_FOOTER =
5354 +HTML_STYLESHEET =
5355 +HTML_ALIGN_MEMBERS = YES
5356 +GENERATE_HTMLHELP = NO
5357 +CHM_FILE =
5358 +HHC_LOCATION =
5359 +GENERATE_CHI = NO
5360 +BINARY_TOC = NO
5361 +TOC_EXPAND = NO
5362 +DISABLE_INDEX = NO
5363 +ENUM_VALUES_PER_LINE = 4
5364 +GENERATE_TREEVIEW = YES
5365 +TREEVIEW_WIDTH = 250
5366 +#---------------------------------------------------------------------------
5367 +# configuration options related to the LaTeX output
5368 +#---------------------------------------------------------------------------
5369 +GENERATE_LATEX = NO
5370 +LATEX_OUTPUT = latex
5371 +LATEX_CMD_NAME = latex
5372 +MAKEINDEX_CMD_NAME = makeindex
5373 +COMPACT_LATEX = NO
5374 +PAPER_TYPE = a4wide
5375 +EXTRA_PACKAGES =
5376 +LATEX_HEADER =
5377 +PDF_HYPERLINKS = NO
5378 +USE_PDFLATEX = NO
5379 +LATEX_BATCHMODE = NO
5380 +LATEX_HIDE_INDICES = NO
5381 +#---------------------------------------------------------------------------
5382 +# configuration options related to the RTF output
5383 +#---------------------------------------------------------------------------
5384 +GENERATE_RTF = NO
5385 +RTF_OUTPUT = rtf
5386 +COMPACT_RTF = NO
5387 +RTF_HYPERLINKS = NO
5388 +RTF_STYLESHEET_FILE =
5389 +RTF_EXTENSIONS_FILE =
5390 +#---------------------------------------------------------------------------
5391 +# configuration options related to the man page output
5392 +#---------------------------------------------------------------------------
5393 +GENERATE_MAN = NO
5394 +MAN_OUTPUT = man
5395 +MAN_EXTENSION = .3
5396 +MAN_LINKS = NO
5397 +#---------------------------------------------------------------------------
5398 +# configuration options related to the XML output
5399 +#---------------------------------------------------------------------------
5400 +GENERATE_XML = NO
5401 +XML_OUTPUT = xml
5402 +XML_SCHEMA =
5403 +XML_DTD =
5404 +XML_PROGRAMLISTING = YES
5405 +#---------------------------------------------------------------------------
5406 +# configuration options for the AutoGen Definitions output
5407 +#---------------------------------------------------------------------------
5408 +GENERATE_AUTOGEN_DEF = NO
5409 +#---------------------------------------------------------------------------
5410 +# configuration options related to the Perl module output
5411 +#---------------------------------------------------------------------------
5412 +GENERATE_PERLMOD = NO
5413 +PERLMOD_LATEX = NO
5414 +PERLMOD_PRETTY = YES
5415 +PERLMOD_MAKEVAR_PREFIX =
5416 +#---------------------------------------------------------------------------
5417 +# Configuration options related to the preprocessor
5418 +#---------------------------------------------------------------------------
5419 +ENABLE_PREPROCESSING = YES
5420 +MACRO_EXPANSION = NO
5421 +EXPAND_ONLY_PREDEF = NO
5422 +SEARCH_INCLUDES = YES
5423 +INCLUDE_PATH =
5424 +INCLUDE_FILE_PATTERNS =
5425 +PREDEFINED = DEBUG DEBUG_MEMORY
5426 +EXPAND_AS_DEFINED =
5427 +SKIP_FUNCTION_MACROS = YES
5428 +#---------------------------------------------------------------------------
5429 +# Configuration::additions related to external references
5430 +#---------------------------------------------------------------------------
5431 +TAGFILES =
5432 +GENERATE_TAGFILE =
5433 +ALLEXTERNALS = NO
5434 +EXTERNAL_GROUPS = YES
5435 +PERL_PATH = /usr/bin/perl
5436 +#---------------------------------------------------------------------------
5437 +# Configuration options related to the dot tool
5438 +#---------------------------------------------------------------------------
5439 +CLASS_DIAGRAMS = YES
5440 +HIDE_UNDOC_RELATIONS = YES
5441 +HAVE_DOT = NO
5442 +CLASS_GRAPH = YES
5443 +COLLABORATION_GRAPH = YES
5444 +GROUP_GRAPHS = YES
5445 +UML_LOOK = NO
5446 +TEMPLATE_RELATIONS = NO
5447 +INCLUDE_GRAPH = NO
5448 +INCLUDED_BY_GRAPH = YES
5449 +CALL_GRAPH = NO
5450 +GRAPHICAL_HIERARCHY = YES
5451 +DIRECTORY_GRAPH = YES
5452 +DOT_IMAGE_FORMAT = png
5453 +DOT_PATH =
5454 +DOTFILE_DIRS =
5455 +MAX_DOT_GRAPH_DEPTH = 1000
5456 +DOT_TRANSPARENT = NO
5457 +DOT_MULTI_TARGETS = NO
5458 +GENERATE_LEGEND = YES
5459 +DOT_CLEANUP = YES
5460 +#---------------------------------------------------------------------------
5461 +# Configuration::additions related to the search engine
5462 +#---------------------------------------------------------------------------
5463 +SEARCHENGINE = NO
5464 --- /dev/null
5465 +++ b/drivers/usb/host/dwc_common_port/dwc_cc.c
5466 @@ -0,0 +1,532 @@
5467 +/* =========================================================================
5468 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
5469 + * $Revision: #4 $
5470 + * $Date: 2010/11/04 $
5471 + * $Change: 1621692 $
5472 + *
5473 + * Synopsys Portability Library Software and documentation
5474 + * (hereinafter, "Software") is an Unsupported proprietary work of
5475 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
5476 + * between Synopsys and you.
5477 + *
5478 + * The Software IS NOT an item of Licensed Software or Licensed Product
5479 + * under any End User Software License Agreement or Agreement for
5480 + * Licensed Product with Synopsys or any supplement thereto. You are
5481 + * permitted to use and redistribute this Software in source and binary
5482 + * forms, with or without modification, provided that redistributions
5483 + * of source code must retain this notice. You may not view, use,
5484 + * disclose, copy or distribute this file or any information contained
5485 + * herein except pursuant to this license grant from Synopsys. If you
5486 + * do not agree with this notice, including the disclaimer below, then
5487 + * you are not authorized to use the Software.
5488 + *
5489 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
5490 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5491 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
5492 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
5493 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5494 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5495 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5496 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
5497 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5498 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
5499 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
5500 + * DAMAGE.
5501 + * ========================================================================= */
5502 +#ifdef DWC_CCLIB
5503 +
5504 +#include "dwc_cc.h"
5505 +
5506 +typedef struct dwc_cc
5507 +{
5508 + uint32_t uid;
5509 + uint8_t chid[16];
5510 + uint8_t cdid[16];
5511 + uint8_t ck[16];
5512 + uint8_t *name;
5513 + uint8_t length;
5514 + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
5515 +} dwc_cc_t;
5516 +
5517 +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
5518 +
5519 +/** The main structure for CC management. */
5520 +struct dwc_cc_if
5521 +{
5522 + dwc_mutex_t *mutex;
5523 + char *filename;
5524 +
5525 + unsigned is_host:1;
5526 +
5527 + dwc_notifier_t *notifier;
5528 +
5529 + struct context_list list;
5530 +};
5531 +
5532 +#ifdef DEBUG
5533 +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
5534 +{
5535 + int i;
5536 + DWC_PRINTF("%s: ", name);
5537 + for (i=0; i<len; i++) {
5538 + DWC_PRINTF("%02x ", bytes[i]);
5539 + }
5540 + DWC_PRINTF("\n");
5541 +}
5542 +#else
5543 +#define dump_bytes(x...)
5544 +#endif
5545 +
5546 +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
5547 +{
5548 + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
5549 + if (!cc) {
5550 + return NULL;
5551 + }
5552 + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
5553 +
5554 + if (name) {
5555 + cc->length = length;
5556 + cc->name = dwc_alloc(mem_ctx, length);
5557 + if (!cc->name) {
5558 + dwc_free(mem_ctx, cc);
5559 + return NULL;
5560 + }
5561 +
5562 + DWC_MEMCPY(cc->name, name, length);
5563 + }
5564 +
5565 + return cc;
5566 +}
5567 +
5568 +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
5569 +{
5570 + if (cc->name) {
5571 + dwc_free(mem_ctx, cc->name);
5572 + }
5573 + dwc_free(mem_ctx, cc);
5574 +}
5575 +
5576 +static uint32_t next_uid(dwc_cc_if_t *cc_if)
5577 +{
5578 + uint32_t uid = 0;
5579 + dwc_cc_t *cc;
5580 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5581 + if (cc->uid > uid) {
5582 + uid = cc->uid;
5583 + }
5584 + }
5585 +
5586 + if (uid == 0) {
5587 + uid = 255;
5588 + }
5589 +
5590 + return uid + 1;
5591 +}
5592 +
5593 +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
5594 +{
5595 + dwc_cc_t *cc;
5596 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5597 + if (cc->uid == uid) {
5598 + return cc;
5599 + }
5600 + }
5601 + return NULL;
5602 +}
5603 +
5604 +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
5605 +{
5606 + unsigned int size = 0;
5607 + dwc_cc_t *cc;
5608 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5609 + size += (48 + 1);
5610 + if (cc->name) {
5611 + size += cc->length;
5612 + }
5613 + }
5614 + return size;
5615 +}
5616 +
5617 +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
5618 +{
5619 + uint32_t uid = 0;
5620 + dwc_cc_t *cc;
5621 +
5622 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5623 + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
5624 + uid = cc->uid;
5625 + break;
5626 + }
5627 + }
5628 + return uid;
5629 +}
5630 +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
5631 +{
5632 + uint32_t uid = 0;
5633 + dwc_cc_t *cc;
5634 +
5635 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5636 + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
5637 + uid = cc->uid;
5638 + break;
5639 + }
5640 + }
5641 + return uid;
5642 +}
5643 +
5644 +/* Internal cc_add */
5645 +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
5646 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5647 +{
5648 + dwc_cc_t *cc;
5649 + uint32_t uid;
5650 +
5651 + if (cc_if->is_host) {
5652 + uid = cc_match_cdid(cc_if, cdid);
5653 + }
5654 + else {
5655 + uid = cc_match_chid(cc_if, chid);
5656 + }
5657 +
5658 + if (uid) {
5659 + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
5660 + cc = cc_find(cc_if, uid);
5661 + }
5662 + else {
5663 + cc = alloc_cc(mem_ctx, name, length);
5664 + cc->uid = next_uid(cc_if);
5665 + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
5666 + }
5667 +
5668 + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
5669 + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
5670 + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
5671 +
5672 + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
5673 + dump_bytes("CHID", cc->chid, 16);
5674 + dump_bytes("CDID", cc->cdid, 16);
5675 + dump_bytes("CK", cc->ck, 16);
5676 + return cc->uid;
5677 +}
5678 +
5679 +/* Internal cc_clear */
5680 +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
5681 +{
5682 + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
5683 + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
5684 + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
5685 + free_cc(mem_ctx, cc);
5686 + }
5687 +}
5688 +
5689 +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
5690 + dwc_notifier_t *notifier, unsigned is_host)
5691 +{
5692 + dwc_cc_if_t *cc_if = NULL;
5693 +
5694 + /* Allocate a common_cc_if structure */
5695 + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
5696 +
5697 + if (!cc_if)
5698 + return NULL;
5699 +
5700 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
5701 + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
5702 +#else
5703 + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
5704 +#endif
5705 + if (!cc_if->mutex) {
5706 + dwc_free(mem_ctx, cc_if);
5707 + return NULL;
5708 + }
5709 +
5710 + DWC_CIRCLEQ_INIT(&cc_if->list);
5711 + cc_if->is_host = is_host;
5712 + cc_if->notifier = notifier;
5713 + return cc_if;
5714 +}
5715 +
5716 +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
5717 +{
5718 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
5719 + DWC_MUTEX_FREE(cc_if->mutex);
5720 +#else
5721 + dwc_mutex_free(mtx_ctx, cc_if->mutex);
5722 +#endif
5723 + cc_clear(mem_ctx, cc_if);
5724 + dwc_free(mem_ctx, cc_if);
5725 +}
5726 +
5727 +static void cc_changed(dwc_cc_if_t *cc_if)
5728 +{
5729 + if (cc_if->notifier) {
5730 + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
5731 + }
5732 +}
5733 +
5734 +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
5735 +{
5736 + DWC_MUTEX_LOCK(cc_if->mutex);
5737 + cc_clear(mem_ctx, cc_if);
5738 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5739 + cc_changed(cc_if);
5740 +}
5741 +
5742 +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
5743 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5744 +{
5745 + uint32_t uid;
5746 +
5747 + DWC_MUTEX_LOCK(cc_if->mutex);
5748 + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
5749 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5750 + cc_changed(cc_if);
5751 +
5752 + return uid;
5753 +}
5754 +
5755 +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
5756 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5757 +{
5758 + dwc_cc_t* cc;
5759 +
5760 + DWC_DEBUGC("Change connection context %d", id);
5761 +
5762 + DWC_MUTEX_LOCK(cc_if->mutex);
5763 + cc = cc_find(cc_if, id);
5764 + if (!cc) {
5765 + DWC_ERROR("Uid %d not found in cc list\n", id);
5766 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5767 + return;
5768 + }
5769 +
5770 + if (chid) {
5771 + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
5772 + }
5773 + if (cdid) {
5774 + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
5775 + }
5776 + if (ck) {
5777 + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
5778 + }
5779 +
5780 + if (name) {
5781 + if (cc->name) {
5782 + dwc_free(mem_ctx, cc->name);
5783 + }
5784 + cc->name = dwc_alloc(mem_ctx, length);
5785 + if (!cc->name) {
5786 + DWC_ERROR("Out of memory in dwc_cc_change()\n");
5787 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5788 + return;
5789 + }
5790 + cc->length = length;
5791 + DWC_MEMCPY(cc->name, name, length);
5792 + }
5793 +
5794 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5795 +
5796 + cc_changed(cc_if);
5797 +
5798 + DWC_DEBUGC("Changed connection context id=%d\n", id);
5799 + dump_bytes("New CHID", cc->chid, 16);
5800 + dump_bytes("New CDID", cc->cdid, 16);
5801 + dump_bytes("New CK", cc->ck, 16);
5802 +}
5803 +
5804 +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
5805 +{
5806 + dwc_cc_t *cc;
5807 +
5808 + DWC_DEBUGC("Removing connection context %d", id);
5809 +
5810 + DWC_MUTEX_LOCK(cc_if->mutex);
5811 + cc = cc_find(cc_if, id);
5812 + if (!cc) {
5813 + DWC_ERROR("Uid %d not found in cc list\n", id);
5814 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5815 + return;
5816 + }
5817 +
5818 + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
5819 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5820 + free_cc(mem_ctx, cc);
5821 +
5822 + cc_changed(cc_if);
5823 +}
5824 +
5825 +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
5826 +{
5827 + uint8_t *buf, *x;
5828 + uint8_t zero = 0;
5829 + dwc_cc_t *cc;
5830 +
5831 + DWC_MUTEX_LOCK(cc_if->mutex);
5832 + *length = cc_data_size(cc_if);
5833 + if (!(*length)) {
5834 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5835 + return NULL;
5836 + }
5837 +
5838 + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
5839 +
5840 + buf = dwc_alloc(mem_ctx, *length);
5841 + if (!buf) {
5842 + *length = 0;
5843 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5844 + return NULL;
5845 + }
5846 +
5847 + x = buf;
5848 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5849 + DWC_MEMCPY(x, cc->chid, 16);
5850 + x += 16;
5851 + DWC_MEMCPY(x, cc->cdid, 16);
5852 + x += 16;
5853 + DWC_MEMCPY(x, cc->ck, 16);
5854 + x += 16;
5855 + if (cc->name) {
5856 + DWC_MEMCPY(x, &cc->length, 1);
5857 + x += 1;
5858 + DWC_MEMCPY(x, cc->name, cc->length);
5859 + x += cc->length;
5860 + }
5861 + else {
5862 + DWC_MEMCPY(x, &zero, 1);
5863 + x += 1;
5864 + }
5865 + }
5866 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5867 +
5868 + return buf;
5869 +}
5870 +
5871 +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
5872 +{
5873 + uint8_t name_length;
5874 + uint8_t *name;
5875 + uint8_t *chid;
5876 + uint8_t *cdid;
5877 + uint8_t *ck;
5878 + uint32_t i = 0;
5879 +
5880 + DWC_MUTEX_LOCK(cc_if->mutex);
5881 + cc_clear(mem_ctx, cc_if);
5882 +
5883 + while (i < length) {
5884 + chid = &data[i];
5885 + i += 16;
5886 + cdid = &data[i];
5887 + i += 16;
5888 + ck = &data[i];
5889 + i += 16;
5890 +
5891 + name_length = data[i];
5892 + i ++;
5893 +
5894 + if (name_length) {
5895 + name = &data[i];
5896 + i += name_length;
5897 + }
5898 + else {
5899 + name = NULL;
5900 + }
5901 +
5902 + /* check to see if we haven't overflown the buffer */
5903 + if (i > length) {
5904 + DWC_ERROR("Data format error while attempting to load CCs "
5905 + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
5906 + break;
5907 + }
5908 +
5909 + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
5910 + }
5911 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5912 +
5913 + cc_changed(cc_if);
5914 +}
5915 +
5916 +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
5917 +{
5918 + uint32_t uid = 0;
5919 +
5920 + DWC_MUTEX_LOCK(cc_if->mutex);
5921 + uid = cc_match_chid(cc_if, chid);
5922 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5923 + return uid;
5924 +}
5925 +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
5926 +{
5927 + uint32_t uid = 0;
5928 +
5929 + DWC_MUTEX_LOCK(cc_if->mutex);
5930 + uid = cc_match_cdid(cc_if, cdid);
5931 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5932 + return uid;
5933 +}
5934 +
5935 +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
5936 +{
5937 + uint8_t *ck = NULL;
5938 + dwc_cc_t *cc;
5939 +
5940 + DWC_MUTEX_LOCK(cc_if->mutex);
5941 + cc = cc_find(cc_if, id);
5942 + if (cc) {
5943 + ck = cc->ck;
5944 + }
5945 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5946 +
5947 + return ck;
5948 +
5949 +}
5950 +
5951 +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
5952 +{
5953 + uint8_t *retval = NULL;
5954 + dwc_cc_t *cc;
5955 +
5956 + DWC_MUTEX_LOCK(cc_if->mutex);
5957 + cc = cc_find(cc_if, id);
5958 + if (cc) {
5959 + retval = cc->chid;
5960 + }
5961 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5962 +
5963 + return retval;
5964 +}
5965 +
5966 +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
5967 +{
5968 + uint8_t *retval = NULL;
5969 + dwc_cc_t *cc;
5970 +
5971 + DWC_MUTEX_LOCK(cc_if->mutex);
5972 + cc = cc_find(cc_if, id);
5973 + if (cc) {
5974 + retval = cc->cdid;
5975 + }
5976 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5977 +
5978 + return retval;
5979 +}
5980 +
5981 +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
5982 +{
5983 + uint8_t *retval = NULL;
5984 + dwc_cc_t *cc;
5985 +
5986 + DWC_MUTEX_LOCK(cc_if->mutex);
5987 + *length = 0;
5988 + cc = cc_find(cc_if, id);
5989 + if (cc) {
5990 + *length = cc->length;
5991 + retval = cc->name;
5992 + }
5993 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5994 +
5995 + return retval;
5996 +}
5997 +
5998 +#endif /* DWC_CCLIB */
5999 --- /dev/null
6000 +++ b/drivers/usb/host/dwc_common_port/dwc_cc.h
6001 @@ -0,0 +1,224 @@
6002 +/* =========================================================================
6003 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
6004 + * $Revision: #4 $
6005 + * $Date: 2010/09/28 $
6006 + * $Change: 1596182 $
6007 + *
6008 + * Synopsys Portability Library Software and documentation
6009 + * (hereinafter, "Software") is an Unsupported proprietary work of
6010 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
6011 + * between Synopsys and you.
6012 + *
6013 + * The Software IS NOT an item of Licensed Software or Licensed Product
6014 + * under any End User Software License Agreement or Agreement for
6015 + * Licensed Product with Synopsys or any supplement thereto. You are
6016 + * permitted to use and redistribute this Software in source and binary
6017 + * forms, with or without modification, provided that redistributions
6018 + * of source code must retain this notice. You may not view, use,
6019 + * disclose, copy or distribute this file or any information contained
6020 + * herein except pursuant to this license grant from Synopsys. If you
6021 + * do not agree with this notice, including the disclaimer below, then
6022 + * you are not authorized to use the Software.
6023 + *
6024 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
6025 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6026 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
6027 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
6028 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
6029 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
6030 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
6031 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
6032 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6033 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
6034 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
6035 + * DAMAGE.
6036 + * ========================================================================= */
6037 +#ifndef _DWC_CC_H_
6038 +#define _DWC_CC_H_
6039 +
6040 +#ifdef __cplusplus
6041 +extern "C" {
6042 +#endif
6043 +
6044 +/** @file
6045 + *
6046 + * This file defines the Context Context library.
6047 + *
6048 + * The main data structure is dwc_cc_if_t which is returned by either the
6049 + * dwc_cc_if_alloc function or returned by the module to the user via a provided
6050 + * function. The data structure is opaque and should only be manipulated via the
6051 + * functions provied in this API.
6052 + *
6053 + * It manages a list of connection contexts and operations can be performed to
6054 + * add, remove, query, search, and change, those contexts. Additionally,
6055 + * a dwc_notifier_t object can be requested from the manager so that
6056 + * the user can be notified whenever the context list has changed.
6057 + */
6058 +
6059 +#include "dwc_os.h"
6060 +#include "dwc_list.h"
6061 +#include "dwc_notifier.h"
6062 +
6063 +
6064 +/* Notifications */
6065 +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
6066 +
6067 +struct dwc_cc_if;
6068 +typedef struct dwc_cc_if dwc_cc_if_t;
6069 +
6070 +
6071 +/** @name Connection Context Operations */
6072 +/** @{ */
6073 +
6074 +/** This function allocates memory for a dwc_cc_if_t structure, initializes
6075 + * fields to default values, and returns a pointer to the structure or NULL on
6076 + * error. */
6077 +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
6078 + dwc_notifier_t *notifier, unsigned is_host);
6079 +
6080 +/** Frees the memory for the specified CC structure allocated from
6081 + * dwc_cc_if_alloc(). */
6082 +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
6083 +
6084 +/** Removes all contexts from the connection context list */
6085 +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
6086 +
6087 +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
6088 + * If a CHID already exists, the CK and name are overwritten. Statistics are
6089 + * not overwritten.
6090 + *
6091 + * @param cc_if The cc_if structure.
6092 + * @param chid A pointer to the 16-byte CHID. This value will be copied.
6093 + * @param ck A pointer to the 16-byte CK. This value will be copied.
6094 + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
6095 + * @param name An optional host friendly name as defined in the association model
6096 + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
6097 + * @param length The length othe unicode string.
6098 + * @return A unique identifier used to refer to this context that is valid for
6099 + * as long as this context is still in the list. */
6100 +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
6101 + uint8_t *cdid, uint8_t *ck, uint8_t *name,
6102 + uint8_t length);
6103 +
6104 +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
6105 + * list, preserving any accumulated statistics. This would typically be called
6106 + * if the host decideds to change the context with a SET_CONNECTION request.
6107 + *
6108 + * @param cc_if The cc_if structure.
6109 + * @param id The identifier of the connection context.
6110 + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
6111 + * indicates no change.
6112 + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
6113 + * indicates no change.
6114 + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
6115 + * indicates no change.
6116 + * @param name Host friendly name UTF16-LE. NULL indicates no change.
6117 + * @param length Length of name. */
6118 +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
6119 + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
6120 + uint8_t *name, uint8_t length);
6121 +
6122 +/** Remove the specified connection context.
6123 + * @param cc_if The cc_if structure.
6124 + * @param id The identifier of the connection context to remove. */
6125 +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
6126 +
6127 +/** Get a binary block of data for the connection context list and attributes.
6128 + * This data can be used by the OS specific driver to save the connection
6129 + * context list into non-volatile memory.
6130 + *
6131 + * @param cc_if The cc_if structure.
6132 + * @param length Return the length of the data buffer.
6133 + * @return A pointer to the data buffer. The memory for this buffer should be
6134 + * freed with DWC_FREE() after use. */
6135 +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
6136 + unsigned int *length);
6137 +
6138 +/** Restore the connection context list from the binary data that was previously
6139 + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
6140 + * driver to load a connection context list from non-volatile memory.
6141 + *
6142 + * @param cc_if The cc_if structure.
6143 + * @param data The data bytes as returned from dwc_cc_data_for_save.
6144 + * @param length The length of the data. */
6145 +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
6146 + uint8_t *data, unsigned int length);
6147 +
6148 +/** Find the connection context from the specified CHID.
6149 + *
6150 + * @param cc_if The cc_if structure.
6151 + * @param chid A pointer to the CHID data.
6152 + * @return A non-zero identifier of the connection context if the CHID matches.
6153 + * Otherwise returns 0. */
6154 +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
6155 +
6156 +/** Find the connection context from the specified CDID.
6157 + *
6158 + * @param cc_if The cc_if structure.
6159 + * @param cdid A pointer to the CDID data.
6160 + * @return A non-zero identifier of the connection context if the CHID matches.
6161 + * Otherwise returns 0. */
6162 +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
6163 +
6164 +/** Retrieve the CK from the specified connection context.
6165 + *
6166 + * @param cc_if The cc_if structure.
6167 + * @param id The identifier of the connection context.
6168 + * @return A pointer to the CK data. The memory does not need to be freed. */
6169 +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
6170 +
6171 +/** Retrieve the CHID from the specified connection context.
6172 + *
6173 + * @param cc_if The cc_if structure.
6174 + * @param id The identifier of the connection context.
6175 + * @return A pointer to the CHID data. The memory does not need to be freed. */
6176 +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
6177 +
6178 +/** Retrieve the CDID from the specified connection context.
6179 + *
6180 + * @param cc_if The cc_if structure.
6181 + * @param id The identifier of the connection context.
6182 + * @return A pointer to the CDID data. The memory does not need to be freed. */
6183 +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
6184 +
6185 +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
6186 +
6187 +/** Checks a buffer for non-zero.
6188 + * @param id A pointer to a 16 byte buffer.
6189 + * @return true if the 16 byte value is non-zero. */
6190 +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
6191 + int i;
6192 + for (i=0; i<16; i++) {
6193 + if (id[i]) return 1;
6194 + }
6195 + return 0;
6196 +}
6197 +
6198 +/** Checks a buffer for zero.
6199 + * @param id A pointer to a 16 byte buffer.
6200 + * @return true if the 16 byte value is zero. */
6201 +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
6202 + return !dwc_assoc_is_not_zero_id(id);
6203 +}
6204 +
6205 +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
6206 + * buffer. */
6207 +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
6208 + char *ptr = buffer;
6209 + int i;
6210 + for (i=0; i<16; i++) {
6211 + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
6212 + if (i < 15) {
6213 + ptr += DWC_SPRINTF(ptr, " ");
6214 + }
6215 + }
6216 + return ptr - buffer;
6217 +}
6218 +
6219 +/** @} */
6220 +
6221 +#ifdef __cplusplus
6222 +}
6223 +#endif
6224 +
6225 +#endif /* _DWC_CC_H_ */
6226 --- /dev/null
6227 +++ b/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
6228 @@ -0,0 +1,1308 @@
6229 +#include "dwc_os.h"
6230 +#include "dwc_list.h"
6231 +
6232 +#ifdef DWC_CCLIB
6233 +# include "dwc_cc.h"
6234 +#endif
6235 +
6236 +#ifdef DWC_CRYPTOLIB
6237 +# include "dwc_modpow.h"
6238 +# include "dwc_dh.h"
6239 +# include "dwc_crypto.h"
6240 +#endif
6241 +
6242 +#ifdef DWC_NOTIFYLIB
6243 +# include "dwc_notifier.h"
6244 +#endif
6245 +
6246 +/* OS-Level Implementations */
6247 +
6248 +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
6249 +
6250 +
6251 +/* MISC */
6252 +
6253 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
6254 +{
6255 + return memset(dest, byte, size);
6256 +}
6257 +
6258 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
6259 +{
6260 + return memcpy(dest, src, size);
6261 +}
6262 +
6263 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
6264 +{
6265 + bcopy(src, dest, size);
6266 + return dest;
6267 +}
6268 +
6269 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
6270 +{
6271 + return memcmp(m1, m2, size);
6272 +}
6273 +
6274 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
6275 +{
6276 + return strncmp(s1, s2, size);
6277 +}
6278 +
6279 +int DWC_STRCMP(void *s1, void *s2)
6280 +{
6281 + return strcmp(s1, s2);
6282 +}
6283 +
6284 +int DWC_STRLEN(char const *str)
6285 +{
6286 + return strlen(str);
6287 +}
6288 +
6289 +char *DWC_STRCPY(char *to, char const *from)
6290 +{
6291 + return strcpy(to, from);
6292 +}
6293 +
6294 +char *DWC_STRDUP(char const *str)
6295 +{
6296 + int len = DWC_STRLEN(str) + 1;
6297 + char *new = DWC_ALLOC_ATOMIC(len);
6298 +
6299 + if (!new) {
6300 + return NULL;
6301 + }
6302 +
6303 + DWC_MEMCPY(new, str, len);
6304 + return new;
6305 +}
6306 +
6307 +int DWC_ATOI(char *str, int32_t *value)
6308 +{
6309 + char *end = NULL;
6310 +
6311 + *value = strtol(str, &end, 0);
6312 + if (*end == '\0') {
6313 + return 0;
6314 + }
6315 +
6316 + return -1;
6317 +}
6318 +
6319 +int DWC_ATOUI(char *str, uint32_t *value)
6320 +{
6321 + char *end = NULL;
6322 +
6323 + *value = strtoul(str, &end, 0);
6324 + if (*end == '\0') {
6325 + return 0;
6326 + }
6327 +
6328 + return -1;
6329 +}
6330 +
6331 +
6332 +#ifdef DWC_UTFLIB
6333 +/* From usbstring.c */
6334 +
6335 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
6336 +{
6337 + int count = 0;
6338 + u8 c;
6339 + u16 uchar;
6340 +
6341 + /* this insists on correct encodings, though not minimal ones.
6342 + * BUT it currently rejects legit 4-byte UTF-8 code points,
6343 + * which need surrogate pairs. (Unicode 3.1 can use them.)
6344 + */
6345 + while (len != 0 && (c = (u8) *s++) != 0) {
6346 + if (unlikely(c & 0x80)) {
6347 + // 2-byte sequence:
6348 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
6349 + if ((c & 0xe0) == 0xc0) {
6350 + uchar = (c & 0x1f) << 6;
6351 +
6352 + c = (u8) *s++;
6353 + if ((c & 0xc0) != 0xc0)
6354 + goto fail;
6355 + c &= 0x3f;
6356 + uchar |= c;
6357 +
6358 + // 3-byte sequence (most CJKV characters):
6359 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
6360 + } else if ((c & 0xf0) == 0xe0) {
6361 + uchar = (c & 0x0f) << 12;
6362 +
6363 + c = (u8) *s++;
6364 + if ((c & 0xc0) != 0xc0)
6365 + goto fail;
6366 + c &= 0x3f;
6367 + uchar |= c << 6;
6368 +
6369 + c = (u8) *s++;
6370 + if ((c & 0xc0) != 0xc0)
6371 + goto fail;
6372 + c &= 0x3f;
6373 + uchar |= c;
6374 +
6375 + /* no bogus surrogates */
6376 + if (0xd800 <= uchar && uchar <= 0xdfff)
6377 + goto fail;
6378 +
6379 + // 4-byte sequence (surrogate pairs, currently rare):
6380 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
6381 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
6382 + // (uuuuu = wwww + 1)
6383 + // FIXME accept the surrogate code points (only)
6384 + } else
6385 + goto fail;
6386 + } else
6387 + uchar = c;
6388 + put_unaligned (cpu_to_le16 (uchar), cp++);
6389 + count++;
6390 + len--;
6391 + }
6392 + return count;
6393 +fail:
6394 + return -1;
6395 +}
6396 +
6397 +#endif /* DWC_UTFLIB */
6398 +
6399 +
6400 +/* dwc_debug.h */
6401 +
6402 +dwc_bool_t DWC_IN_IRQ(void)
6403 +{
6404 +// return in_irq();
6405 + return 0;
6406 +}
6407 +
6408 +dwc_bool_t DWC_IN_BH(void)
6409 +{
6410 +// return in_softirq();
6411 + return 0;
6412 +}
6413 +
6414 +void DWC_VPRINTF(char *format, va_list args)
6415 +{
6416 + vprintf(format, args);
6417 +}
6418 +
6419 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
6420 +{
6421 + return vsnprintf(str, size, format, args);
6422 +}
6423 +
6424 +void DWC_PRINTF(char *format, ...)
6425 +{
6426 + va_list args;
6427 +
6428 + va_start(args, format);
6429 + DWC_VPRINTF(format, args);
6430 + va_end(args);
6431 +}
6432 +
6433 +int DWC_SPRINTF(char *buffer, char *format, ...)
6434 +{
6435 + int retval;
6436 + va_list args;
6437 +
6438 + va_start(args, format);
6439 + retval = vsprintf(buffer, format, args);
6440 + va_end(args);
6441 + return retval;
6442 +}
6443 +
6444 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
6445 +{
6446 + int retval;
6447 + va_list args;
6448 +
6449 + va_start(args, format);
6450 + retval = vsnprintf(buffer, size, format, args);
6451 + va_end(args);
6452 + return retval;
6453 +}
6454 +
6455 +void __DWC_WARN(char *format, ...)
6456 +{
6457 + va_list args;
6458 +
6459 + va_start(args, format);
6460 + DWC_VPRINTF(format, args);
6461 + va_end(args);
6462 +}
6463 +
6464 +void __DWC_ERROR(char *format, ...)
6465 +{
6466 + va_list args;
6467 +
6468 + va_start(args, format);
6469 + DWC_VPRINTF(format, args);
6470 + va_end(args);
6471 +}
6472 +
6473 +void DWC_EXCEPTION(char *format, ...)
6474 +{
6475 + va_list args;
6476 +
6477 + va_start(args, format);
6478 + DWC_VPRINTF(format, args);
6479 + va_end(args);
6480 +// BUG_ON(1); ???
6481 +}
6482 +
6483 +#ifdef DEBUG
6484 +void __DWC_DEBUG(char *format, ...)
6485 +{
6486 + va_list args;
6487 +
6488 + va_start(args, format);
6489 + DWC_VPRINTF(format, args);
6490 + va_end(args);
6491 +}
6492 +#endif
6493 +
6494 +
6495 +/* dwc_mem.h */
6496 +
6497 +#if 0
6498 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
6499 + uint32_t align,
6500 + uint32_t alloc)
6501 +{
6502 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
6503 + size, align, alloc);
6504 + return (dwc_pool_t *)pool;
6505 +}
6506 +
6507 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
6508 +{
6509 + dma_pool_destroy((struct dma_pool *)pool);
6510 +}
6511 +
6512 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
6513 +{
6514 +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
6515 + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
6516 +}
6517 +
6518 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
6519 +{
6520 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
6521 + memset(..);
6522 +}
6523 +
6524 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
6525 +{
6526 + dma_pool_free(pool, vaddr, daddr);
6527 +}
6528 +#endif
6529 +
6530 +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
6531 +{
6532 + if (error)
6533 + return;
6534 + *(bus_addr_t *)arg = segs[0].ds_addr;
6535 +}
6536 +
6537 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
6538 +{
6539 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
6540 + int error;
6541 +
6542 + error = bus_dma_tag_create(
6543 +#if __FreeBSD_version >= 700000
6544 + bus_get_dma_tag(dma->dev), /* parent */
6545 +#else
6546 + NULL, /* parent */
6547 +#endif
6548 + 4, 0, /* alignment, bounds */
6549 + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
6550 + BUS_SPACE_MAXADDR, /* highaddr */
6551 + NULL, NULL, /* filter, filterarg */
6552 + size, /* maxsize */
6553 + 1, /* nsegments */
6554 + size, /* maxsegsize */
6555 + 0, /* flags */
6556 + NULL, /* lockfunc */
6557 + NULL, /* lockarg */
6558 + &dma->dma_tag);
6559 + if (error) {
6560 + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
6561 + __func__, error);
6562 + goto fail_0;
6563 + }
6564 +
6565 + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
6566 + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
6567 + if (error) {
6568 + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
6569 + __func__, (uintmax_t)size, error);
6570 + goto fail_1;
6571 + }
6572 +
6573 + dma->dma_paddr = 0;
6574 + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
6575 + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
6576 + if (error || dma->dma_paddr == 0) {
6577 + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
6578 + __func__, error);
6579 + goto fail_2;
6580 + }
6581 +
6582 + *dma_addr = dma->dma_paddr;
6583 + return dma->dma_vaddr;
6584 +
6585 +fail_2:
6586 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
6587 +fail_1:
6588 + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
6589 + bus_dma_tag_destroy(dma->dma_tag);
6590 +fail_0:
6591 + dma->dma_map = NULL;
6592 + dma->dma_tag = NULL;
6593 +
6594 + return NULL;
6595 +}
6596 +
6597 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
6598 +{
6599 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
6600 +
6601 + if (dma->dma_tag == NULL)
6602 + return;
6603 + if (dma->dma_map != NULL) {
6604 + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
6605 + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6606 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
6607 + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
6608 + dma->dma_map = NULL;
6609 + }
6610 +
6611 + bus_dma_tag_destroy(dma->dma_tag);
6612 + dma->dma_tag = NULL;
6613 +}
6614 +
6615 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
6616 +{
6617 + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
6618 +}
6619 +
6620 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
6621 +{
6622 + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6623 +}
6624 +
6625 +void __DWC_FREE(void *mem_ctx, void *addr)
6626 +{
6627 + free(addr, M_DEVBUF);
6628 +}
6629 +
6630 +
6631 +#ifdef DWC_CRYPTOLIB
6632 +/* dwc_crypto.h */
6633 +
6634 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
6635 +{
6636 + get_random_bytes(buffer, length);
6637 +}
6638 +
6639 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
6640 +{
6641 + struct crypto_blkcipher *tfm;
6642 + struct blkcipher_desc desc;
6643 + struct scatterlist sgd;
6644 + struct scatterlist sgs;
6645 +
6646 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
6647 + if (tfm == NULL) {
6648 + printk("failed to load transform for aes CBC\n");
6649 + return -1;
6650 + }
6651 +
6652 + crypto_blkcipher_setkey(tfm, key, keylen);
6653 + crypto_blkcipher_set_iv(tfm, iv, 16);
6654 +
6655 + sg_init_one(&sgd, out, messagelen);
6656 + sg_init_one(&sgs, message, messagelen);
6657 +
6658 + desc.tfm = tfm;
6659 + desc.flags = 0;
6660 +
6661 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
6662 + crypto_free_blkcipher(tfm);
6663 + DWC_ERROR("AES CBC encryption failed");
6664 + return -1;
6665 + }
6666 +
6667 + crypto_free_blkcipher(tfm);
6668 + return 0;
6669 +}
6670 +
6671 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
6672 +{
6673 + struct crypto_hash *tfm;
6674 + struct hash_desc desc;
6675 + struct scatterlist sg;
6676 +
6677 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
6678 + if (IS_ERR(tfm)) {
6679 + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
6680 + return 0;
6681 + }
6682 + desc.tfm = tfm;
6683 + desc.flags = 0;
6684 +
6685 + sg_init_one(&sg, message, len);
6686 + crypto_hash_digest(&desc, &sg, len, out);
6687 + crypto_free_hash(tfm);
6688 +
6689 + return 1;
6690 +}
6691 +
6692 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
6693 + uint8_t *key, uint32_t keylen, uint8_t *out)
6694 +{
6695 + struct crypto_hash *tfm;
6696 + struct hash_desc desc;
6697 + struct scatterlist sg;
6698 +
6699 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
6700 + if (IS_ERR(tfm)) {
6701 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
6702 + return 0;
6703 + }
6704 + desc.tfm = tfm;
6705 + desc.flags = 0;
6706 +
6707 + sg_init_one(&sg, message, messagelen);
6708 + crypto_hash_setkey(tfm, key, keylen);
6709 + crypto_hash_digest(&desc, &sg, messagelen, out);
6710 + crypto_free_hash(tfm);
6711 +
6712 + return 1;
6713 +}
6714 +
6715 +#endif /* DWC_CRYPTOLIB */
6716 +
6717 +
6718 +/* Byte Ordering Conversions */
6719 +
6720 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
6721 +{
6722 +#ifdef __LITTLE_ENDIAN
6723 + return *p;
6724 +#else
6725 + uint8_t *u_p = (uint8_t *)p;
6726 +
6727 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6728 +#endif
6729 +}
6730 +
6731 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
6732 +{
6733 +#ifdef __BIG_ENDIAN
6734 + return *p;
6735 +#else
6736 + uint8_t *u_p = (uint8_t *)p;
6737 +
6738 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6739 +#endif
6740 +}
6741 +
6742 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
6743 +{
6744 +#ifdef __LITTLE_ENDIAN
6745 + return *p;
6746 +#else
6747 + uint8_t *u_p = (uint8_t *)p;
6748 +
6749 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6750 +#endif
6751 +}
6752 +
6753 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
6754 +{
6755 +#ifdef __BIG_ENDIAN
6756 + return *p;
6757 +#else
6758 + uint8_t *u_p = (uint8_t *)p;
6759 +
6760 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6761 +#endif
6762 +}
6763 +
6764 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
6765 +{
6766 +#ifdef __LITTLE_ENDIAN
6767 + return *p;
6768 +#else
6769 + uint8_t *u_p = (uint8_t *)p;
6770 + return (u_p[1] | (u_p[0] << 8));
6771 +#endif
6772 +}
6773 +
6774 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
6775 +{
6776 +#ifdef __BIG_ENDIAN
6777 + return *p;
6778 +#else
6779 + uint8_t *u_p = (uint8_t *)p;
6780 + return (u_p[1] | (u_p[0] << 8));
6781 +#endif
6782 +}
6783 +
6784 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
6785 +{
6786 +#ifdef __LITTLE_ENDIAN
6787 + return *p;
6788 +#else
6789 + uint8_t *u_p = (uint8_t *)p;
6790 + return (u_p[1] | (u_p[0] << 8));
6791 +#endif
6792 +}
6793 +
6794 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
6795 +{
6796 +#ifdef __BIG_ENDIAN
6797 + return *p;
6798 +#else
6799 + uint8_t *u_p = (uint8_t *)p;
6800 + return (u_p[1] | (u_p[0] << 8));
6801 +#endif
6802 +}
6803 +
6804 +
6805 +/* Registers */
6806 +
6807 +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
6808 +{
6809 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6810 + bus_size_t ior = (bus_size_t)reg;
6811 +
6812 + return bus_space_read_4(io->iot, io->ioh, ior);
6813 +}
6814 +
6815 +#if 0
6816 +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
6817 +{
6818 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6819 + bus_size_t ior = (bus_size_t)reg;
6820 +
6821 + return bus_space_read_8(io->iot, io->ioh, ior);
6822 +}
6823 +#endif
6824 +
6825 +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
6826 +{
6827 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6828 + bus_size_t ior = (bus_size_t)reg;
6829 +
6830 + bus_space_write_4(io->iot, io->ioh, ior, value);
6831 +}
6832 +
6833 +#if 0
6834 +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
6835 +{
6836 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6837 + bus_size_t ior = (bus_size_t)reg;
6838 +
6839 + bus_space_write_8(io->iot, io->ioh, ior, value);
6840 +}
6841 +#endif
6842 +
6843 +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
6844 + uint32_t set_mask)
6845 +{
6846 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6847 + bus_size_t ior = (bus_size_t)reg;
6848 +
6849 + bus_space_write_4(io->iot, io->ioh, ior,
6850 + (bus_space_read_4(io->iot, io->ioh, ior) &
6851 + ~clear_mask) | set_mask);
6852 +}
6853 +
6854 +#if 0
6855 +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
6856 + uint64_t set_mask)
6857 +{
6858 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6859 + bus_size_t ior = (bus_size_t)reg;
6860 +
6861 + bus_space_write_8(io->iot, io->ioh, ior,
6862 + (bus_space_read_8(io->iot, io->ioh, ior) &
6863 + ~clear_mask) | set_mask);
6864 +}
6865 +#endif
6866 +
6867 +
6868 +/* Locking */
6869 +
6870 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
6871 +{
6872 + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
6873 +
6874 + if (!sl) {
6875 + DWC_ERROR("Cannot allocate memory for spinlock");
6876 + return NULL;
6877 + }
6878 +
6879 + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
6880 + return (dwc_spinlock_t *)sl;
6881 +}
6882 +
6883 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
6884 +{
6885 + struct mtx *sl = (struct mtx *)lock;
6886 +
6887 + mtx_destroy(sl);
6888 + DWC_FREE(sl);
6889 +}
6890 +
6891 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
6892 +{
6893 + mtx_lock_spin((struct mtx *)lock); // ???
6894 +}
6895 +
6896 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
6897 +{
6898 + mtx_unlock_spin((struct mtx *)lock); // ???
6899 +}
6900 +
6901 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
6902 +{
6903 + mtx_lock_spin((struct mtx *)lock);
6904 +}
6905 +
6906 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
6907 +{
6908 + mtx_unlock_spin((struct mtx *)lock);
6909 +}
6910 +
6911 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
6912 +{
6913 + struct mtx *m;
6914 + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
6915 +
6916 + if (!mutex) {
6917 + DWC_ERROR("Cannot allocate memory for mutex");
6918 + return NULL;
6919 + }
6920 +
6921 + m = (struct mtx *)mutex;
6922 + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
6923 + return mutex;
6924 +}
6925 +
6926 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
6927 +#else
6928 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
6929 +{
6930 + mtx_destroy((struct mtx *)mutex);
6931 + DWC_FREE(mutex);
6932 +}
6933 +#endif
6934 +
6935 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
6936 +{
6937 + struct mtx *m = (struct mtx *)mutex;
6938 +
6939 + mtx_lock(m);
6940 +}
6941 +
6942 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
6943 +{
6944 + struct mtx *m = (struct mtx *)mutex;
6945 +
6946 + return mtx_trylock(m);
6947 +}
6948 +
6949 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
6950 +{
6951 + struct mtx *m = (struct mtx *)mutex;
6952 +
6953 + mtx_unlock(m);
6954 +}
6955 +
6956 +
6957 +/* Timing */
6958 +
6959 +void DWC_UDELAY(uint32_t usecs)
6960 +{
6961 + DELAY(usecs);
6962 +}
6963 +
6964 +void DWC_MDELAY(uint32_t msecs)
6965 +{
6966 + do {
6967 + DELAY(1000);
6968 + } while (--msecs);
6969 +}
6970 +
6971 +void DWC_MSLEEP(uint32_t msecs)
6972 +{
6973 + struct timeval tv;
6974 +
6975 + tv.tv_sec = msecs / 1000;
6976 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
6977 + pause("dw3slp", tvtohz(&tv));
6978 +}
6979 +
6980 +uint32_t DWC_TIME(void)
6981 +{
6982 + struct timeval tv;
6983 +
6984 + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
6985 + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
6986 +}
6987 +
6988 +
6989 +/* Timers */
6990 +
6991 +struct dwc_timer {
6992 + struct callout t;
6993 + char *name;
6994 + dwc_spinlock_t *lock;
6995 + dwc_timer_callback_t cb;
6996 + void *data;
6997 +};
6998 +
6999 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
7000 +{
7001 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
7002 +
7003 + if (!t) {
7004 + DWC_ERROR("Cannot allocate memory for timer");
7005 + return NULL;
7006 + }
7007 +
7008 + callout_init(&t->t, 1);
7009 +
7010 + t->name = DWC_STRDUP(name);
7011 + if (!t->name) {
7012 + DWC_ERROR("Cannot allocate memory for timer->name");
7013 + goto no_name;
7014 + }
7015 +
7016 + t->lock = DWC_SPINLOCK_ALLOC();
7017 + if (!t->lock) {
7018 + DWC_ERROR("Cannot allocate memory for lock");
7019 + goto no_lock;
7020 + }
7021 +
7022 + t->cb = cb;
7023 + t->data = data;
7024 +
7025 + return t;
7026 +
7027 + no_lock:
7028 + DWC_FREE(t->name);
7029 + no_name:
7030 + DWC_FREE(t);
7031 +
7032 + return NULL;
7033 +}
7034 +
7035 +void DWC_TIMER_FREE(dwc_timer_t *timer)
7036 +{
7037 + callout_stop(&timer->t);
7038 + DWC_SPINLOCK_FREE(timer->lock);
7039 + DWC_FREE(timer->name);
7040 + DWC_FREE(timer);
7041 +}
7042 +
7043 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
7044 +{
7045 + struct timeval tv;
7046 +
7047 + tv.tv_sec = time / 1000;
7048 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
7049 + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
7050 +}
7051 +
7052 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
7053 +{
7054 + callout_stop(&timer->t);
7055 +}
7056 +
7057 +
7058 +/* Wait Queues */
7059 +
7060 +struct dwc_waitq {
7061 + struct mtx lock;
7062 + int abort;
7063 +};
7064 +
7065 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
7066 +{
7067 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
7068 +
7069 + if (!wq) {
7070 + DWC_ERROR("Cannot allocate memory for waitqueue");
7071 + return NULL;
7072 + }
7073 +
7074 + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
7075 + wq->abort = 0;
7076 +
7077 + return wq;
7078 +}
7079 +
7080 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
7081 +{
7082 + mtx_destroy(&wq->lock);
7083 + DWC_FREE(wq);
7084 +}
7085 +
7086 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
7087 +{
7088 +// intrmask_t ipl;
7089 + int result = 0;
7090 +
7091 + mtx_lock(&wq->lock);
7092 +// ipl = splbio();
7093 +
7094 + /* Skip the sleep if already aborted or triggered */
7095 + if (!wq->abort && !cond(data)) {
7096 +// splx(ipl);
7097 + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
7098 +// ipl = splbio();
7099 + }
7100 +
7101 + if (result == ERESTART) { // signaled - restart
7102 + result = -DWC_E_RESTART;
7103 +
7104 + } else if (result == EINTR) { // signaled - interrupt
7105 + result = -DWC_E_ABORT;
7106 +
7107 + } else if (wq->abort) {
7108 + result = -DWC_E_ABORT;
7109 +
7110 + } else {
7111 + result = 0;
7112 + }
7113 +
7114 + wq->abort = 0;
7115 +// splx(ipl);
7116 + mtx_unlock(&wq->lock);
7117 + return result;
7118 +}
7119 +
7120 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
7121 + void *data, int32_t msecs)
7122 +{
7123 + struct timeval tv, tv1, tv2;
7124 +// intrmask_t ipl;
7125 + int result = 0;
7126 +
7127 + tv.tv_sec = msecs / 1000;
7128 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
7129 +
7130 + mtx_lock(&wq->lock);
7131 +// ipl = splbio();
7132 +
7133 + /* Skip the sleep if already aborted or triggered */
7134 + if (!wq->abort && !cond(data)) {
7135 +// splx(ipl);
7136 + getmicrouptime(&tv1);
7137 + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
7138 + getmicrouptime(&tv2);
7139 +// ipl = splbio();
7140 + }
7141 +
7142 + if (result == 0) { // awoken
7143 + if (wq->abort) {
7144 + result = -DWC_E_ABORT;
7145 + } else {
7146 + tv2.tv_usec -= tv1.tv_usec;
7147 + if (tv2.tv_usec < 0) {
7148 + tv2.tv_usec += 1000000;
7149 + tv2.tv_sec--;
7150 + }
7151 +
7152 + tv2.tv_sec -= tv1.tv_sec;
7153 + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
7154 + result = msecs - result;
7155 + if (result <= 0)
7156 + result = 1;
7157 + }
7158 + } else if (result == ERESTART) { // signaled - restart
7159 + result = -DWC_E_RESTART;
7160 +
7161 + } else if (result == EINTR) { // signaled - interrupt
7162 + result = -DWC_E_ABORT;
7163 +
7164 + } else { // timed out
7165 + result = -DWC_E_TIMEOUT;
7166 + }
7167 +
7168 + wq->abort = 0;
7169 +// splx(ipl);
7170 + mtx_unlock(&wq->lock);
7171 + return result;
7172 +}
7173 +
7174 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
7175 +{
7176 + wakeup(wq);
7177 +}
7178 +
7179 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
7180 +{
7181 +// intrmask_t ipl;
7182 +
7183 + mtx_lock(&wq->lock);
7184 +// ipl = splbio();
7185 + wq->abort = 1;
7186 + wakeup(wq);
7187 +// splx(ipl);
7188 + mtx_unlock(&wq->lock);
7189 +}
7190 +
7191 +
7192 +/* Threading */
7193 +
7194 +struct dwc_thread {
7195 + struct proc *proc;
7196 + int abort;
7197 +};
7198 +
7199 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
7200 +{
7201 + int retval;
7202 + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
7203 +
7204 + if (!thread) {
7205 + return NULL;
7206 + }
7207 +
7208 + thread->abort = 0;
7209 + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
7210 + RFPROC | RFNOWAIT, 0, "%s", name);
7211 + if (retval) {
7212 + DWC_FREE(thread);
7213 + return NULL;
7214 + }
7215 +
7216 + return thread;
7217 +}
7218 +
7219 +int DWC_THREAD_STOP(dwc_thread_t *thread)
7220 +{
7221 + int retval;
7222 +
7223 + thread->abort = 1;
7224 + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
7225 +
7226 + if (retval == 0) {
7227 + /* DWC_THREAD_EXIT() will free the thread struct */
7228 + return 0;
7229 + }
7230 +
7231 + /* NOTE: We leak the thread struct if thread doesn't die */
7232 +
7233 + if (retval == EWOULDBLOCK) {
7234 + return -DWC_E_TIMEOUT;
7235 + }
7236 +
7237 + return -DWC_E_UNKNOWN;
7238 +}
7239 +
7240 +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
7241 +{
7242 + return thread->abort;
7243 +}
7244 +
7245 +void DWC_THREAD_EXIT(dwc_thread_t *thread)
7246 +{
7247 + wakeup(&thread->abort);
7248 + DWC_FREE(thread);
7249 + kthread_exit(0);
7250 +}
7251 +
7252 +
7253 +/* tasklets
7254 + - Runs in interrupt context (cannot sleep)
7255 + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
7256 + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
7257 + */
7258 +struct dwc_tasklet {
7259 + struct task t;
7260 + dwc_tasklet_callback_t cb;
7261 + void *data;
7262 +};
7263 +
7264 +static void tasklet_callback(void *data, int pending) // what to do with pending ???
7265 +{
7266 + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
7267 +
7268 + task->cb(task->data);
7269 +}
7270 +
7271 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
7272 +{
7273 + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
7274 +
7275 + if (task) {
7276 + task->cb = cb;
7277 + task->data = data;
7278 + TASK_INIT(&task->t, 0, tasklet_callback, task);
7279 + } else {
7280 + DWC_ERROR("Cannot allocate memory for tasklet");
7281 + }
7282 +
7283 + return task;
7284 +}
7285 +
7286 +void DWC_TASK_FREE(dwc_tasklet_t *task)
7287 +{
7288 + taskqueue_drain(taskqueue_fast, &task->t); // ???
7289 + DWC_FREE(task);
7290 +}
7291 +
7292 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
7293 +{
7294 + /* Uses predefined system queue */
7295 + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
7296 +}
7297 +
7298 +
7299 +/* workqueues
7300 + - Runs in process context (can sleep)
7301 + */
7302 +typedef struct work_container {
7303 + dwc_work_callback_t cb;
7304 + void *data;
7305 + dwc_workq_t *wq;
7306 + char *name;
7307 + int hz;
7308 +
7309 +#ifdef DEBUG
7310 + DWC_CIRCLEQ_ENTRY(work_container) entry;
7311 +#endif
7312 + struct task task;
7313 +} work_container_t;
7314 +
7315 +#ifdef DEBUG
7316 +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
7317 +#endif
7318 +
7319 +struct dwc_workq {
7320 + struct taskqueue *taskq;
7321 + dwc_spinlock_t *lock;
7322 + dwc_waitq_t *waitq;
7323 + int pending;
7324 +
7325 +#ifdef DEBUG
7326 + struct work_container_queue entries;
7327 +#endif
7328 +};
7329 +
7330 +static void do_work(void *data, int pending) // what to do with pending ???
7331 +{
7332 + work_container_t *container = (work_container_t *)data;
7333 + dwc_workq_t *wq = container->wq;
7334 + dwc_irqflags_t flags;
7335 +
7336 + if (container->hz) {
7337 + pause("dw3wrk", container->hz);
7338 + }
7339 +
7340 + container->cb(container->data);
7341 + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
7342 +
7343 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7344 +
7345 +#ifdef DEBUG
7346 + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
7347 +#endif
7348 + if (container->name)
7349 + DWC_FREE(container->name);
7350 + DWC_FREE(container);
7351 + wq->pending--;
7352 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7353 + DWC_WAITQ_TRIGGER(wq->waitq);
7354 +}
7355 +
7356 +static int work_done(void *data)
7357 +{
7358 + dwc_workq_t *workq = (dwc_workq_t *)data;
7359 +
7360 + return workq->pending == 0;
7361 +}
7362 +
7363 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
7364 +{
7365 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
7366 +}
7367 +
7368 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
7369 +{
7370 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
7371 +
7372 + if (!wq) {
7373 + DWC_ERROR("Cannot allocate memory for workqueue");
7374 + return NULL;
7375 + }
7376 +
7377 + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
7378 + if (!wq->taskq) {
7379 + DWC_ERROR("Cannot allocate memory for taskqueue");
7380 + goto no_taskq;
7381 + }
7382 +
7383 + wq->pending = 0;
7384 +
7385 + wq->lock = DWC_SPINLOCK_ALLOC();
7386 + if (!wq->lock) {
7387 + DWC_ERROR("Cannot allocate memory for spinlock");
7388 + goto no_lock;
7389 + }
7390 +
7391 + wq->waitq = DWC_WAITQ_ALLOC();
7392 + if (!wq->waitq) {
7393 + DWC_ERROR("Cannot allocate memory for waitqueue");
7394 + goto no_waitq;
7395 + }
7396 +
7397 + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
7398 +
7399 +#ifdef DEBUG
7400 + DWC_CIRCLEQ_INIT(&wq->entries);
7401 +#endif
7402 + return wq;
7403 +
7404 + no_waitq:
7405 + DWC_SPINLOCK_FREE(wq->lock);
7406 + no_lock:
7407 + taskqueue_free(wq->taskq);
7408 + no_taskq:
7409 + DWC_FREE(wq);
7410 +
7411 + return NULL;
7412 +}
7413 +
7414 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
7415 +{
7416 +#ifdef DEBUG
7417 + dwc_irqflags_t flags;
7418 +
7419 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7420 +
7421 + if (wq->pending != 0) {
7422 + struct work_container *container;
7423 +
7424 + DWC_ERROR("Destroying work queue with pending work");
7425 +
7426 + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
7427 + DWC_ERROR("Work %s still pending", container->name);
7428 + }
7429 + }
7430 +
7431 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7432 +#endif
7433 + DWC_WAITQ_FREE(wq->waitq);
7434 + DWC_SPINLOCK_FREE(wq->lock);
7435 + taskqueue_free(wq->taskq);
7436 + DWC_FREE(wq);
7437 +}
7438 +
7439 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
7440 + char *format, ...)
7441 +{
7442 + dwc_irqflags_t flags;
7443 + work_container_t *container;
7444 + static char name[128];
7445 + va_list args;
7446 +
7447 + va_start(args, format);
7448 + DWC_VSNPRINTF(name, 128, format, args);
7449 + va_end(args);
7450 +
7451 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7452 + wq->pending++;
7453 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7454 + DWC_WAITQ_TRIGGER(wq->waitq);
7455 +
7456 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
7457 + if (!container) {
7458 + DWC_ERROR("Cannot allocate memory for container");
7459 + return;
7460 + }
7461 +
7462 + container->name = DWC_STRDUP(name);
7463 + if (!container->name) {
7464 + DWC_ERROR("Cannot allocate memory for container->name");
7465 + DWC_FREE(container);
7466 + return;
7467 + }
7468 +
7469 + container->cb = cb;
7470 + container->data = data;
7471 + container->wq = wq;
7472 + container->hz = 0;
7473 +
7474 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
7475 +
7476 + TASK_INIT(&container->task, 0, do_work, container);
7477 +
7478 +#ifdef DEBUG
7479 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
7480 +#endif
7481 + taskqueue_enqueue_fast(wq->taskq, &container->task);
7482 +}
7483 +
7484 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
7485 + void *data, uint32_t time, char *format, ...)
7486 +{
7487 + dwc_irqflags_t flags;
7488 + work_container_t *container;
7489 + static char name[128];
7490 + struct timeval tv;
7491 + va_list args;
7492 +
7493 + va_start(args, format);
7494 + DWC_VSNPRINTF(name, 128, format, args);
7495 + va_end(args);
7496 +
7497 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7498 + wq->pending++;
7499 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7500 + DWC_WAITQ_TRIGGER(wq->waitq);
7501 +
7502 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
7503 + if (!container) {
7504 + DWC_ERROR("Cannot allocate memory for container");
7505 + return;
7506 + }
7507 +
7508 + container->name = DWC_STRDUP(name);
7509 + if (!container->name) {
7510 + DWC_ERROR("Cannot allocate memory for container->name");
7511 + DWC_FREE(container);
7512 + return;
7513 + }
7514 +
7515 + container->cb = cb;
7516 + container->data = data;
7517 + container->wq = wq;
7518 +
7519 + tv.tv_sec = time / 1000;
7520 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
7521 + container->hz = tvtohz(&tv);
7522 +
7523 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
7524 +
7525 + TASK_INIT(&container->task, 0, do_work, container);
7526 +
7527 +#ifdef DEBUG
7528 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
7529 +#endif
7530 + taskqueue_enqueue_fast(wq->taskq, &container->task);
7531 +}
7532 +
7533 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
7534 +{
7535 + return wq->pending;
7536 +}
7537 --- /dev/null
7538 +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
7539 @@ -0,0 +1,1409 @@
7540 +#include <linux/kernel.h>
7541 +#include <linux/init.h>
7542 +#include <linux/module.h>
7543 +#include <linux/kthread.h>
7544 +
7545 +#ifdef DWC_CCLIB
7546 +# include "dwc_cc.h"
7547 +#endif
7548 +
7549 +#ifdef DWC_CRYPTOLIB
7550 +# include "dwc_modpow.h"
7551 +# include "dwc_dh.h"
7552 +# include "dwc_crypto.h"
7553 +#endif
7554 +
7555 +#ifdef DWC_NOTIFYLIB
7556 +# include "dwc_notifier.h"
7557 +#endif
7558 +
7559 +/* OS-Level Implementations */
7560 +
7561 +/* This is the Linux kernel implementation of the DWC platform library. */
7562 +#include <linux/moduleparam.h>
7563 +#include <linux/ctype.h>
7564 +#include <linux/crypto.h>
7565 +#include <linux/delay.h>
7566 +#include <linux/device.h>
7567 +#include <linux/dma-mapping.h>
7568 +#include <linux/cdev.h>
7569 +#include <linux/errno.h>
7570 +#include <linux/interrupt.h>
7571 +#include <linux/jiffies.h>
7572 +#include <linux/list.h>
7573 +#include <linux/pci.h>
7574 +#include <linux/random.h>
7575 +#include <linux/scatterlist.h>
7576 +#include <linux/slab.h>
7577 +#include <linux/stat.h>
7578 +#include <linux/string.h>
7579 +#include <linux/timer.h>
7580 +#include <linux/usb.h>
7581 +
7582 +#include <linux/version.h>
7583 +
7584 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
7585 +# include <linux/usb/gadget.h>
7586 +#else
7587 +# include <linux/usb_gadget.h>
7588 +#endif
7589 +
7590 +#include <asm/io.h>
7591 +#include <asm/page.h>
7592 +#include <asm/uaccess.h>
7593 +#include <asm/unaligned.h>
7594 +
7595 +#include "dwc_os.h"
7596 +#include "dwc_list.h"
7597 +
7598 +
7599 +/* MISC */
7600 +
7601 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
7602 +{
7603 + return memset(dest, byte, size);
7604 +}
7605 +
7606 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
7607 +{
7608 + return memcpy(dest, src, size);
7609 +}
7610 +
7611 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
7612 +{
7613 + return memmove(dest, src, size);
7614 +}
7615 +
7616 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
7617 +{
7618 + return memcmp(m1, m2, size);
7619 +}
7620 +
7621 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
7622 +{
7623 + return strncmp(s1, s2, size);
7624 +}
7625 +
7626 +int DWC_STRCMP(void *s1, void *s2)
7627 +{
7628 + return strcmp(s1, s2);
7629 +}
7630 +
7631 +int DWC_STRLEN(char const *str)
7632 +{
7633 + return strlen(str);
7634 +}
7635 +
7636 +char *DWC_STRCPY(char *to, char const *from)
7637 +{
7638 + return strcpy(to, from);
7639 +}
7640 +
7641 +char *DWC_STRDUP(char const *str)
7642 +{
7643 + int len = DWC_STRLEN(str) + 1;
7644 + char *new = DWC_ALLOC_ATOMIC(len);
7645 +
7646 + if (!new) {
7647 + return NULL;
7648 + }
7649 +
7650 + DWC_MEMCPY(new, str, len);
7651 + return new;
7652 +}
7653 +
7654 +int DWC_ATOI(const char *str, int32_t *value)
7655 +{
7656 + char *end = NULL;
7657 +
7658 + *value = simple_strtol(str, &end, 0);
7659 + if (*end == '\0') {
7660 + return 0;
7661 + }
7662 +
7663 + return -1;
7664 +}
7665 +
7666 +int DWC_ATOUI(const char *str, uint32_t *value)
7667 +{
7668 + char *end = NULL;
7669 +
7670 + *value = simple_strtoul(str, &end, 0);
7671 + if (*end == '\0') {
7672 + return 0;
7673 + }
7674 +
7675 + return -1;
7676 +}
7677 +
7678 +
7679 +#ifdef DWC_UTFLIB
7680 +/* From usbstring.c */
7681 +
7682 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
7683 +{
7684 + int count = 0;
7685 + u8 c;
7686 + u16 uchar;
7687 +
7688 + /* this insists on correct encodings, though not minimal ones.
7689 + * BUT it currently rejects legit 4-byte UTF-8 code points,
7690 + * which need surrogate pairs. (Unicode 3.1 can use them.)
7691 + */
7692 + while (len != 0 && (c = (u8) *s++) != 0) {
7693 + if (unlikely(c & 0x80)) {
7694 + // 2-byte sequence:
7695 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
7696 + if ((c & 0xe0) == 0xc0) {
7697 + uchar = (c & 0x1f) << 6;
7698 +
7699 + c = (u8) *s++;
7700 + if ((c & 0xc0) != 0xc0)
7701 + goto fail;
7702 + c &= 0x3f;
7703 + uchar |= c;
7704 +
7705 + // 3-byte sequence (most CJKV characters):
7706 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
7707 + } else if ((c & 0xf0) == 0xe0) {
7708 + uchar = (c & 0x0f) << 12;
7709 +
7710 + c = (u8) *s++;
7711 + if ((c & 0xc0) != 0xc0)
7712 + goto fail;
7713 + c &= 0x3f;
7714 + uchar |= c << 6;
7715 +
7716 + c = (u8) *s++;
7717 + if ((c & 0xc0) != 0xc0)
7718 + goto fail;
7719 + c &= 0x3f;
7720 + uchar |= c;
7721 +
7722 + /* no bogus surrogates */
7723 + if (0xd800 <= uchar && uchar <= 0xdfff)
7724 + goto fail;
7725 +
7726 + // 4-byte sequence (surrogate pairs, currently rare):
7727 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
7728 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
7729 + // (uuuuu = wwww + 1)
7730 + // FIXME accept the surrogate code points (only)
7731 + } else
7732 + goto fail;
7733 + } else
7734 + uchar = c;
7735 + put_unaligned (cpu_to_le16 (uchar), cp++);
7736 + count++;
7737 + len--;
7738 + }
7739 + return count;
7740 +fail:
7741 + return -1;
7742 +}
7743 +#endif /* DWC_UTFLIB */
7744 +
7745 +
7746 +/* dwc_debug.h */
7747 +
7748 +dwc_bool_t DWC_IN_IRQ(void)
7749 +{
7750 + return in_irq();
7751 +}
7752 +
7753 +dwc_bool_t DWC_IN_BH(void)
7754 +{
7755 + return in_softirq();
7756 +}
7757 +
7758 +void DWC_VPRINTF(char *format, va_list args)
7759 +{
7760 + vprintk(format, args);
7761 +}
7762 +
7763 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
7764 +{
7765 + return vsnprintf(str, size, format, args);
7766 +}
7767 +
7768 +void DWC_PRINTF(char *format, ...)
7769 +{
7770 + va_list args;
7771 +
7772 + va_start(args, format);
7773 + DWC_VPRINTF(format, args);
7774 + va_end(args);
7775 +}
7776 +
7777 +int DWC_SPRINTF(char *buffer, char *format, ...)
7778 +{
7779 + int retval;
7780 + va_list args;
7781 +
7782 + va_start(args, format);
7783 + retval = vsprintf(buffer, format, args);
7784 + va_end(args);
7785 + return retval;
7786 +}
7787 +
7788 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
7789 +{
7790 + int retval;
7791 + va_list args;
7792 +
7793 + va_start(args, format);
7794 + retval = vsnprintf(buffer, size, format, args);
7795 + va_end(args);
7796 + return retval;
7797 +}
7798 +
7799 +void __DWC_WARN(char *format, ...)
7800 +{
7801 + va_list args;
7802 +
7803 + va_start(args, format);
7804 + DWC_PRINTF(KERN_WARNING);
7805 + DWC_VPRINTF(format, args);
7806 + va_end(args);
7807 +}
7808 +
7809 +void __DWC_ERROR(char *format, ...)
7810 +{
7811 + va_list args;
7812 +
7813 + va_start(args, format);
7814 + DWC_PRINTF(KERN_ERR);
7815 + DWC_VPRINTF(format, args);
7816 + va_end(args);
7817 +}
7818 +
7819 +void DWC_EXCEPTION(char *format, ...)
7820 +{
7821 + va_list args;
7822 +
7823 + va_start(args, format);
7824 + DWC_PRINTF(KERN_ERR);
7825 + DWC_VPRINTF(format, args);
7826 + va_end(args);
7827 + BUG_ON(1);
7828 +}
7829 +
7830 +#ifdef DEBUG
7831 +void __DWC_DEBUG(char *format, ...)
7832 +{
7833 + va_list args;
7834 +
7835 + va_start(args, format);
7836 + DWC_PRINTF(KERN_DEBUG);
7837 + DWC_VPRINTF(format, args);
7838 + va_end(args);
7839 +}
7840 +#endif
7841 +
7842 +
7843 +/* dwc_mem.h */
7844 +
7845 +#if 0
7846 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
7847 + uint32_t align,
7848 + uint32_t alloc)
7849 +{
7850 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
7851 + size, align, alloc);
7852 + return (dwc_pool_t *)pool;
7853 +}
7854 +
7855 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
7856 +{
7857 + dma_pool_destroy((struct dma_pool *)pool);
7858 +}
7859 +
7860 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
7861 +{
7862 + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
7863 +}
7864 +
7865 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
7866 +{
7867 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
7868 + memset(..);
7869 +}
7870 +
7871 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
7872 +{
7873 + dma_pool_free(pool, vaddr, daddr);
7874 +}
7875 +#endif
7876 +
7877 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
7878 +{
7879 + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
7880 +}
7881 +
7882 +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
7883 +{
7884 + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
7885 +}
7886 +
7887 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
7888 +{
7889 + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
7890 +}
7891 +
7892 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
7893 +{
7894 + return kzalloc(size, GFP_KERNEL);
7895 +}
7896 +
7897 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
7898 +{
7899 + return kzalloc(size, GFP_ATOMIC);
7900 +}
7901 +
7902 +void __DWC_FREE(void *mem_ctx, void *addr)
7903 +{
7904 + kfree(addr);
7905 +}
7906 +
7907 +
7908 +#ifdef DWC_CRYPTOLIB
7909 +/* dwc_crypto.h */
7910 +
7911 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
7912 +{
7913 + get_random_bytes(buffer, length);
7914 +}
7915 +
7916 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
7917 +{
7918 + struct crypto_blkcipher *tfm;
7919 + struct blkcipher_desc desc;
7920 + struct scatterlist sgd;
7921 + struct scatterlist sgs;
7922 +
7923 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
7924 + if (tfm == NULL) {
7925 + printk("failed to load transform for aes CBC\n");
7926 + return -1;
7927 + }
7928 +
7929 + crypto_blkcipher_setkey(tfm, key, keylen);
7930 + crypto_blkcipher_set_iv(tfm, iv, 16);
7931 +
7932 + sg_init_one(&sgd, out, messagelen);
7933 + sg_init_one(&sgs, message, messagelen);
7934 +
7935 + desc.tfm = tfm;
7936 + desc.flags = 0;
7937 +
7938 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
7939 + crypto_free_blkcipher(tfm);
7940 + DWC_ERROR("AES CBC encryption failed");
7941 + return -1;
7942 + }
7943 +
7944 + crypto_free_blkcipher(tfm);
7945 + return 0;
7946 +}
7947 +
7948 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
7949 +{
7950 + struct crypto_hash *tfm;
7951 + struct hash_desc desc;
7952 + struct scatterlist sg;
7953 +
7954 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
7955 + if (IS_ERR(tfm)) {
7956 + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
7957 + return 0;
7958 + }
7959 + desc.tfm = tfm;
7960 + desc.flags = 0;
7961 +
7962 + sg_init_one(&sg, message, len);
7963 + crypto_hash_digest(&desc, &sg, len, out);
7964 + crypto_free_hash(tfm);
7965 +
7966 + return 1;
7967 +}
7968 +
7969 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
7970 + uint8_t *key, uint32_t keylen, uint8_t *out)
7971 +{
7972 + struct crypto_hash *tfm;
7973 + struct hash_desc desc;
7974 + struct scatterlist sg;
7975 +
7976 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
7977 + if (IS_ERR(tfm)) {
7978 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
7979 + return 0;
7980 + }
7981 + desc.tfm = tfm;
7982 + desc.flags = 0;
7983 +
7984 + sg_init_one(&sg, message, messagelen);
7985 + crypto_hash_setkey(tfm, key, keylen);
7986 + crypto_hash_digest(&desc, &sg, messagelen, out);
7987 + crypto_free_hash(tfm);
7988 +
7989 + return 1;
7990 +}
7991 +#endif /* DWC_CRYPTOLIB */
7992 +
7993 +
7994 +/* Byte Ordering Conversions */
7995 +
7996 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
7997 +{
7998 +#ifdef __LITTLE_ENDIAN
7999 + return *p;
8000 +#else
8001 + uint8_t *u_p = (uint8_t *)p;
8002 +
8003 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
8004 +#endif
8005 +}
8006 +
8007 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
8008 +{
8009 +#ifdef __BIG_ENDIAN
8010 + return *p;
8011 +#else
8012 + uint8_t *u_p = (uint8_t *)p;
8013 +
8014 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
8015 +#endif
8016 +}
8017 +
8018 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
8019 +{
8020 +#ifdef __LITTLE_ENDIAN
8021 + return *p;
8022 +#else
8023 + uint8_t *u_p = (uint8_t *)p;
8024 +
8025 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
8026 +#endif
8027 +}
8028 +
8029 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
8030 +{
8031 +#ifdef __BIG_ENDIAN
8032 + return *p;
8033 +#else
8034 + uint8_t *u_p = (uint8_t *)p;
8035 +
8036 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
8037 +#endif
8038 +}
8039 +
8040 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
8041 +{
8042 +#ifdef __LITTLE_ENDIAN
8043 + return *p;
8044 +#else
8045 + uint8_t *u_p = (uint8_t *)p;
8046 + return (u_p[1] | (u_p[0] << 8));
8047 +#endif
8048 +}
8049 +
8050 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
8051 +{
8052 +#ifdef __BIG_ENDIAN
8053 + return *p;
8054 +#else
8055 + uint8_t *u_p = (uint8_t *)p;
8056 + return (u_p[1] | (u_p[0] << 8));
8057 +#endif
8058 +}
8059 +
8060 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
8061 +{
8062 +#ifdef __LITTLE_ENDIAN
8063 + return *p;
8064 +#else
8065 + uint8_t *u_p = (uint8_t *)p;
8066 + return (u_p[1] | (u_p[0] << 8));
8067 +#endif
8068 +}
8069 +
8070 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
8071 +{
8072 +#ifdef __BIG_ENDIAN
8073 + return *p;
8074 +#else
8075 + uint8_t *u_p = (uint8_t *)p;
8076 + return (u_p[1] | (u_p[0] << 8));
8077 +#endif
8078 +}
8079 +
8080 +
8081 +/* Registers */
8082 +
8083 +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
8084 +{
8085 + return readl(reg);
8086 +}
8087 +
8088 +#if 0
8089 +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
8090 +{
8091 +}
8092 +#endif
8093 +
8094 +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
8095 +{
8096 + writel(value, reg);
8097 +}
8098 +
8099 +#if 0
8100 +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
8101 +{
8102 +}
8103 +#endif
8104 +
8105 +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
8106 +{
8107 + writel((readl(reg) & ~clear_mask) | set_mask, reg);
8108 +}
8109 +
8110 +#if 0
8111 +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
8112 +{
8113 +}
8114 +#endif
8115 +
8116 +
8117 +/* Locking */
8118 +
8119 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
8120 +{
8121 + spinlock_t *sl = (spinlock_t *)1;
8122 +
8123 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8124 + sl = DWC_ALLOC(sizeof(*sl));
8125 + if (!sl) {
8126 + DWC_ERROR("Cannot allocate memory for spinlock\n");
8127 + return NULL;
8128 + }
8129 +
8130 + spin_lock_init(sl);
8131 +#endif
8132 + return (dwc_spinlock_t *)sl;
8133 +}
8134 +
8135 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
8136 +{
8137 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8138 + DWC_FREE(lock);
8139 +#endif
8140 +}
8141 +
8142 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
8143 +{
8144 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8145 + spin_lock((spinlock_t *)lock);
8146 +#endif
8147 +}
8148 +
8149 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
8150 +{
8151 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8152 + spin_unlock((spinlock_t *)lock);
8153 +#endif
8154 +}
8155 +
8156 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
8157 +{
8158 + dwc_irqflags_t f;
8159 +
8160 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8161 + spin_lock_irqsave((spinlock_t *)lock, f);
8162 +#else
8163 + local_irq_save(f);
8164 +#endif
8165 + *flags = f;
8166 +}
8167 +
8168 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
8169 +{
8170 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8171 + spin_unlock_irqrestore((spinlock_t *)lock, flags);
8172 +#else
8173 + local_irq_restore(flags);
8174 +#endif
8175 +}
8176 +
8177 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
8178 +{
8179 + struct mutex *m;
8180 + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
8181 +
8182 + if (!mutex) {
8183 + DWC_ERROR("Cannot allocate memory for mutex\n");
8184 + return NULL;
8185 + }
8186 +
8187 + m = (struct mutex *)mutex;
8188 + mutex_init(m);
8189 + return mutex;
8190 +}
8191 +
8192 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
8193 +#else
8194 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
8195 +{
8196 + mutex_destroy((struct mutex *)mutex);
8197 + DWC_FREE(mutex);
8198 +}
8199 +#endif
8200 +
8201 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
8202 +{
8203 + struct mutex *m = (struct mutex *)mutex;
8204 + mutex_lock(m);
8205 +}
8206 +
8207 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
8208 +{
8209 + struct mutex *m = (struct mutex *)mutex;
8210 + return mutex_trylock(m);
8211 +}
8212 +
8213 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
8214 +{
8215 + struct mutex *m = (struct mutex *)mutex;
8216 + mutex_unlock(m);
8217 +}
8218 +
8219 +
8220 +/* Timing */
8221 +
8222 +void DWC_UDELAY(uint32_t usecs)
8223 +{
8224 + udelay(usecs);
8225 +}
8226 +
8227 +void DWC_MDELAY(uint32_t msecs)
8228 +{
8229 + mdelay(msecs);
8230 +}
8231 +
8232 +void DWC_MSLEEP(uint32_t msecs)
8233 +{
8234 + msleep(msecs);
8235 +}
8236 +
8237 +uint32_t DWC_TIME(void)
8238 +{
8239 + return jiffies_to_msecs(jiffies);
8240 +}
8241 +
8242 +
8243 +/* Timers */
8244 +
8245 +struct dwc_timer {
8246 + struct timer_list t;
8247 + char *name;
8248 + dwc_timer_callback_t cb;
8249 + void *data;
8250 + uint8_t scheduled;
8251 + dwc_spinlock_t *lock;
8252 +};
8253 +
8254 +static void timer_callback(struct timer_list *tt)
8255 +{
8256 + dwc_timer_t *timer = from_timer(timer, tt, t);
8257 + dwc_irqflags_t flags;
8258 +
8259 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8260 + timer->scheduled = 0;
8261 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8262 + DWC_DEBUGC("Timer %s callback", timer->name);
8263 + timer->cb(timer->data);
8264 +}
8265 +
8266 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
8267 +{
8268 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
8269 +
8270 + if (!t) {
8271 + DWC_ERROR("Cannot allocate memory for timer");
8272 + return NULL;
8273 + }
8274 +
8275 + t->name = DWC_STRDUP(name);
8276 + if (!t->name) {
8277 + DWC_ERROR("Cannot allocate memory for timer->name");
8278 + goto no_name;
8279 + }
8280 +
8281 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
8282 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
8283 +#else
8284 + t->lock = DWC_SPINLOCK_ALLOC();
8285 +#endif
8286 + if (!t->lock) {
8287 + DWC_ERROR("Cannot allocate memory for lock");
8288 + goto no_lock;
8289 + }
8290 +
8291 + t->scheduled = 0;
8292 + t->t.expires = jiffies;
8293 + timer_setup(&t->t, timer_callback, 0);
8294 +
8295 + t->cb = cb;
8296 + t->data = data;
8297 +
8298 + return t;
8299 +
8300 + no_lock:
8301 + DWC_FREE(t->name);
8302 + no_name:
8303 + DWC_FREE(t);
8304 + return NULL;
8305 +}
8306 +
8307 +void DWC_TIMER_FREE(dwc_timer_t *timer)
8308 +{
8309 + dwc_irqflags_t flags;
8310 +
8311 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8312 +
8313 + if (timer->scheduled) {
8314 + del_timer(&timer->t);
8315 + timer->scheduled = 0;
8316 + }
8317 +
8318 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8319 + DWC_SPINLOCK_FREE(timer->lock);
8320 + DWC_FREE(timer->name);
8321 + DWC_FREE(timer);
8322 +}
8323 +
8324 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
8325 +{
8326 + dwc_irqflags_t flags;
8327 +
8328 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8329 +
8330 + if (!timer->scheduled) {
8331 + timer->scheduled = 1;
8332 + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
8333 + timer->t.expires = jiffies + msecs_to_jiffies(time);
8334 + add_timer(&timer->t);
8335 + } else {
8336 + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
8337 + mod_timer(&timer->t, jiffies + msecs_to_jiffies(time));
8338 + }
8339 +
8340 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8341 +}
8342 +
8343 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
8344 +{
8345 + del_timer(&timer->t);
8346 +}
8347 +
8348 +
8349 +/* Wait Queues */
8350 +
8351 +struct dwc_waitq {
8352 + wait_queue_head_t queue;
8353 + int abort;
8354 +};
8355 +
8356 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
8357 +{
8358 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
8359 +
8360 + if (!wq) {
8361 + DWC_ERROR("Cannot allocate memory for waitqueue\n");
8362 + return NULL;
8363 + }
8364 +
8365 + init_waitqueue_head(&wq->queue);
8366 + wq->abort = 0;
8367 + return wq;
8368 +}
8369 +
8370 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
8371 +{
8372 + DWC_FREE(wq);
8373 +}
8374 +
8375 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
8376 +{
8377 + int result = wait_event_interruptible(wq->queue,
8378 + cond(data) || wq->abort);
8379 + if (result == -ERESTARTSYS) {
8380 + wq->abort = 0;
8381 + return -DWC_E_RESTART;
8382 + }
8383 +
8384 + if (wq->abort == 1) {
8385 + wq->abort = 0;
8386 + return -DWC_E_ABORT;
8387 + }
8388 +
8389 + wq->abort = 0;
8390 +
8391 + if (result == 0) {
8392 + return 0;
8393 + }
8394 +
8395 + return -DWC_E_UNKNOWN;
8396 +}
8397 +
8398 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
8399 + void *data, int32_t msecs)
8400 +{
8401 + int32_t tmsecs;
8402 + int result = wait_event_interruptible_timeout(wq->queue,
8403 + cond(data) || wq->abort,
8404 + msecs_to_jiffies(msecs));
8405 + if (result == -ERESTARTSYS) {
8406 + wq->abort = 0;
8407 + return -DWC_E_RESTART;
8408 + }
8409 +
8410 + if (wq->abort == 1) {
8411 + wq->abort = 0;
8412 + return -DWC_E_ABORT;
8413 + }
8414 +
8415 + wq->abort = 0;
8416 +
8417 + if (result > 0) {
8418 + tmsecs = jiffies_to_msecs(result);
8419 + if (!tmsecs) {
8420 + return 1;
8421 + }
8422 +
8423 + return tmsecs;
8424 + }
8425 +
8426 + if (result == 0) {
8427 + return -DWC_E_TIMEOUT;
8428 + }
8429 +
8430 + return -DWC_E_UNKNOWN;
8431 +}
8432 +
8433 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
8434 +{
8435 + wq->abort = 0;
8436 + wake_up_interruptible(&wq->queue);
8437 +}
8438 +
8439 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
8440 +{
8441 + wq->abort = 1;
8442 + wake_up_interruptible(&wq->queue);
8443 +}
8444 +
8445 +
8446 +/* Threading */
8447 +
8448 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
8449 +{
8450 + struct task_struct *thread = kthread_run(func, data, name);
8451 +
8452 + if (thread == ERR_PTR(-ENOMEM)) {
8453 + return NULL;
8454 + }
8455 +
8456 + return (dwc_thread_t *)thread;
8457 +}
8458 +
8459 +int DWC_THREAD_STOP(dwc_thread_t *thread)
8460 +{
8461 + return kthread_stop((struct task_struct *)thread);
8462 +}
8463 +
8464 +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
8465 +{
8466 + return kthread_should_stop();
8467 +}
8468 +
8469 +
8470 +/* tasklets
8471 + - run in interrupt context (cannot sleep)
8472 + - each tasklet runs on a single CPU
8473 + - different tasklets can be running simultaneously on different CPUs
8474 + */
8475 +struct dwc_tasklet {
8476 + struct tasklet_struct t;
8477 + dwc_tasklet_callback_t cb;
8478 + void *data;
8479 +};
8480 +
8481 +static void tasklet_callback(unsigned long data)
8482 +{
8483 + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
8484 + t->cb(t->data);
8485 +}
8486 +
8487 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
8488 +{
8489 + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
8490 +
8491 + if (t) {
8492 + t->cb = cb;
8493 + t->data = data;
8494 + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
8495 + } else {
8496 + DWC_ERROR("Cannot allocate memory for tasklet\n");
8497 + }
8498 +
8499 + return t;
8500 +}
8501 +
8502 +void DWC_TASK_FREE(dwc_tasklet_t *task)
8503 +{
8504 + DWC_FREE(task);
8505 +}
8506 +
8507 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
8508 +{
8509 + tasklet_schedule(&task->t);
8510 +}
8511 +
8512 +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
8513 +{
8514 + tasklet_hi_schedule(&task->t);
8515 +}
8516 +
8517 +
8518 +/* workqueues
8519 + - run in process context (can sleep)
8520 + */
8521 +typedef struct work_container {
8522 + dwc_work_callback_t cb;
8523 + void *data;
8524 + dwc_workq_t *wq;
8525 + char *name;
8526 +
8527 +#ifdef DEBUG
8528 + DWC_CIRCLEQ_ENTRY(work_container) entry;
8529 +#endif
8530 + struct delayed_work work;
8531 +} work_container_t;
8532 +
8533 +#ifdef DEBUG
8534 +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
8535 +#endif
8536 +
8537 +struct dwc_workq {
8538 + struct workqueue_struct *wq;
8539 + dwc_spinlock_t *lock;
8540 + dwc_waitq_t *waitq;
8541 + int pending;
8542 +
8543 +#ifdef DEBUG
8544 + struct work_container_queue entries;
8545 +#endif
8546 +};
8547 +
8548 +static void do_work(struct work_struct *work)
8549 +{
8550 + dwc_irqflags_t flags;
8551 + struct delayed_work *dw = container_of(work, struct delayed_work, work);
8552 + work_container_t *container = container_of(dw, struct work_container, work);
8553 + dwc_workq_t *wq = container->wq;
8554 +
8555 + container->cb(container->data);
8556 +
8557 +#ifdef DEBUG
8558 + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
8559 +#endif
8560 + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
8561 + if (container->name) {
8562 + DWC_FREE(container->name);
8563 + }
8564 + DWC_FREE(container);
8565 +
8566 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8567 + wq->pending--;
8568 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8569 + DWC_WAITQ_TRIGGER(wq->waitq);
8570 +}
8571 +
8572 +static int work_done(void *data)
8573 +{
8574 + dwc_workq_t *workq = (dwc_workq_t *)data;
8575 + return workq->pending == 0;
8576 +}
8577 +
8578 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
8579 +{
8580 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
8581 +}
8582 +
8583 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
8584 +{
8585 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
8586 +
8587 + if (!wq) {
8588 + return NULL;
8589 + }
8590 +
8591 + wq->wq = create_singlethread_workqueue(name);
8592 + if (!wq->wq) {
8593 + goto no_wq;
8594 + }
8595 +
8596 + wq->pending = 0;
8597 +
8598 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
8599 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
8600 +#else
8601 + wq->lock = DWC_SPINLOCK_ALLOC();
8602 +#endif
8603 + if (!wq->lock) {
8604 + goto no_lock;
8605 + }
8606 +
8607 + wq->waitq = DWC_WAITQ_ALLOC();
8608 + if (!wq->waitq) {
8609 + goto no_waitq;
8610 + }
8611 +
8612 +#ifdef DEBUG
8613 + DWC_CIRCLEQ_INIT(&wq->entries);
8614 +#endif
8615 + return wq;
8616 +
8617 + no_waitq:
8618 + DWC_SPINLOCK_FREE(wq->lock);
8619 + no_lock:
8620 + destroy_workqueue(wq->wq);
8621 + no_wq:
8622 + DWC_FREE(wq);
8623 +
8624 + return NULL;
8625 +}
8626 +
8627 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
8628 +{
8629 +#ifdef DEBUG
8630 + if (wq->pending != 0) {
8631 + struct work_container *wc;
8632 + DWC_ERROR("Destroying work queue with pending work");
8633 + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
8634 + DWC_ERROR("Work %s still pending", wc->name);
8635 + }
8636 + }
8637 +#endif
8638 + destroy_workqueue(wq->wq);
8639 + DWC_SPINLOCK_FREE(wq->lock);
8640 + DWC_WAITQ_FREE(wq->waitq);
8641 + DWC_FREE(wq);
8642 +}
8643 +
8644 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
8645 + char *format, ...)
8646 +{
8647 + dwc_irqflags_t flags;
8648 + work_container_t *container;
8649 + static char name[128];
8650 + va_list args;
8651 +
8652 + va_start(args, format);
8653 + DWC_VSNPRINTF(name, 128, format, args);
8654 + va_end(args);
8655 +
8656 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8657 + wq->pending++;
8658 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8659 + DWC_WAITQ_TRIGGER(wq->waitq);
8660 +
8661 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
8662 + if (!container) {
8663 + DWC_ERROR("Cannot allocate memory for container\n");
8664 + return;
8665 + }
8666 +
8667 + container->name = DWC_STRDUP(name);
8668 + if (!container->name) {
8669 + DWC_ERROR("Cannot allocate memory for container->name\n");
8670 + DWC_FREE(container);
8671 + return;
8672 + }
8673 +
8674 + container->cb = cb;
8675 + container->data = data;
8676 + container->wq = wq;
8677 + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
8678 + INIT_WORK(&container->work.work, do_work);
8679 +
8680 +#ifdef DEBUG
8681 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
8682 +#endif
8683 + queue_work(wq->wq, &container->work.work);
8684 +}
8685 +
8686 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
8687 + void *data, uint32_t time, char *format, ...)
8688 +{
8689 + dwc_irqflags_t flags;
8690 + work_container_t *container;
8691 + static char name[128];
8692 + va_list args;
8693 +
8694 + va_start(args, format);
8695 + DWC_VSNPRINTF(name, 128, format, args);
8696 + va_end(args);
8697 +
8698 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8699 + wq->pending++;
8700 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8701 + DWC_WAITQ_TRIGGER(wq->waitq);
8702 +
8703 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
8704 + if (!container) {
8705 + DWC_ERROR("Cannot allocate memory for container\n");
8706 + return;
8707 + }
8708 +
8709 + container->name = DWC_STRDUP(name);
8710 + if (!container->name) {
8711 + DWC_ERROR("Cannot allocate memory for container->name\n");
8712 + DWC_FREE(container);
8713 + return;
8714 + }
8715 +
8716 + container->cb = cb;
8717 + container->data = data;
8718 + container->wq = wq;
8719 + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
8720 + INIT_DELAYED_WORK(&container->work, do_work);
8721 +
8722 +#ifdef DEBUG
8723 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
8724 +#endif
8725 + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
8726 +}
8727 +
8728 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
8729 +{
8730 + return wq->pending;
8731 +}
8732 +
8733 +
8734 +#ifdef DWC_LIBMODULE
8735 +
8736 +#ifdef DWC_CCLIB
8737 +/* CC */
8738 +EXPORT_SYMBOL(dwc_cc_if_alloc);
8739 +EXPORT_SYMBOL(dwc_cc_if_free);
8740 +EXPORT_SYMBOL(dwc_cc_clear);
8741 +EXPORT_SYMBOL(dwc_cc_add);
8742 +EXPORT_SYMBOL(dwc_cc_remove);
8743 +EXPORT_SYMBOL(dwc_cc_change);
8744 +EXPORT_SYMBOL(dwc_cc_data_for_save);
8745 +EXPORT_SYMBOL(dwc_cc_restore_from_data);
8746 +EXPORT_SYMBOL(dwc_cc_match_chid);
8747 +EXPORT_SYMBOL(dwc_cc_match_cdid);
8748 +EXPORT_SYMBOL(dwc_cc_ck);
8749 +EXPORT_SYMBOL(dwc_cc_chid);
8750 +EXPORT_SYMBOL(dwc_cc_cdid);
8751 +EXPORT_SYMBOL(dwc_cc_name);
8752 +#endif /* DWC_CCLIB */
8753 +
8754 +#ifdef DWC_CRYPTOLIB
8755 +# ifndef CONFIG_MACH_IPMATE
8756 +/* Modpow */
8757 +EXPORT_SYMBOL(dwc_modpow);
8758 +
8759 +/* DH */
8760 +EXPORT_SYMBOL(dwc_dh_modpow);
8761 +EXPORT_SYMBOL(dwc_dh_derive_keys);
8762 +EXPORT_SYMBOL(dwc_dh_pk);
8763 +# endif /* CONFIG_MACH_IPMATE */
8764 +
8765 +/* Crypto */
8766 +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
8767 +EXPORT_SYMBOL(dwc_wusb_cmf);
8768 +EXPORT_SYMBOL(dwc_wusb_prf);
8769 +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
8770 +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
8771 +EXPORT_SYMBOL(dwc_wusb_gen_key);
8772 +EXPORT_SYMBOL(dwc_wusb_gen_mic);
8773 +#endif /* DWC_CRYPTOLIB */
8774 +
8775 +/* Notification */
8776 +#ifdef DWC_NOTIFYLIB
8777 +EXPORT_SYMBOL(dwc_alloc_notification_manager);
8778 +EXPORT_SYMBOL(dwc_free_notification_manager);
8779 +EXPORT_SYMBOL(dwc_register_notifier);
8780 +EXPORT_SYMBOL(dwc_unregister_notifier);
8781 +EXPORT_SYMBOL(dwc_add_observer);
8782 +EXPORT_SYMBOL(dwc_remove_observer);
8783 +EXPORT_SYMBOL(dwc_notify);
8784 +#endif
8785 +
8786 +/* Memory Debugging Routines */
8787 +#ifdef DWC_DEBUG_MEMORY
8788 +EXPORT_SYMBOL(dwc_alloc_debug);
8789 +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
8790 +EXPORT_SYMBOL(dwc_free_debug);
8791 +EXPORT_SYMBOL(dwc_dma_alloc_debug);
8792 +EXPORT_SYMBOL(dwc_dma_free_debug);
8793 +#endif
8794 +
8795 +EXPORT_SYMBOL(DWC_MEMSET);
8796 +EXPORT_SYMBOL(DWC_MEMCPY);
8797 +EXPORT_SYMBOL(DWC_MEMMOVE);
8798 +EXPORT_SYMBOL(DWC_MEMCMP);
8799 +EXPORT_SYMBOL(DWC_STRNCMP);
8800 +EXPORT_SYMBOL(DWC_STRCMP);
8801 +EXPORT_SYMBOL(DWC_STRLEN);
8802 +EXPORT_SYMBOL(DWC_STRCPY);
8803 +EXPORT_SYMBOL(DWC_STRDUP);
8804 +EXPORT_SYMBOL(DWC_ATOI);
8805 +EXPORT_SYMBOL(DWC_ATOUI);
8806 +
8807 +#ifdef DWC_UTFLIB
8808 +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
8809 +#endif /* DWC_UTFLIB */
8810 +
8811 +EXPORT_SYMBOL(DWC_IN_IRQ);
8812 +EXPORT_SYMBOL(DWC_IN_BH);
8813 +EXPORT_SYMBOL(DWC_VPRINTF);
8814 +EXPORT_SYMBOL(DWC_VSNPRINTF);
8815 +EXPORT_SYMBOL(DWC_PRINTF);
8816 +EXPORT_SYMBOL(DWC_SPRINTF);
8817 +EXPORT_SYMBOL(DWC_SNPRINTF);
8818 +EXPORT_SYMBOL(__DWC_WARN);
8819 +EXPORT_SYMBOL(__DWC_ERROR);
8820 +EXPORT_SYMBOL(DWC_EXCEPTION);
8821 +
8822 +#ifdef DEBUG
8823 +EXPORT_SYMBOL(__DWC_DEBUG);
8824 +#endif
8825 +
8826 +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
8827 +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
8828 +EXPORT_SYMBOL(__DWC_DMA_FREE);
8829 +EXPORT_SYMBOL(__DWC_ALLOC);
8830 +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
8831 +EXPORT_SYMBOL(__DWC_FREE);
8832 +
8833 +#ifdef DWC_CRYPTOLIB
8834 +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
8835 +EXPORT_SYMBOL(DWC_AES_CBC);
8836 +EXPORT_SYMBOL(DWC_SHA256);
8837 +EXPORT_SYMBOL(DWC_HMAC_SHA256);
8838 +#endif
8839 +
8840 +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
8841 +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
8842 +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
8843 +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
8844 +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
8845 +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
8846 +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
8847 +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
8848 +EXPORT_SYMBOL(DWC_READ_REG32);
8849 +EXPORT_SYMBOL(DWC_WRITE_REG32);
8850 +EXPORT_SYMBOL(DWC_MODIFY_REG32);
8851 +
8852 +#if 0
8853 +EXPORT_SYMBOL(DWC_READ_REG64);
8854 +EXPORT_SYMBOL(DWC_WRITE_REG64);
8855 +EXPORT_SYMBOL(DWC_MODIFY_REG64);
8856 +#endif
8857 +
8858 +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
8859 +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
8860 +EXPORT_SYMBOL(DWC_SPINLOCK);
8861 +EXPORT_SYMBOL(DWC_SPINUNLOCK);
8862 +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
8863 +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
8864 +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
8865 +
8866 +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
8867 +EXPORT_SYMBOL(DWC_MUTEX_FREE);
8868 +#endif
8869 +
8870 +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
8871 +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
8872 +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
8873 +EXPORT_SYMBOL(DWC_UDELAY);
8874 +EXPORT_SYMBOL(DWC_MDELAY);
8875 +EXPORT_SYMBOL(DWC_MSLEEP);
8876 +EXPORT_SYMBOL(DWC_TIME);
8877 +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
8878 +EXPORT_SYMBOL(DWC_TIMER_FREE);
8879 +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
8880 +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
8881 +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
8882 +EXPORT_SYMBOL(DWC_WAITQ_FREE);
8883 +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
8884 +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
8885 +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
8886 +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
8887 +EXPORT_SYMBOL(DWC_THREAD_RUN);
8888 +EXPORT_SYMBOL(DWC_THREAD_STOP);
8889 +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
8890 +EXPORT_SYMBOL(DWC_TASK_ALLOC);
8891 +EXPORT_SYMBOL(DWC_TASK_FREE);
8892 +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
8893 +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
8894 +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
8895 +EXPORT_SYMBOL(DWC_WORKQ_FREE);
8896 +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
8897 +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
8898 +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
8899 +
8900 +static int dwc_common_port_init_module(void)
8901 +{
8902 + int result = 0;
8903 +
8904 + printk(KERN_DEBUG "Module dwc_common_port init\n" );
8905 +
8906 +#ifdef DWC_DEBUG_MEMORY
8907 + result = dwc_memory_debug_start(NULL);
8908 + if (result) {
8909 + printk(KERN_ERR
8910 + "dwc_memory_debug_start() failed with error %d\n",
8911 + result);
8912 + return result;
8913 + }
8914 +#endif
8915 +
8916 +#ifdef DWC_NOTIFYLIB
8917 + result = dwc_alloc_notification_manager(NULL, NULL);
8918 + if (result) {
8919 + printk(KERN_ERR
8920 + "dwc_alloc_notification_manager() failed with error %d\n",
8921 + result);
8922 + return result;
8923 + }
8924 +#endif
8925 + return result;
8926 +}
8927 +
8928 +static void dwc_common_port_exit_module(void)
8929 +{
8930 + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
8931 +
8932 +#ifdef DWC_NOTIFYLIB
8933 + dwc_free_notification_manager();
8934 +#endif
8935 +
8936 +#ifdef DWC_DEBUG_MEMORY
8937 + dwc_memory_debug_stop();
8938 +#endif
8939 +}
8940 +
8941 +module_init(dwc_common_port_init_module);
8942 +module_exit(dwc_common_port_exit_module);
8943 +
8944 +MODULE_DESCRIPTION("DWC Common Library - Portable version");
8945 +MODULE_AUTHOR("Synopsys Inc.");
8946 +MODULE_LICENSE ("GPL");
8947 +
8948 +#endif /* DWC_LIBMODULE */
8949 --- /dev/null
8950 +++ b/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
8951 @@ -0,0 +1,1275 @@
8952 +#include "dwc_os.h"
8953 +#include "dwc_list.h"
8954 +
8955 +#ifdef DWC_CCLIB
8956 +# include "dwc_cc.h"
8957 +#endif
8958 +
8959 +#ifdef DWC_CRYPTOLIB
8960 +# include "dwc_modpow.h"
8961 +# include "dwc_dh.h"
8962 +# include "dwc_crypto.h"
8963 +#endif
8964 +
8965 +#ifdef DWC_NOTIFYLIB
8966 +# include "dwc_notifier.h"
8967 +#endif
8968 +
8969 +/* OS-Level Implementations */
8970 +
8971 +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
8972 +
8973 +
8974 +/* MISC */
8975 +
8976 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
8977 +{
8978 + return memset(dest, byte, size);
8979 +}
8980 +
8981 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
8982 +{
8983 + return memcpy(dest, src, size);
8984 +}
8985 +
8986 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
8987 +{
8988 + bcopy(src, dest, size);
8989 + return dest;
8990 +}
8991 +
8992 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
8993 +{
8994 + return memcmp(m1, m2, size);
8995 +}
8996 +
8997 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
8998 +{
8999 + return strncmp(s1, s2, size);
9000 +}
9001 +
9002 +int DWC_STRCMP(void *s1, void *s2)
9003 +{
9004 + return strcmp(s1, s2);
9005 +}
9006 +
9007 +int DWC_STRLEN(char const *str)
9008 +{
9009 + return strlen(str);
9010 +}
9011 +
9012 +char *DWC_STRCPY(char *to, char const *from)
9013 +{
9014 + return strcpy(to, from);
9015 +}
9016 +
9017 +char *DWC_STRDUP(char const *str)
9018 +{
9019 + int len = DWC_STRLEN(str) + 1;
9020 + char *new = DWC_ALLOC_ATOMIC(len);
9021 +
9022 + if (!new) {
9023 + return NULL;
9024 + }
9025 +
9026 + DWC_MEMCPY(new, str, len);
9027 + return new;
9028 +}
9029 +
9030 +int DWC_ATOI(char *str, int32_t *value)
9031 +{
9032 + char *end = NULL;
9033 +
9034 + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
9035 + * should be equivalent on 2's complement machines
9036 + */
9037 + *value = strtoul(str, &end, 0);
9038 + if (*end == '\0') {
9039 + return 0;
9040 + }
9041 +
9042 + return -1;
9043 +}
9044 +
9045 +int DWC_ATOUI(char *str, uint32_t *value)
9046 +{
9047 + char *end = NULL;
9048 +
9049 + *value = strtoul(str, &end, 0);
9050 + if (*end == '\0') {
9051 + return 0;
9052 + }
9053 +
9054 + return -1;
9055 +}
9056 +
9057 +
9058 +#ifdef DWC_UTFLIB
9059 +/* From usbstring.c */
9060 +
9061 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
9062 +{
9063 + int count = 0;
9064 + u8 c;
9065 + u16 uchar;
9066 +
9067 + /* this insists on correct encodings, though not minimal ones.
9068 + * BUT it currently rejects legit 4-byte UTF-8 code points,
9069 + * which need surrogate pairs. (Unicode 3.1 can use them.)
9070 + */
9071 + while (len != 0 && (c = (u8) *s++) != 0) {
9072 + if (unlikely(c & 0x80)) {
9073 + // 2-byte sequence:
9074 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
9075 + if ((c & 0xe0) == 0xc0) {
9076 + uchar = (c & 0x1f) << 6;
9077 +
9078 + c = (u8) *s++;
9079 + if ((c & 0xc0) != 0xc0)
9080 + goto fail;
9081 + c &= 0x3f;
9082 + uchar |= c;
9083 +
9084 + // 3-byte sequence (most CJKV characters):
9085 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
9086 + } else if ((c & 0xf0) == 0xe0) {
9087 + uchar = (c & 0x0f) << 12;
9088 +
9089 + c = (u8) *s++;
9090 + if ((c & 0xc0) != 0xc0)
9091 + goto fail;
9092 + c &= 0x3f;
9093 + uchar |= c << 6;
9094 +
9095 + c = (u8) *s++;
9096 + if ((c & 0xc0) != 0xc0)
9097 + goto fail;
9098 + c &= 0x3f;
9099 + uchar |= c;
9100 +
9101 + /* no bogus surrogates */
9102 + if (0xd800 <= uchar && uchar <= 0xdfff)
9103 + goto fail;
9104 +
9105 + // 4-byte sequence (surrogate pairs, currently rare):
9106 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
9107 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
9108 + // (uuuuu = wwww + 1)
9109 + // FIXME accept the surrogate code points (only)
9110 + } else
9111 + goto fail;
9112 + } else
9113 + uchar = c;
9114 + put_unaligned (cpu_to_le16 (uchar), cp++);
9115 + count++;
9116 + len--;
9117 + }
9118 + return count;
9119 +fail:
9120 + return -1;
9121 +}
9122 +
9123 +#endif /* DWC_UTFLIB */
9124 +
9125 +
9126 +/* dwc_debug.h */
9127 +
9128 +dwc_bool_t DWC_IN_IRQ(void)
9129 +{
9130 +// return in_irq();
9131 + return 0;
9132 +}
9133 +
9134 +dwc_bool_t DWC_IN_BH(void)
9135 +{
9136 +// return in_softirq();
9137 + return 0;
9138 +}
9139 +
9140 +void DWC_VPRINTF(char *format, va_list args)
9141 +{
9142 + vprintf(format, args);
9143 +}
9144 +
9145 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
9146 +{
9147 + return vsnprintf(str, size, format, args);
9148 +}
9149 +
9150 +void DWC_PRINTF(char *format, ...)
9151 +{
9152 + va_list args;
9153 +
9154 + va_start(args, format);
9155 + DWC_VPRINTF(format, args);
9156 + va_end(args);
9157 +}
9158 +
9159 +int DWC_SPRINTF(char *buffer, char *format, ...)
9160 +{
9161 + int retval;
9162 + va_list args;
9163 +
9164 + va_start(args, format);
9165 + retval = vsprintf(buffer, format, args);
9166 + va_end(args);
9167 + return retval;
9168 +}
9169 +
9170 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
9171 +{
9172 + int retval;
9173 + va_list args;
9174 +
9175 + va_start(args, format);
9176 + retval = vsnprintf(buffer, size, format, args);
9177 + va_end(args);
9178 + return retval;
9179 +}
9180 +
9181 +void __DWC_WARN(char *format, ...)
9182 +{
9183 + va_list args;
9184 +
9185 + va_start(args, format);
9186 + DWC_VPRINTF(format, args);
9187 + va_end(args);
9188 +}
9189 +
9190 +void __DWC_ERROR(char *format, ...)
9191 +{
9192 + va_list args;
9193 +
9194 + va_start(args, format);
9195 + DWC_VPRINTF(format, args);
9196 + va_end(args);
9197 +}
9198 +
9199 +void DWC_EXCEPTION(char *format, ...)
9200 +{
9201 + va_list args;
9202 +
9203 + va_start(args, format);
9204 + DWC_VPRINTF(format, args);
9205 + va_end(args);
9206 +// BUG_ON(1); ???
9207 +}
9208 +
9209 +#ifdef DEBUG
9210 +void __DWC_DEBUG(char *format, ...)
9211 +{
9212 + va_list args;
9213 +
9214 + va_start(args, format);
9215 + DWC_VPRINTF(format, args);
9216 + va_end(args);
9217 +}
9218 +#endif
9219 +
9220 +
9221 +/* dwc_mem.h */
9222 +
9223 +#if 0
9224 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
9225 + uint32_t align,
9226 + uint32_t alloc)
9227 +{
9228 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
9229 + size, align, alloc);
9230 + return (dwc_pool_t *)pool;
9231 +}
9232 +
9233 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
9234 +{
9235 + dma_pool_destroy((struct dma_pool *)pool);
9236 +}
9237 +
9238 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
9239 +{
9240 +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
9241 + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
9242 +}
9243 +
9244 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
9245 +{
9246 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
9247 + memset(..);
9248 +}
9249 +
9250 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
9251 +{
9252 + dma_pool_free(pool, vaddr, daddr);
9253 +}
9254 +#endif
9255 +
9256 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
9257 +{
9258 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
9259 + int error;
9260 +
9261 + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
9262 + sizeof(dma->segs) / sizeof(dma->segs[0]),
9263 + &dma->nsegs, BUS_DMA_NOWAIT);
9264 + if (error) {
9265 + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
9266 + (uintmax_t)size, error);
9267 + goto fail_0;
9268 + }
9269 +
9270 + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
9271 + (caddr_t *)&dma->dma_vaddr,
9272 + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
9273 + if (error) {
9274 + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
9275 + goto fail_1;
9276 + }
9277 +
9278 + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
9279 + BUS_DMA_NOWAIT, &dma->dma_map);
9280 + if (error) {
9281 + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
9282 + goto fail_2;
9283 + }
9284 +
9285 + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
9286 + size, NULL, BUS_DMA_NOWAIT);
9287 + if (error) {
9288 + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
9289 + goto fail_3;
9290 + }
9291 +
9292 + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
9293 + *dma_addr = dma->dma_paddr;
9294 + return dma->dma_vaddr;
9295 +
9296 +fail_3:
9297 + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
9298 +fail_2:
9299 + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
9300 +fail_1:
9301 + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
9302 +fail_0:
9303 + dma->dma_map = NULL;
9304 + dma->dma_vaddr = NULL;
9305 + dma->nsegs = 0;
9306 +
9307 + return NULL;
9308 +}
9309 +
9310 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
9311 +{
9312 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
9313 +
9314 + if (dma->dma_map != NULL) {
9315 + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
9316 + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9317 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
9318 + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
9319 + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
9320 + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
9321 + dma->dma_paddr = 0;
9322 + dma->dma_map = NULL;
9323 + dma->dma_vaddr = NULL;
9324 + dma->nsegs = 0;
9325 + }
9326 +}
9327 +
9328 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
9329 +{
9330 + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
9331 +}
9332 +
9333 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
9334 +{
9335 + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
9336 +}
9337 +
9338 +void __DWC_FREE(void *mem_ctx, void *addr)
9339 +{
9340 + free(addr, M_DEVBUF);
9341 +}
9342 +
9343 +
9344 +#ifdef DWC_CRYPTOLIB
9345 +/* dwc_crypto.h */
9346 +
9347 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
9348 +{
9349 + get_random_bytes(buffer, length);
9350 +}
9351 +
9352 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
9353 +{
9354 + struct crypto_blkcipher *tfm;
9355 + struct blkcipher_desc desc;
9356 + struct scatterlist sgd;
9357 + struct scatterlist sgs;
9358 +
9359 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
9360 + if (tfm == NULL) {
9361 + printk("failed to load transform for aes CBC\n");
9362 + return -1;
9363 + }
9364 +
9365 + crypto_blkcipher_setkey(tfm, key, keylen);
9366 + crypto_blkcipher_set_iv(tfm, iv, 16);
9367 +
9368 + sg_init_one(&sgd, out, messagelen);
9369 + sg_init_one(&sgs, message, messagelen);
9370 +
9371 + desc.tfm = tfm;
9372 + desc.flags = 0;
9373 +
9374 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
9375 + crypto_free_blkcipher(tfm);
9376 + DWC_ERROR("AES CBC encryption failed");
9377 + return -1;
9378 + }
9379 +
9380 + crypto_free_blkcipher(tfm);
9381 + return 0;
9382 +}
9383 +
9384 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
9385 +{
9386 + struct crypto_hash *tfm;
9387 + struct hash_desc desc;
9388 + struct scatterlist sg;
9389 +
9390 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
9391 + if (IS_ERR(tfm)) {
9392 + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
9393 + return 0;
9394 + }
9395 + desc.tfm = tfm;
9396 + desc.flags = 0;
9397 +
9398 + sg_init_one(&sg, message, len);
9399 + crypto_hash_digest(&desc, &sg, len, out);
9400 + crypto_free_hash(tfm);
9401 +
9402 + return 1;
9403 +}
9404 +
9405 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
9406 + uint8_t *key, uint32_t keylen, uint8_t *out)
9407 +{
9408 + struct crypto_hash *tfm;
9409 + struct hash_desc desc;
9410 + struct scatterlist sg;
9411 +
9412 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
9413 + if (IS_ERR(tfm)) {
9414 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
9415 + return 0;
9416 + }
9417 + desc.tfm = tfm;
9418 + desc.flags = 0;
9419 +
9420 + sg_init_one(&sg, message, messagelen);
9421 + crypto_hash_setkey(tfm, key, keylen);
9422 + crypto_hash_digest(&desc, &sg, messagelen, out);
9423 + crypto_free_hash(tfm);
9424 +
9425 + return 1;
9426 +}
9427 +
9428 +#endif /* DWC_CRYPTOLIB */
9429 +
9430 +
9431 +/* Byte Ordering Conversions */
9432 +
9433 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
9434 +{
9435 +#ifdef __LITTLE_ENDIAN
9436 + return *p;
9437 +#else
9438 + uint8_t *u_p = (uint8_t *)p;
9439 +
9440 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9441 +#endif
9442 +}
9443 +
9444 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
9445 +{
9446 +#ifdef __BIG_ENDIAN
9447 + return *p;
9448 +#else
9449 + uint8_t *u_p = (uint8_t *)p;
9450 +
9451 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9452 +#endif
9453 +}
9454 +
9455 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
9456 +{
9457 +#ifdef __LITTLE_ENDIAN
9458 + return *p;
9459 +#else
9460 + uint8_t *u_p = (uint8_t *)p;
9461 +
9462 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9463 +#endif
9464 +}
9465 +
9466 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
9467 +{
9468 +#ifdef __BIG_ENDIAN
9469 + return *p;
9470 +#else
9471 + uint8_t *u_p = (uint8_t *)p;
9472 +
9473 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9474 +#endif
9475 +}
9476 +
9477 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
9478 +{
9479 +#ifdef __LITTLE_ENDIAN
9480 + return *p;
9481 +#else
9482 + uint8_t *u_p = (uint8_t *)p;
9483 + return (u_p[1] | (u_p[0] << 8));
9484 +#endif
9485 +}
9486 +
9487 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
9488 +{
9489 +#ifdef __BIG_ENDIAN
9490 + return *p;
9491 +#else
9492 + uint8_t *u_p = (uint8_t *)p;
9493 + return (u_p[1] | (u_p[0] << 8));
9494 +#endif
9495 +}
9496 +
9497 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
9498 +{
9499 +#ifdef __LITTLE_ENDIAN
9500 + return *p;
9501 +#else
9502 + uint8_t *u_p = (uint8_t *)p;
9503 + return (u_p[1] | (u_p[0] << 8));
9504 +#endif
9505 +}
9506 +
9507 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
9508 +{
9509 +#ifdef __BIG_ENDIAN
9510 + return *p;
9511 +#else
9512 + uint8_t *u_p = (uint8_t *)p;
9513 + return (u_p[1] | (u_p[0] << 8));
9514 +#endif
9515 +}
9516 +
9517 +
9518 +/* Registers */
9519 +
9520 +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
9521 +{
9522 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9523 + bus_size_t ior = (bus_size_t)reg;
9524 +
9525 + return bus_space_read_4(io->iot, io->ioh, ior);
9526 +}
9527 +
9528 +#if 0
9529 +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
9530 +{
9531 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9532 + bus_size_t ior = (bus_size_t)reg;
9533 +
9534 + return bus_space_read_8(io->iot, io->ioh, ior);
9535 +}
9536 +#endif
9537 +
9538 +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
9539 +{
9540 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9541 + bus_size_t ior = (bus_size_t)reg;
9542 +
9543 + bus_space_write_4(io->iot, io->ioh, ior, value);
9544 +}
9545 +
9546 +#if 0
9547 +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
9548 +{
9549 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9550 + bus_size_t ior = (bus_size_t)reg;
9551 +
9552 + bus_space_write_8(io->iot, io->ioh, ior, value);
9553 +}
9554 +#endif
9555 +
9556 +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
9557 + uint32_t set_mask)
9558 +{
9559 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9560 + bus_size_t ior = (bus_size_t)reg;
9561 +
9562 + bus_space_write_4(io->iot, io->ioh, ior,
9563 + (bus_space_read_4(io->iot, io->ioh, ior) &
9564 + ~clear_mask) | set_mask);
9565 +}
9566 +
9567 +#if 0
9568 +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
9569 + uint64_t set_mask)
9570 +{
9571 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9572 + bus_size_t ior = (bus_size_t)reg;
9573 +
9574 + bus_space_write_8(io->iot, io->ioh, ior,
9575 + (bus_space_read_8(io->iot, io->ioh, ior) &
9576 + ~clear_mask) | set_mask);
9577 +}
9578 +#endif
9579 +
9580 +
9581 +/* Locking */
9582 +
9583 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
9584 +{
9585 + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
9586 +
9587 + if (!sl) {
9588 + DWC_ERROR("Cannot allocate memory for spinlock");
9589 + return NULL;
9590 + }
9591 +
9592 + simple_lock_init(sl);
9593 + return (dwc_spinlock_t *)sl;
9594 +}
9595 +
9596 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
9597 +{
9598 + struct simplelock *sl = (struct simplelock *)lock;
9599 +
9600 + DWC_FREE(sl);
9601 +}
9602 +
9603 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
9604 +{
9605 + simple_lock((struct simplelock *)lock);
9606 +}
9607 +
9608 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
9609 +{
9610 + simple_unlock((struct simplelock *)lock);
9611 +}
9612 +
9613 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
9614 +{
9615 + simple_lock((struct simplelock *)lock);
9616 + *flags = splbio();
9617 +}
9618 +
9619 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
9620 +{
9621 + splx(flags);
9622 + simple_unlock((struct simplelock *)lock);
9623 +}
9624 +
9625 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
9626 +{
9627 + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
9628 +
9629 + if (!mutex) {
9630 + DWC_ERROR("Cannot allocate memory for mutex");
9631 + return NULL;
9632 + }
9633 +
9634 + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
9635 + return mutex;
9636 +}
9637 +
9638 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
9639 +#else
9640 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
9641 +{
9642 + DWC_FREE(mutex);
9643 +}
9644 +#endif
9645 +
9646 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
9647 +{
9648 + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
9649 +}
9650 +
9651 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
9652 +{
9653 + int status;
9654 +
9655 + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
9656 + return status == 0;
9657 +}
9658 +
9659 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
9660 +{
9661 + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
9662 +}
9663 +
9664 +
9665 +/* Timing */
9666 +
9667 +void DWC_UDELAY(uint32_t usecs)
9668 +{
9669 + DELAY(usecs);
9670 +}
9671 +
9672 +void DWC_MDELAY(uint32_t msecs)
9673 +{
9674 + do {
9675 + DELAY(1000);
9676 + } while (--msecs);
9677 +}
9678 +
9679 +void DWC_MSLEEP(uint32_t msecs)
9680 +{
9681 + struct timeval tv;
9682 +
9683 + tv.tv_sec = msecs / 1000;
9684 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
9685 + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
9686 +}
9687 +
9688 +uint32_t DWC_TIME(void)
9689 +{
9690 + struct timeval tv;
9691 +
9692 + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
9693 + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
9694 +}
9695 +
9696 +
9697 +/* Timers */
9698 +
9699 +struct dwc_timer {
9700 + struct callout t;
9701 + char *name;
9702 + dwc_spinlock_t *lock;
9703 + dwc_timer_callback_t cb;
9704 + void *data;
9705 +};
9706 +
9707 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
9708 +{
9709 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
9710 +
9711 + if (!t) {
9712 + DWC_ERROR("Cannot allocate memory for timer");
9713 + return NULL;
9714 + }
9715 +
9716 + callout_init(&t->t);
9717 +
9718 + t->name = DWC_STRDUP(name);
9719 + if (!t->name) {
9720 + DWC_ERROR("Cannot allocate memory for timer->name");
9721 + goto no_name;
9722 + }
9723 +
9724 + t->lock = DWC_SPINLOCK_ALLOC();
9725 + if (!t->lock) {
9726 + DWC_ERROR("Cannot allocate memory for timer->lock");
9727 + goto no_lock;
9728 + }
9729 +
9730 + t->cb = cb;
9731 + t->data = data;
9732 +
9733 + return t;
9734 +
9735 + no_lock:
9736 + DWC_FREE(t->name);
9737 + no_name:
9738 + DWC_FREE(t);
9739 +
9740 + return NULL;
9741 +}
9742 +
9743 +void DWC_TIMER_FREE(dwc_timer_t *timer)
9744 +{
9745 + callout_stop(&timer->t);
9746 + DWC_SPINLOCK_FREE(timer->lock);
9747 + DWC_FREE(timer->name);
9748 + DWC_FREE(timer);
9749 +}
9750 +
9751 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
9752 +{
9753 + struct timeval tv;
9754 +
9755 + tv.tv_sec = time / 1000;
9756 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
9757 + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
9758 +}
9759 +
9760 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
9761 +{
9762 + callout_stop(&timer->t);
9763 +}
9764 +
9765 +
9766 +/* Wait Queues */
9767 +
9768 +struct dwc_waitq {
9769 + struct simplelock lock;
9770 + int abort;
9771 +};
9772 +
9773 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
9774 +{
9775 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
9776 +
9777 + if (!wq) {
9778 + DWC_ERROR("Cannot allocate memory for waitqueue");
9779 + return NULL;
9780 + }
9781 +
9782 + simple_lock_init(&wq->lock);
9783 + wq->abort = 0;
9784 +
9785 + return wq;
9786 +}
9787 +
9788 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
9789 +{
9790 + DWC_FREE(wq);
9791 +}
9792 +
9793 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
9794 +{
9795 + int ipl;
9796 + int result = 0;
9797 +
9798 + simple_lock(&wq->lock);
9799 + ipl = splbio();
9800 +
9801 + /* Skip the sleep if already aborted or triggered */
9802 + if (!wq->abort && !cond(data)) {
9803 + splx(ipl);
9804 + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
9805 + ipl = splbio();
9806 + }
9807 +
9808 + if (result == 0) { // awoken
9809 + if (wq->abort) {
9810 + wq->abort = 0;
9811 + result = -DWC_E_ABORT;
9812 + } else {
9813 + result = 0;
9814 + }
9815 +
9816 + splx(ipl);
9817 + simple_unlock(&wq->lock);
9818 + } else {
9819 + wq->abort = 0;
9820 + splx(ipl);
9821 + simple_unlock(&wq->lock);
9822 +
9823 + if (result == ERESTART) { // signaled - restart
9824 + result = -DWC_E_RESTART;
9825 + } else { // signaled - must be EINTR
9826 + result = -DWC_E_ABORT;
9827 + }
9828 + }
9829 +
9830 + return result;
9831 +}
9832 +
9833 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
9834 + void *data, int32_t msecs)
9835 +{
9836 + struct timeval tv, tv1, tv2;
9837 + int ipl;
9838 + int result = 0;
9839 +
9840 + tv.tv_sec = msecs / 1000;
9841 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
9842 +
9843 + simple_lock(&wq->lock);
9844 + ipl = splbio();
9845 +
9846 + /* Skip the sleep if already aborted or triggered */
9847 + if (!wq->abort && !cond(data)) {
9848 + splx(ipl);
9849 + getmicrouptime(&tv1);
9850 + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
9851 + getmicrouptime(&tv2);
9852 + ipl = splbio();
9853 + }
9854 +
9855 + if (result == 0) { // awoken
9856 + if (wq->abort) {
9857 + wq->abort = 0;
9858 + splx(ipl);
9859 + simple_unlock(&wq->lock);
9860 + result = -DWC_E_ABORT;
9861 + } else {
9862 + splx(ipl);
9863 + simple_unlock(&wq->lock);
9864 +
9865 + tv2.tv_usec -= tv1.tv_usec;
9866 + if (tv2.tv_usec < 0) {
9867 + tv2.tv_usec += 1000000;
9868 + tv2.tv_sec--;
9869 + }
9870 +
9871 + tv2.tv_sec -= tv1.tv_sec;
9872 + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
9873 + result = msecs - result;
9874 + if (result <= 0)
9875 + result = 1;
9876 + }
9877 + } else {
9878 + wq->abort = 0;
9879 + splx(ipl);
9880 + simple_unlock(&wq->lock);
9881 +
9882 + if (result == ERESTART) { // signaled - restart
9883 + result = -DWC_E_RESTART;
9884 +
9885 + } else if (result == EINTR) { // signaled - interrupt
9886 + result = -DWC_E_ABORT;
9887 +
9888 + } else { // timed out
9889 + result = -DWC_E_TIMEOUT;
9890 + }
9891 + }
9892 +
9893 + return result;
9894 +}
9895 +
9896 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
9897 +{
9898 + wakeup(wq);
9899 +}
9900 +
9901 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
9902 +{
9903 + int ipl;
9904 +
9905 + simple_lock(&wq->lock);
9906 + ipl = splbio();
9907 + wq->abort = 1;
9908 + wakeup(wq);
9909 + splx(ipl);
9910 + simple_unlock(&wq->lock);
9911 +}
9912 +
9913 +
9914 +/* Threading */
9915 +
9916 +struct dwc_thread {
9917 + struct proc *proc;
9918 + int abort;
9919 +};
9920 +
9921 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
9922 +{
9923 + int retval;
9924 + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
9925 +
9926 + if (!thread) {
9927 + return NULL;
9928 + }
9929 +
9930 + thread->abort = 0;
9931 + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
9932 + "%s", name);
9933 + if (retval) {
9934 + DWC_FREE(thread);
9935 + return NULL;
9936 + }
9937 +
9938 + return thread;
9939 +}
9940 +
9941 +int DWC_THREAD_STOP(dwc_thread_t *thread)
9942 +{
9943 + int retval;
9944 +
9945 + thread->abort = 1;
9946 + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
9947 +
9948 + if (retval == 0) {
9949 + /* DWC_THREAD_EXIT() will free the thread struct */
9950 + return 0;
9951 + }
9952 +
9953 + /* NOTE: We leak the thread struct if thread doesn't die */
9954 +
9955 + if (retval == EWOULDBLOCK) {
9956 + return -DWC_E_TIMEOUT;
9957 + }
9958 +
9959 + return -DWC_E_UNKNOWN;
9960 +}
9961 +
9962 +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
9963 +{
9964 + return thread->abort;
9965 +}
9966 +
9967 +void DWC_THREAD_EXIT(dwc_thread_t *thread)
9968 +{
9969 + wakeup(&thread->abort);
9970 + DWC_FREE(thread);
9971 + kthread_exit(0);
9972 +}
9973 +
9974 +/* tasklets
9975 + - Runs in interrupt context (cannot sleep)
9976 + - Each tasklet runs on a single CPU
9977 + - Different tasklets can be running simultaneously on different CPUs
9978 + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
9979 + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
9980 + */
9981 +struct dwc_tasklet {
9982 + dwc_tasklet_callback_t cb;
9983 + void *data;
9984 +};
9985 +
9986 +static void tasklet_callback(void *data)
9987 +{
9988 + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
9989 +
9990 + task->cb(task->data);
9991 +}
9992 +
9993 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
9994 +{
9995 + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
9996 +
9997 + if (task) {
9998 + task->cb = cb;
9999 + task->data = data;
10000 + } else {
10001 + DWC_ERROR("Cannot allocate memory for tasklet");
10002 + }
10003 +
10004 + return task;
10005 +}
10006 +
10007 +void DWC_TASK_FREE(dwc_tasklet_t *task)
10008 +{
10009 + DWC_FREE(task);
10010 +}
10011 +
10012 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
10013 +{
10014 + tasklet_callback(task);
10015 +}
10016 +
10017 +
10018 +/* workqueues
10019 + - Runs in process context (can sleep)
10020 + */
10021 +typedef struct work_container {
10022 + dwc_work_callback_t cb;
10023 + void *data;
10024 + dwc_workq_t *wq;
10025 + char *name;
10026 + int hz;
10027 + struct work task;
10028 +} work_container_t;
10029 +
10030 +struct dwc_workq {
10031 + struct workqueue *taskq;
10032 + dwc_spinlock_t *lock;
10033 + dwc_waitq_t *waitq;
10034 + int pending;
10035 + struct work_container *container;
10036 +};
10037 +
10038 +static void do_work(struct work *task, void *data)
10039 +{
10040 + dwc_workq_t *wq = (dwc_workq_t *)data;
10041 + work_container_t *container = wq->container;
10042 + dwc_irqflags_t flags;
10043 +
10044 + if (container->hz) {
10045 + tsleep(container, 0, "dw3wrk", container->hz);
10046 + }
10047 +
10048 + container->cb(container->data);
10049 + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
10050 +
10051 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10052 + if (container->name)
10053 + DWC_FREE(container->name);
10054 + DWC_FREE(container);
10055 + wq->pending--;
10056 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10057 + DWC_WAITQ_TRIGGER(wq->waitq);
10058 +}
10059 +
10060 +static int work_done(void *data)
10061 +{
10062 + dwc_workq_t *workq = (dwc_workq_t *)data;
10063 +
10064 + return workq->pending == 0;
10065 +}
10066 +
10067 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
10068 +{
10069 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
10070 +}
10071 +
10072 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
10073 +{
10074 + int result;
10075 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
10076 +
10077 + if (!wq) {
10078 + DWC_ERROR("Cannot allocate memory for workqueue");
10079 + return NULL;
10080 + }
10081 +
10082 + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
10083 + IPL_BIO, 0);
10084 + if (result) {
10085 + DWC_ERROR("Cannot create workqueue");
10086 + goto no_taskq;
10087 + }
10088 +
10089 + wq->pending = 0;
10090 +
10091 + wq->lock = DWC_SPINLOCK_ALLOC();
10092 + if (!wq->lock) {
10093 + DWC_ERROR("Cannot allocate memory for spinlock");
10094 + goto no_lock;
10095 + }
10096 +
10097 + wq->waitq = DWC_WAITQ_ALLOC();
10098 + if (!wq->waitq) {
10099 + DWC_ERROR("Cannot allocate memory for waitqueue");
10100 + goto no_waitq;
10101 + }
10102 +
10103 + return wq;
10104 +
10105 + no_waitq:
10106 + DWC_SPINLOCK_FREE(wq->lock);
10107 + no_lock:
10108 + workqueue_destroy(wq->taskq);
10109 + no_taskq:
10110 + DWC_FREE(wq);
10111 +
10112 + return NULL;
10113 +}
10114 +
10115 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
10116 +{
10117 +#ifdef DEBUG
10118 + dwc_irqflags_t flags;
10119 +
10120 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10121 +
10122 + if (wq->pending != 0) {
10123 + struct work_container *container = wq->container;
10124 +
10125 + DWC_ERROR("Destroying work queue with pending work");
10126 +
10127 + if (container && container->name) {
10128 + DWC_ERROR("Work %s still pending", container->name);
10129 + }
10130 + }
10131 +
10132 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10133 +#endif
10134 + DWC_WAITQ_FREE(wq->waitq);
10135 + DWC_SPINLOCK_FREE(wq->lock);
10136 + workqueue_destroy(wq->taskq);
10137 + DWC_FREE(wq);
10138 +}
10139 +
10140 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
10141 + char *format, ...)
10142 +{
10143 + dwc_irqflags_t flags;
10144 + work_container_t *container;
10145 + static char name[128];
10146 + va_list args;
10147 +
10148 + va_start(args, format);
10149 + DWC_VSNPRINTF(name, 128, format, args);
10150 + va_end(args);
10151 +
10152 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10153 + wq->pending++;
10154 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10155 + DWC_WAITQ_TRIGGER(wq->waitq);
10156 +
10157 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
10158 + if (!container) {
10159 + DWC_ERROR("Cannot allocate memory for container");
10160 + return;
10161 + }
10162 +
10163 + container->name = DWC_STRDUP(name);
10164 + if (!container->name) {
10165 + DWC_ERROR("Cannot allocate memory for container->name");
10166 + DWC_FREE(container);
10167 + return;
10168 + }
10169 +
10170 + container->cb = cb;
10171 + container->data = data;
10172 + container->wq = wq;
10173 + container->hz = 0;
10174 + wq->container = container;
10175 +
10176 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
10177 + workqueue_enqueue(wq->taskq, &container->task);
10178 +}
10179 +
10180 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
10181 + void *data, uint32_t time, char *format, ...)
10182 +{
10183 + dwc_irqflags_t flags;
10184 + work_container_t *container;
10185 + static char name[128];
10186 + struct timeval tv;
10187 + va_list args;
10188 +
10189 + va_start(args, format);
10190 + DWC_VSNPRINTF(name, 128, format, args);
10191 + va_end(args);
10192 +
10193 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10194 + wq->pending++;
10195 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10196 + DWC_WAITQ_TRIGGER(wq->waitq);
10197 +
10198 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
10199 + if (!container) {
10200 + DWC_ERROR("Cannot allocate memory for container");
10201 + return;
10202 + }
10203 +
10204 + container->name = DWC_STRDUP(name);
10205 + if (!container->name) {
10206 + DWC_ERROR("Cannot allocate memory for container->name");
10207 + DWC_FREE(container);
10208 + return;
10209 + }
10210 +
10211 + container->cb = cb;
10212 + container->data = data;
10213 + container->wq = wq;
10214 + tv.tv_sec = time / 1000;
10215 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
10216 + container->hz = tvtohz(&tv);
10217 + wq->container = container;
10218 +
10219 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
10220 + workqueue_enqueue(wq->taskq, &container->task);
10221 +}
10222 +
10223 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
10224 +{
10225 + return wq->pending;
10226 +}
10227 --- /dev/null
10228 +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.c
10229 @@ -0,0 +1,308 @@
10230 +/* =========================================================================
10231 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
10232 + * $Revision: #5 $
10233 + * $Date: 2010/09/28 $
10234 + * $Change: 1596182 $
10235 + *
10236 + * Synopsys Portability Library Software and documentation
10237 + * (hereinafter, "Software") is an Unsupported proprietary work of
10238 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10239 + * between Synopsys and you.
10240 + *
10241 + * The Software IS NOT an item of Licensed Software or Licensed Product
10242 + * under any End User Software License Agreement or Agreement for
10243 + * Licensed Product with Synopsys or any supplement thereto. You are
10244 + * permitted to use and redistribute this Software in source and binary
10245 + * forms, with or without modification, provided that redistributions
10246 + * of source code must retain this notice. You may not view, use,
10247 + * disclose, copy or distribute this file or any information contained
10248 + * herein except pursuant to this license grant from Synopsys. If you
10249 + * do not agree with this notice, including the disclaimer below, then
10250 + * you are not authorized to use the Software.
10251 + *
10252 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10253 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10254 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10255 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10256 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10257 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10258 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10259 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10260 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10261 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10262 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10263 + * DAMAGE.
10264 + * ========================================================================= */
10265 +
10266 +/** @file
10267 + * This file contains the WUSB cryptographic routines.
10268 + */
10269 +
10270 +#ifdef DWC_CRYPTOLIB
10271 +
10272 +#include "dwc_crypto.h"
10273 +#include "usb.h"
10274 +
10275 +#ifdef DEBUG
10276 +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
10277 +{
10278 + int i;
10279 + DWC_PRINTF("%s: ", name);
10280 + for (i=0; i<len; i++) {
10281 + DWC_PRINTF("%02x ", bytes[i]);
10282 + }
10283 + DWC_PRINTF("\n");
10284 +}
10285 +#else
10286 +#define dump_bytes(x...)
10287 +#endif
10288 +
10289 +/* Display a block */
10290 +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
10291 +{
10292 +#ifdef DWC_DEBUG_CRYPTO
10293 + int i, blksize = 16;
10294 +
10295 + DWC_DEBUG("%s", prefix);
10296 +
10297 + if (suffix == NULL) {
10298 + suffix = "\n";
10299 + blksize = a;
10300 + }
10301 +
10302 + for (i = 0; i < blksize; i++)
10303 + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
10304 + DWC_PRINT(suffix);
10305 +#endif
10306 +}
10307 +
10308 +/**
10309 + * Encrypts an array of bytes using the AES encryption engine.
10310 + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
10311 + * in-place.
10312 + *
10313 + * @return 0 on success, negative error code on error.
10314 + */
10315 +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
10316 +{
10317 + u8 block_t[16];
10318 + DWC_MEMSET(block_t, 0, 16);
10319 +
10320 + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
10321 +}
10322 +
10323 +/**
10324 + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
10325 + * This function takes a data string and returns the encrypted CBC
10326 + * Counter-mode MIC.
10327 + *
10328 + * @param key The 128-bit symmetric key.
10329 + * @param nonce The CCM nonce.
10330 + * @param label The unique 14-byte ASCII text label.
10331 + * @param bytes The byte array to be encrypted.
10332 + * @param len Length of the byte array.
10333 + * @param result Byte array to receive the 8-byte encrypted MIC.
10334 + */
10335 +void dwc_wusb_cmf(u8 *key, u8 *nonce,
10336 + char *label, u8 *bytes, int len, u8 *result)
10337 +{
10338 + u8 block_m[16];
10339 + u8 block_x[16];
10340 + u8 block_t[8];
10341 + int idx, blkNum;
10342 + u16 la = (u16)(len + 14);
10343 +
10344 + /* Set the AES-128 key */
10345 + //dwc_aes_setkey(tfm, key, 16);
10346 +
10347 + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
10348 + block_m[0] = 0x59;
10349 + for (idx = 0; idx < 13; idx++)
10350 + block_m[idx + 1] = nonce[idx];
10351 + block_m[14] = 0;
10352 + block_m[15] = 0;
10353 +
10354 + /* Produce the CBC IV */
10355 + dwc_wusb_aes_encrypt(block_m, key, block_x);
10356 + show_block(block_m, "CBC IV in: ", "\n", 0);
10357 + show_block(block_x, "CBC IV out:", "\n", 0);
10358 +
10359 + /* Fill block B1 from l(a) = Blen + 14, and A */
10360 + block_x[0] ^= (u8)(la >> 8);
10361 + block_x[1] ^= (u8)la;
10362 + for (idx = 0; idx < 14; idx++)
10363 + block_x[idx + 2] ^= label[idx];
10364 + show_block(block_x, "After xor: ", "b1\n", 16);
10365 +
10366 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10367 + show_block(block_x, "After AES: ", "b1\n", 16);
10368 +
10369 + idx = 0;
10370 + blkNum = 0;
10371 +
10372 + /* Fill remaining blocks with B */
10373 + while (len-- > 0) {
10374 + block_x[idx] ^= *bytes++;
10375 + if (++idx >= 16) {
10376 + idx = 0;
10377 + show_block(block_x, "After xor: ", "\n", blkNum);
10378 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10379 + show_block(block_x, "After AES: ", "\n", blkNum);
10380 + blkNum++;
10381 + }
10382 + }
10383 +
10384 + /* Handle partial last block */
10385 + if (idx > 0) {
10386 + show_block(block_x, "After xor: ", "\n", blkNum);
10387 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10388 + show_block(block_x, "After AES: ", "\n", blkNum);
10389 + }
10390 +
10391 + /* Save the MIC tag */
10392 + DWC_MEMCPY(block_t, block_x, 8);
10393 + show_block(block_t, "MIC tag : ", NULL, 8);
10394 +
10395 + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
10396 + block_m[0] = 0x01;
10397 + block_m[14] = 0;
10398 + block_m[15] = 0;
10399 +
10400 + /* Encrypt the counter */
10401 + dwc_wusb_aes_encrypt(block_m, key, block_x);
10402 + show_block(block_x, "CTR[MIC] : ", NULL, 8);
10403 +
10404 + /* XOR with MIC tag */
10405 + for (idx = 0; idx < 8; idx++) {
10406 + block_t[idx] ^= block_x[idx];
10407 + }
10408 +
10409 + /* Return result to caller */
10410 + DWC_MEMCPY(result, block_t, 8);
10411 + show_block(result, "CCM-MIC : ", NULL, 8);
10412 +
10413 +}
10414 +
10415 +/**
10416 + * The PRF function described in section 6.5 of the WUSB spec. This function
10417 + * concatenates MIC values returned from dwc_cmf() to create a value of
10418 + * the requested length.
10419 + *
10420 + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
10421 + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
10422 + * @param result Byte array to receive the result.
10423 + */
10424 +void dwc_wusb_prf(int prf_len, u8 *key,
10425 + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
10426 +{
10427 + int i;
10428 +
10429 + nonce[0] = 0;
10430 + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
10431 + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
10432 + result += 8;
10433 + }
10434 +}
10435 +
10436 +/**
10437 + * Fills in CCM Nonce per the WUSB spec.
10438 + *
10439 + * @param[in] haddr Host address.
10440 + * @param[in] daddr Device address.
10441 + * @param[in] tkid Session Key(PTK) identifier.
10442 + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
10443 + */
10444 +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
10445 + uint8_t *nonce)
10446 +{
10447 +
10448 + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
10449 +
10450 + DWC_MEMSET(&nonce[0], 0, 16);
10451 +
10452 + DWC_MEMCPY(&nonce[6], tkid, 3);
10453 + nonce[9] = daddr & 0xFF;
10454 + nonce[10] = (daddr >> 8) & 0xFF;
10455 + nonce[11] = haddr & 0xFF;
10456 + nonce[12] = (haddr >> 8) & 0xFF;
10457 +
10458 + dump_bytes("CCM nonce", nonce, 16);
10459 +}
10460 +
10461 +/**
10462 + * Generates a 16-byte cryptographic-grade random number for the Host/Device
10463 + * Nonce.
10464 + */
10465 +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
10466 +{
10467 + uint8_t inonce[16];
10468 + uint32_t temp[4];
10469 +
10470 + /* Fill in the Nonce */
10471 + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
10472 + inonce[9] = addr & 0xFF;
10473 + inonce[10] = (addr >> 8) & 0xFF;
10474 + inonce[11] = inonce[9];
10475 + inonce[12] = inonce[10];
10476 +
10477 + /* Collect "randomness samples" */
10478 + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
10479 +
10480 + dwc_wusb_prf_128((uint8_t *)temp, nonce,
10481 + "Random Numbers", (uint8_t *)temp, sizeof(temp),
10482 + nonce);
10483 +}
10484 +
10485 +/**
10486 + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
10487 + * WUSB spec.
10488 + *
10489 + * @param[in] ccm_nonce Pointer to CCM Nonce.
10490 + * @param[in] mk Master Key to derive the session from
10491 + * @param[in] hnonce Pointer to Host Nonce.
10492 + * @param[in] dnonce Pointer to Device Nonce.
10493 + * @param[out] kck Pointer to where the KCK output is to be written.
10494 + * @param[out] ptk Pointer to where the PTK output is to be written.
10495 + */
10496 +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
10497 + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
10498 +{
10499 + uint8_t idata[32];
10500 + uint8_t odata[32];
10501 +
10502 + dump_bytes("ck", mk, 16);
10503 + dump_bytes("hnonce", hnonce, 16);
10504 + dump_bytes("dnonce", dnonce, 16);
10505 +
10506 + /* The data is the HNonce and DNonce concatenated */
10507 + DWC_MEMCPY(&idata[0], hnonce, 16);
10508 + DWC_MEMCPY(&idata[16], dnonce, 16);
10509 +
10510 + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
10511 +
10512 + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
10513 + DWC_MEMCPY(kck, &odata[0], 16);
10514 + DWC_MEMCPY(ptk, &odata[16], 16);
10515 +
10516 + dump_bytes("kck", kck, 16);
10517 + dump_bytes("ptk", ptk, 16);
10518 +}
10519 +
10520 +/**
10521 + * Generates the Message Integrity Code over the Handshake data per the
10522 + * WUSB spec.
10523 + *
10524 + * @param ccm_nonce Pointer to CCM Nonce.
10525 + * @param kck Pointer to Key Confirmation Key.
10526 + * @param data Pointer to Handshake data to be checked.
10527 + * @param mic Pointer to where the MIC output is to be written.
10528 + */
10529 +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
10530 + uint8_t *data, uint8_t *mic)
10531 +{
10532 +
10533 + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
10534 + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
10535 +}
10536 +
10537 +#endif /* DWC_CRYPTOLIB */
10538 --- /dev/null
10539 +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.h
10540 @@ -0,0 +1,111 @@
10541 +/* =========================================================================
10542 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
10543 + * $Revision: #3 $
10544 + * $Date: 2010/09/28 $
10545 + * $Change: 1596182 $
10546 + *
10547 + * Synopsys Portability Library Software and documentation
10548 + * (hereinafter, "Software") is an Unsupported proprietary work of
10549 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10550 + * between Synopsys and you.
10551 + *
10552 + * The Software IS NOT an item of Licensed Software or Licensed Product
10553 + * under any End User Software License Agreement or Agreement for
10554 + * Licensed Product with Synopsys or any supplement thereto. You are
10555 + * permitted to use and redistribute this Software in source and binary
10556 + * forms, with or without modification, provided that redistributions
10557 + * of source code must retain this notice. You may not view, use,
10558 + * disclose, copy or distribute this file or any information contained
10559 + * herein except pursuant to this license grant from Synopsys. If you
10560 + * do not agree with this notice, including the disclaimer below, then
10561 + * you are not authorized to use the Software.
10562 + *
10563 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10564 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10565 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10566 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10567 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10568 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10569 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10570 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10571 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10572 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10573 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10574 + * DAMAGE.
10575 + * ========================================================================= */
10576 +
10577 +#ifndef _DWC_CRYPTO_H_
10578 +#define _DWC_CRYPTO_H_
10579 +
10580 +#ifdef __cplusplus
10581 +extern "C" {
10582 +#endif
10583 +
10584 +/** @file
10585 + *
10586 + * This file contains declarations for the WUSB Cryptographic routines as
10587 + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
10588 + * modules.
10589 + */
10590 +
10591 +#include "dwc_os.h"
10592 +
10593 +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
10594 +
10595 +void dwc_wusb_cmf(u8 *key, u8 *nonce,
10596 + char *label, u8 *bytes, int len, u8 *result);
10597 +void dwc_wusb_prf(int prf_len, u8 *key,
10598 + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
10599 +
10600 +/**
10601 + * The PRF-64 function described in section 6.5 of the WUSB spec.
10602 + *
10603 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10604 + */
10605 +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
10606 + char *label, u8 *bytes, int len, u8 *result)
10607 +{
10608 + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
10609 +}
10610 +
10611 +/**
10612 + * The PRF-128 function described in section 6.5 of the WUSB spec.
10613 + *
10614 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10615 + */
10616 +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
10617 + char *label, u8 *bytes, int len, u8 *result)
10618 +{
10619 + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
10620 +}
10621 +
10622 +/**
10623 + * The PRF-256 function described in section 6.5 of the WUSB spec.
10624 + *
10625 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10626 + */
10627 +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
10628 + char *label, u8 *bytes, int len, u8 *result)
10629 +{
10630 + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
10631 +}
10632 +
10633 +
10634 +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
10635 + uint8_t *nonce);
10636 +void dwc_wusb_gen_nonce(uint16_t addr,
10637 + uint8_t *nonce);
10638 +
10639 +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
10640 + uint8_t *hnonce, uint8_t *dnonce,
10641 + uint8_t *kck, uint8_t *ptk);
10642 +
10643 +
10644 +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
10645 + *kck, uint8_t *data, uint8_t *mic);
10646 +
10647 +#ifdef __cplusplus
10648 +}
10649 +#endif
10650 +
10651 +#endif /* _DWC_CRYPTO_H_ */
10652 --- /dev/null
10653 +++ b/drivers/usb/host/dwc_common_port/dwc_dh.c
10654 @@ -0,0 +1,291 @@
10655 +/* =========================================================================
10656 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
10657 + * $Revision: #3 $
10658 + * $Date: 2010/09/28 $
10659 + * $Change: 1596182 $
10660 + *
10661 + * Synopsys Portability Library Software and documentation
10662 + * (hereinafter, "Software") is an Unsupported proprietary work of
10663 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10664 + * between Synopsys and you.
10665 + *
10666 + * The Software IS NOT an item of Licensed Software or Licensed Product
10667 + * under any End User Software License Agreement or Agreement for
10668 + * Licensed Product with Synopsys or any supplement thereto. You are
10669 + * permitted to use and redistribute this Software in source and binary
10670 + * forms, with or without modification, provided that redistributions
10671 + * of source code must retain this notice. You may not view, use,
10672 + * disclose, copy or distribute this file or any information contained
10673 + * herein except pursuant to this license grant from Synopsys. If you
10674 + * do not agree with this notice, including the disclaimer below, then
10675 + * you are not authorized to use the Software.
10676 + *
10677 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10678 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10679 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10680 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10681 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10682 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10683 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10684 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10685 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10686 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10687 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10688 + * DAMAGE.
10689 + * ========================================================================= */
10690 +#ifdef DWC_CRYPTOLIB
10691 +
10692 +#ifndef CONFIG_MACH_IPMATE
10693 +
10694 +#include "dwc_dh.h"
10695 +#include "dwc_modpow.h"
10696 +
10697 +#ifdef DEBUG
10698 +/* This function prints out a buffer in the format described in the Association
10699 + * Model specification. */
10700 +static void dh_dump(char *str, void *_num, int len)
10701 +{
10702 + uint8_t *num = _num;
10703 + int i;
10704 + DWC_PRINTF("%s\n", str);
10705 + for (i = 0; i < len; i ++) {
10706 + DWC_PRINTF("%02x", num[i]);
10707 + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
10708 + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
10709 + }
10710 +
10711 + DWC_PRINTF("\n");
10712 +}
10713 +#else
10714 +#define dh_dump(_x...) do {; } while(0)
10715 +#endif
10716 +
10717 +/* Constant g value */
10718 +static __u32 dh_g[] = {
10719 + 0x02000000,
10720 +};
10721 +
10722 +/* Constant p value */
10723 +static __u32 dh_p[] = {
10724 + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
10725 + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
10726 + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
10727 + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
10728 + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
10729 + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
10730 + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
10731 + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
10732 + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
10733 + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
10734 + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
10735 + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
10736 +};
10737 +
10738 +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
10739 +{
10740 + uint8_t *in = _in;
10741 + uint8_t *out = _out;
10742 + int i;
10743 + for (i=0; i<len; i++) {
10744 + out[i] = in[len-1-i];
10745 + }
10746 +}
10747 +
10748 +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
10749 + * big endian numbers of size len, in bytes. Each len value must be a multiple
10750 + * of 4. */
10751 +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
10752 + void *exp, uint32_t exp_len,
10753 + void *mod, uint32_t mod_len,
10754 + void *out)
10755 +{
10756 + /* modpow() takes little endian numbers. AM uses big-endian. This
10757 + * function swaps bytes of numbers before passing onto modpow. */
10758 +
10759 + int retval = 0;
10760 + uint32_t *result;
10761 +
10762 + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
10763 + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
10764 + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
10765 +
10766 + dh_swap_bytes(num, &bignum_num[1], num_len);
10767 + bignum_num[0] = num_len / 4;
10768 +
10769 + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
10770 + bignum_exp[0] = exp_len / 4;
10771 +
10772 + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
10773 + bignum_mod[0] = mod_len / 4;
10774 +
10775 + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
10776 + if (!result) {
10777 + retval = -1;
10778 + goto dh_modpow_nomem;
10779 + }
10780 +
10781 + dh_swap_bytes(&result[1], out, result[0] * 4);
10782 + dwc_free(mem_ctx, result);
10783 +
10784 + dh_modpow_nomem:
10785 + dwc_free(mem_ctx, bignum_num);
10786 + dwc_free(mem_ctx, bignum_exp);
10787 + dwc_free(mem_ctx, bignum_mod);
10788 + return retval;
10789 +}
10790 +
10791 +
10792 +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
10793 +{
10794 + int retval;
10795 + uint8_t m3[385];
10796 +
10797 +#ifndef DH_TEST_VECTORS
10798 + DWC_RANDOM_BYTES(exp, 32);
10799 +#endif
10800 +
10801 + /* Compute the pkd */
10802 + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
10803 + exp, 32,
10804 + dh_p, 384, pk))) {
10805 + return retval;
10806 + }
10807 +
10808 + m3[384] = nd;
10809 + DWC_MEMCPY(&m3[0], pk, 384);
10810 + DWC_SHA256(m3, 385, hash);
10811 +
10812 + dh_dump("PK", pk, 384);
10813 + dh_dump("SHA-256(M3)", hash, 32);
10814 + return 0;
10815 +}
10816 +
10817 +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
10818 + uint8_t *exp, int is_host,
10819 + char *dd, uint8_t *ck, uint8_t *kdk)
10820 +{
10821 + int retval;
10822 + uint8_t mv[784];
10823 + uint8_t sha_result[32];
10824 + uint8_t dhkey[384];
10825 + uint8_t shared_secret[384];
10826 + char *message;
10827 + uint32_t vd;
10828 +
10829 + uint8_t *pk;
10830 +
10831 + if (is_host) {
10832 + pk = pkd;
10833 + }
10834 + else {
10835 + pk = pkh;
10836 + }
10837 +
10838 + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
10839 + exp, 32,
10840 + dh_p, 384, shared_secret))) {
10841 + return retval;
10842 + }
10843 + dh_dump("Shared Secret", shared_secret, 384);
10844 +
10845 + DWC_SHA256(shared_secret, 384, dhkey);
10846 + dh_dump("DHKEY", dhkey, 384);
10847 +
10848 + DWC_MEMCPY(&mv[0], pkd, 384);
10849 + DWC_MEMCPY(&mv[384], pkh, 384);
10850 + DWC_MEMCPY(&mv[768], "displayed digest", 16);
10851 + dh_dump("MV", mv, 784);
10852 +
10853 + DWC_SHA256(mv, 784, sha_result);
10854 + dh_dump("SHA-256(MV)", sha_result, 32);
10855 + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
10856 +
10857 + dh_swap_bytes(sha_result, &vd, 4);
10858 +#ifdef DEBUG
10859 + DWC_PRINTF("Vd (decimal) = %d\n", vd);
10860 +#endif
10861 +
10862 + switch (nd) {
10863 + case 2:
10864 + vd = vd % 100;
10865 + DWC_SPRINTF(dd, "%02d", vd);
10866 + break;
10867 + case 3:
10868 + vd = vd % 1000;
10869 + DWC_SPRINTF(dd, "%03d", vd);
10870 + break;
10871 + case 4:
10872 + vd = vd % 10000;
10873 + DWC_SPRINTF(dd, "%04d", vd);
10874 + break;
10875 + }
10876 +#ifdef DEBUG
10877 + DWC_PRINTF("Display Digits: %s\n", dd);
10878 +#endif
10879 +
10880 + message = "connection key";
10881 + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
10882 + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
10883 + DWC_MEMCPY(ck, sha_result, 16);
10884 +
10885 + message = "key derivation key";
10886 + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
10887 + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
10888 + DWC_MEMCPY(kdk, sha_result, 32);
10889 +
10890 + return 0;
10891 +}
10892 +
10893 +
10894 +#ifdef DH_TEST_VECTORS
10895 +
10896 +static __u8 dh_a[] = {
10897 + 0x44, 0x00, 0x51, 0xd6,
10898 + 0xf0, 0xb5, 0x5e, 0xa9,
10899 + 0x67, 0xab, 0x31, 0xc6,
10900 + 0x8a, 0x8b, 0x5e, 0x37,
10901 + 0xd9, 0x10, 0xda, 0xe0,
10902 + 0xe2, 0xd4, 0x59, 0xa4,
10903 + 0x86, 0x45, 0x9c, 0xaa,
10904 + 0xdf, 0x36, 0x75, 0x16,
10905 +};
10906 +
10907 +static __u8 dh_b[] = {
10908 + 0x5d, 0xae, 0xc7, 0x86,
10909 + 0x79, 0x80, 0xa3, 0x24,
10910 + 0x8c, 0xe3, 0x57, 0x8f,
10911 + 0xc7, 0x5f, 0x1b, 0x0f,
10912 + 0x2d, 0xf8, 0x9d, 0x30,
10913 + 0x6f, 0xa4, 0x52, 0xcd,
10914 + 0xe0, 0x7a, 0x04, 0x8a,
10915 + 0xde, 0xd9, 0x26, 0x56,
10916 +};
10917 +
10918 +void dwc_run_dh_test_vectors(void *mem_ctx)
10919 +{
10920 + uint8_t pkd[384];
10921 + uint8_t pkh[384];
10922 + uint8_t hashd[32];
10923 + uint8_t hashh[32];
10924 + uint8_t ck[16];
10925 + uint8_t kdk[32];
10926 + char dd[5];
10927 +
10928 + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
10929 +
10930 + /* compute the PKd and SHA-256(PKd || Nd) */
10931 + DWC_PRINTF("Computing PKd\n");
10932 + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
10933 +
10934 + /* compute the PKd and SHA-256(PKh || Nd) */
10935 + DWC_PRINTF("Computing PKh\n");
10936 + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
10937 +
10938 + /* compute the dhkey */
10939 + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
10940 +}
10941 +#endif /* DH_TEST_VECTORS */
10942 +
10943 +#endif /* !CONFIG_MACH_IPMATE */
10944 +
10945 +#endif /* DWC_CRYPTOLIB */
10946 --- /dev/null
10947 +++ b/drivers/usb/host/dwc_common_port/dwc_dh.h
10948 @@ -0,0 +1,106 @@
10949 +/* =========================================================================
10950 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
10951 + * $Revision: #4 $
10952 + * $Date: 2010/09/28 $
10953 + * $Change: 1596182 $
10954 + *
10955 + * Synopsys Portability Library Software and documentation
10956 + * (hereinafter, "Software") is an Unsupported proprietary work of
10957 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10958 + * between Synopsys and you.
10959 + *
10960 + * The Software IS NOT an item of Licensed Software or Licensed Product
10961 + * under any End User Software License Agreement or Agreement for
10962 + * Licensed Product with Synopsys or any supplement thereto. You are
10963 + * permitted to use and redistribute this Software in source and binary
10964 + * forms, with or without modification, provided that redistributions
10965 + * of source code must retain this notice. You may not view, use,
10966 + * disclose, copy or distribute this file or any information contained
10967 + * herein except pursuant to this license grant from Synopsys. If you
10968 + * do not agree with this notice, including the disclaimer below, then
10969 + * you are not authorized to use the Software.
10970 + *
10971 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10972 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10973 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10974 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10975 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10976 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10977 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10978 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10979 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10980 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10981 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10982 + * DAMAGE.
10983 + * ========================================================================= */
10984 +#ifndef _DWC_DH_H_
10985 +#define _DWC_DH_H_
10986 +
10987 +#ifdef __cplusplus
10988 +extern "C" {
10989 +#endif
10990 +
10991 +#include "dwc_os.h"
10992 +
10993 +/** @file
10994 + *
10995 + * This file defines the common functions on device and host for performing
10996 + * numeric association as defined in the WUSB spec. They are only to be
10997 + * used internally by the DWC UWB modules. */
10998 +
10999 +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
11000 +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
11001 + uint8_t *key, uint32_t keylen,
11002 + uint8_t *out);
11003 +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
11004 + void *exp, uint32_t exp_len,
11005 + void *mod, uint32_t mod_len,
11006 + void *out);
11007 +
11008 +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
11009 + *
11010 + * PK = g^exp mod p.
11011 + *
11012 + * Input:
11013 + * Nd = Number of digits on the device.
11014 + *
11015 + * Output:
11016 + * exp = A 32-byte buffer to be filled with a randomly generated number.
11017 + * used as either A or B.
11018 + * pk = A 384-byte buffer to be filled with the PKH or PKD.
11019 + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
11020 + */
11021 +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
11022 +
11023 +/** Computes the DHKEY, and VD.
11024 + *
11025 + * If called from host, then it will comput DHKEY=PKD^exp % p.
11026 + * If called from device, then it will comput DHKEY=PKH^exp % p.
11027 + *
11028 + * Input:
11029 + * pkd = The PKD value.
11030 + * pkh = The PKH value.
11031 + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
11032 + * is_host = Set to non zero if a WUSB host is calling this function.
11033 + *
11034 + * Output:
11035 +
11036 + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
11037 + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
11038 + * null termination character. This buffer can be used directly for display.
11039 + * ck = A 16-byte buffer to be filled with the CK.
11040 + * kdk = A 32-byte buffer to be filled with the KDK.
11041 + */
11042 +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
11043 + uint8_t *exp, int is_host,
11044 + char *dd, uint8_t *ck, uint8_t *kdk);
11045 +
11046 +#ifdef DH_TEST_VECTORS
11047 +extern void dwc_run_dh_test_vectors(void);
11048 +#endif
11049 +
11050 +#ifdef __cplusplus
11051 +}
11052 +#endif
11053 +
11054 +#endif /* _DWC_DH_H_ */
11055 --- /dev/null
11056 +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
11057 @@ -0,0 +1,594 @@
11058 +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
11059 +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
11060 +
11061 +/*
11062 + * Copyright (c) 1991, 1993
11063 + * The Regents of the University of California. All rights reserved.
11064 + *
11065 + * Redistribution and use in source and binary forms, with or without
11066 + * modification, are permitted provided that the following conditions
11067 + * are met:
11068 + * 1. Redistributions of source code must retain the above copyright
11069 + * notice, this list of conditions and the following disclaimer.
11070 + * 2. Redistributions in binary form must reproduce the above copyright
11071 + * notice, this list of conditions and the following disclaimer in the
11072 + * documentation and/or other materials provided with the distribution.
11073 + * 3. Neither the name of the University nor the names of its contributors
11074 + * may be used to endorse or promote products derived from this software
11075 + * without specific prior written permission.
11076 + *
11077 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
11078 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11079 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11080 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
11081 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11082 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
11083 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
11084 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
11085 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
11086 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
11087 + * SUCH DAMAGE.
11088 + *
11089 + * @(#)queue.h 8.5 (Berkeley) 8/20/94
11090 + */
11091 +
11092 +#ifndef _DWC_LIST_H_
11093 +#define _DWC_LIST_H_
11094 +
11095 +#ifdef __cplusplus
11096 +extern "C" {
11097 +#endif
11098 +
11099 +/** @file
11100 + *
11101 + * This file defines linked list operations. It is derived from BSD with
11102 + * only the MACRO names being prefixed with DWC_. This is because a few of
11103 + * these names conflict with those on Linux. For documentation on use, see the
11104 + * inline comments in the source code. The original license for this source
11105 + * code applies and is preserved in the dwc_list.h source file.
11106 + */
11107 +
11108 +/*
11109 + * This file defines five types of data structures: singly-linked lists,
11110 + * lists, simple queues, tail queues, and circular queues.
11111 + *
11112 + *
11113 + * A singly-linked list is headed by a single forward pointer. The elements
11114 + * are singly linked for minimum space and pointer manipulation overhead at
11115 + * the expense of O(n) removal for arbitrary elements. New elements can be
11116 + * added to the list after an existing element or at the head of the list.
11117 + * Elements being removed from the head of the list should use the explicit
11118 + * macro for this purpose for optimum efficiency. A singly-linked list may
11119 + * only be traversed in the forward direction. Singly-linked lists are ideal
11120 + * for applications with large datasets and few or no removals or for
11121 + * implementing a LIFO queue.
11122 + *
11123 + * A list is headed by a single forward pointer (or an array of forward
11124 + * pointers for a hash table header). The elements are doubly linked
11125 + * so that an arbitrary element can be removed without a need to
11126 + * traverse the list. New elements can be added to the list before
11127 + * or after an existing element or at the head of the list. A list
11128 + * may only be traversed in the forward direction.
11129 + *
11130 + * A simple queue is headed by a pair of pointers, one the head of the
11131 + * list and the other to the tail of the list. The elements are singly
11132 + * linked to save space, so elements can only be removed from the
11133 + * head of the list. New elements can be added to the list before or after
11134 + * an existing element, at the head of the list, or at the end of the
11135 + * list. A simple queue may only be traversed in the forward direction.
11136 + *
11137 + * A tail queue is headed by a pair of pointers, one to the head of the
11138 + * list and the other to the tail of the list. The elements are doubly
11139 + * linked so that an arbitrary element can be removed without a need to
11140 + * traverse the list. New elements can be added to the list before or
11141 + * after an existing element, at the head of the list, or at the end of
11142 + * the list. A tail queue may be traversed in either direction.
11143 + *
11144 + * A circle queue is headed by a pair of pointers, one to the head of the
11145 + * list and the other to the tail of the list. The elements are doubly
11146 + * linked so that an arbitrary element can be removed without a need to
11147 + * traverse the list. New elements can be added to the list before or after
11148 + * an existing element, at the head of the list, or at the end of the list.
11149 + * A circle queue may be traversed in either direction, but has a more
11150 + * complex end of list detection.
11151 + *
11152 + * For details on the use of these macros, see the queue(3) manual page.
11153 + */
11154 +
11155 +/*
11156 + * Double-linked List.
11157 + */
11158 +
11159 +typedef struct dwc_list_link {
11160 + struct dwc_list_link *next;
11161 + struct dwc_list_link *prev;
11162 +} dwc_list_link_t;
11163 +
11164 +#define DWC_LIST_INIT(link) do { \
11165 + (link)->next = (link); \
11166 + (link)->prev = (link); \
11167 +} while (0)
11168 +
11169 +#define DWC_LIST_FIRST(link) ((link)->next)
11170 +#define DWC_LIST_LAST(link) ((link)->prev)
11171 +#define DWC_LIST_END(link) (link)
11172 +#define DWC_LIST_NEXT(link) ((link)->next)
11173 +#define DWC_LIST_PREV(link) ((link)->prev)
11174 +#define DWC_LIST_EMPTY(link) \
11175 + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
11176 +#define DWC_LIST_ENTRY(link, type, field) \
11177 + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
11178 +
11179 +#if 0
11180 +#define DWC_LIST_INSERT_HEAD(list, link) do { \
11181 + (link)->next = (list)->next; \
11182 + (link)->prev = (list); \
11183 + (list)->next->prev = (link); \
11184 + (list)->next = (link); \
11185 +} while (0)
11186 +
11187 +#define DWC_LIST_INSERT_TAIL(list, link) do { \
11188 + (link)->next = (list); \
11189 + (link)->prev = (list)->prev; \
11190 + (list)->prev->next = (link); \
11191 + (list)->prev = (link); \
11192 +} while (0)
11193 +#else
11194 +#define DWC_LIST_INSERT_HEAD(list, link) do { \
11195 + dwc_list_link_t *__next__ = (list)->next; \
11196 + __next__->prev = (link); \
11197 + (link)->next = __next__; \
11198 + (link)->prev = (list); \
11199 + (list)->next = (link); \
11200 +} while (0)
11201 +
11202 +#define DWC_LIST_INSERT_TAIL(list, link) do { \
11203 + dwc_list_link_t *__prev__ = (list)->prev; \
11204 + (list)->prev = (link); \
11205 + (link)->next = (list); \
11206 + (link)->prev = __prev__; \
11207 + __prev__->next = (link); \
11208 +} while (0)
11209 +#endif
11210 +
11211 +#if 0
11212 +static inline void __list_add(struct list_head *new,
11213 + struct list_head *prev,
11214 + struct list_head *next)
11215 +{
11216 + next->prev = new;
11217 + new->next = next;
11218 + new->prev = prev;
11219 + prev->next = new;
11220 +}
11221 +
11222 +static inline void list_add(struct list_head *new, struct list_head *head)
11223 +{
11224 + __list_add(new, head, head->next);
11225 +}
11226 +
11227 +static inline void list_add_tail(struct list_head *new, struct list_head *head)
11228 +{
11229 + __list_add(new, head->prev, head);
11230 +}
11231 +
11232 +static inline void __list_del(struct list_head * prev, struct list_head * next)
11233 +{
11234 + next->prev = prev;
11235 + prev->next = next;
11236 +}
11237 +
11238 +static inline void list_del(struct list_head *entry)
11239 +{
11240 + __list_del(entry->prev, entry->next);
11241 + entry->next = LIST_POISON1;
11242 + entry->prev = LIST_POISON2;
11243 +}
11244 +#endif
11245 +
11246 +#define DWC_LIST_REMOVE(link) do { \
11247 + (link)->next->prev = (link)->prev; \
11248 + (link)->prev->next = (link)->next; \
11249 +} while (0)
11250 +
11251 +#define DWC_LIST_REMOVE_INIT(link) do { \
11252 + DWC_LIST_REMOVE(link); \
11253 + DWC_LIST_INIT(link); \
11254 +} while (0)
11255 +
11256 +#define DWC_LIST_MOVE_HEAD(list, link) do { \
11257 + DWC_LIST_REMOVE(link); \
11258 + DWC_LIST_INSERT_HEAD(list, link); \
11259 +} while (0)
11260 +
11261 +#define DWC_LIST_MOVE_TAIL(list, link) do { \
11262 + DWC_LIST_REMOVE(link); \
11263 + DWC_LIST_INSERT_TAIL(list, link); \
11264 +} while (0)
11265 +
11266 +#define DWC_LIST_FOREACH(var, list) \
11267 + for((var) = DWC_LIST_FIRST(list); \
11268 + (var) != DWC_LIST_END(list); \
11269 + (var) = DWC_LIST_NEXT(var))
11270 +
11271 +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
11272 + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
11273 + (var) != DWC_LIST_END(list); \
11274 + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
11275 +
11276 +#define DWC_LIST_FOREACH_REVERSE(var, list) \
11277 + for((var) = DWC_LIST_LAST(list); \
11278 + (var) != DWC_LIST_END(list); \
11279 + (var) = DWC_LIST_PREV(var))
11280 +
11281 +/*
11282 + * Singly-linked List definitions.
11283 + */
11284 +#define DWC_SLIST_HEAD(name, type) \
11285 +struct name { \
11286 + struct type *slh_first; /* first element */ \
11287 +}
11288 +
11289 +#define DWC_SLIST_HEAD_INITIALIZER(head) \
11290 + { NULL }
11291 +
11292 +#define DWC_SLIST_ENTRY(type) \
11293 +struct { \
11294 + struct type *sle_next; /* next element */ \
11295 +}
11296 +
11297 +/*
11298 + * Singly-linked List access methods.
11299 + */
11300 +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
11301 +#define DWC_SLIST_END(head) NULL
11302 +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
11303 +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
11304 +
11305 +#define DWC_SLIST_FOREACH(var, head, field) \
11306 + for((var) = SLIST_FIRST(head); \
11307 + (var) != SLIST_END(head); \
11308 + (var) = SLIST_NEXT(var, field))
11309 +
11310 +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
11311 + for((varp) = &SLIST_FIRST((head)); \
11312 + ((var) = *(varp)) != SLIST_END(head); \
11313 + (varp) = &SLIST_NEXT((var), field))
11314 +
11315 +/*
11316 + * Singly-linked List functions.
11317 + */
11318 +#define DWC_SLIST_INIT(head) { \
11319 + SLIST_FIRST(head) = SLIST_END(head); \
11320 +}
11321 +
11322 +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
11323 + (elm)->field.sle_next = (slistelm)->field.sle_next; \
11324 + (slistelm)->field.sle_next = (elm); \
11325 +} while (0)
11326 +
11327 +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
11328 + (elm)->field.sle_next = (head)->slh_first; \
11329 + (head)->slh_first = (elm); \
11330 +} while (0)
11331 +
11332 +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
11333 + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
11334 +} while (0)
11335 +
11336 +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
11337 + (head)->slh_first = (head)->slh_first->field.sle_next; \
11338 +} while (0)
11339 +
11340 +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
11341 + if ((head)->slh_first == (elm)) { \
11342 + SLIST_REMOVE_HEAD((head), field); \
11343 + } \
11344 + else { \
11345 + struct type *curelm = (head)->slh_first; \
11346 + while( curelm->field.sle_next != (elm) ) \
11347 + curelm = curelm->field.sle_next; \
11348 + curelm->field.sle_next = \
11349 + curelm->field.sle_next->field.sle_next; \
11350 + } \
11351 +} while (0)
11352 +
11353 +/*
11354 + * Simple queue definitions.
11355 + */
11356 +#define DWC_SIMPLEQ_HEAD(name, type) \
11357 +struct name { \
11358 + struct type *sqh_first; /* first element */ \
11359 + struct type **sqh_last; /* addr of last next element */ \
11360 +}
11361 +
11362 +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
11363 + { NULL, &(head).sqh_first }
11364 +
11365 +#define DWC_SIMPLEQ_ENTRY(type) \
11366 +struct { \
11367 + struct type *sqe_next; /* next element */ \
11368 +}
11369 +
11370 +/*
11371 + * Simple queue access methods.
11372 + */
11373 +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
11374 +#define DWC_SIMPLEQ_END(head) NULL
11375 +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
11376 +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
11377 +
11378 +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
11379 + for((var) = SIMPLEQ_FIRST(head); \
11380 + (var) != SIMPLEQ_END(head); \
11381 + (var) = SIMPLEQ_NEXT(var, field))
11382 +
11383 +/*
11384 + * Simple queue functions.
11385 + */
11386 +#define DWC_SIMPLEQ_INIT(head) do { \
11387 + (head)->sqh_first = NULL; \
11388 + (head)->sqh_last = &(head)->sqh_first; \
11389 +} while (0)
11390 +
11391 +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
11392 + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
11393 + (head)->sqh_last = &(elm)->field.sqe_next; \
11394 + (head)->sqh_first = (elm); \
11395 +} while (0)
11396 +
11397 +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
11398 + (elm)->field.sqe_next = NULL; \
11399 + *(head)->sqh_last = (elm); \
11400 + (head)->sqh_last = &(elm)->field.sqe_next; \
11401 +} while (0)
11402 +
11403 +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
11404 + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
11405 + (head)->sqh_last = &(elm)->field.sqe_next; \
11406 + (listelm)->field.sqe_next = (elm); \
11407 +} while (0)
11408 +
11409 +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
11410 + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
11411 + (head)->sqh_last = &(head)->sqh_first; \
11412 +} while (0)
11413 +
11414 +/*
11415 + * Tail queue definitions.
11416 + */
11417 +#define DWC_TAILQ_HEAD(name, type) \
11418 +struct name { \
11419 + struct type *tqh_first; /* first element */ \
11420 + struct type **tqh_last; /* addr of last next element */ \
11421 +}
11422 +
11423 +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
11424 + { NULL, &(head).tqh_first }
11425 +
11426 +#define DWC_TAILQ_ENTRY(type) \
11427 +struct { \
11428 + struct type *tqe_next; /* next element */ \
11429 + struct type **tqe_prev; /* address of previous next element */ \
11430 +}
11431 +
11432 +/*
11433 + * tail queue access methods
11434 + */
11435 +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
11436 +#define DWC_TAILQ_END(head) NULL
11437 +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
11438 +#define DWC_TAILQ_LAST(head, headname) \
11439 + (*(((struct headname *)((head)->tqh_last))->tqh_last))
11440 +/* XXX */
11441 +#define DWC_TAILQ_PREV(elm, headname, field) \
11442 + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
11443 +#define DWC_TAILQ_EMPTY(head) \
11444 + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
11445 +
11446 +#define DWC_TAILQ_FOREACH(var, head, field) \
11447 + for ((var) = DWC_TAILQ_FIRST(head); \
11448 + (var) != DWC_TAILQ_END(head); \
11449 + (var) = DWC_TAILQ_NEXT(var, field))
11450 +
11451 +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
11452 + for ((var) = DWC_TAILQ_LAST(head, headname); \
11453 + (var) != DWC_TAILQ_END(head); \
11454 + (var) = DWC_TAILQ_PREV(var, headname, field))
11455 +
11456 +/*
11457 + * Tail queue functions.
11458 + */
11459 +#define DWC_TAILQ_INIT(head) do { \
11460 + (head)->tqh_first = NULL; \
11461 + (head)->tqh_last = &(head)->tqh_first; \
11462 +} while (0)
11463 +
11464 +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
11465 + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
11466 + (head)->tqh_first->field.tqe_prev = \
11467 + &(elm)->field.tqe_next; \
11468 + else \
11469 + (head)->tqh_last = &(elm)->field.tqe_next; \
11470 + (head)->tqh_first = (elm); \
11471 + (elm)->field.tqe_prev = &(head)->tqh_first; \
11472 +} while (0)
11473 +
11474 +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
11475 + (elm)->field.tqe_next = NULL; \
11476 + (elm)->field.tqe_prev = (head)->tqh_last; \
11477 + *(head)->tqh_last = (elm); \
11478 + (head)->tqh_last = &(elm)->field.tqe_next; \
11479 +} while (0)
11480 +
11481 +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
11482 + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
11483 + (elm)->field.tqe_next->field.tqe_prev = \
11484 + &(elm)->field.tqe_next; \
11485 + else \
11486 + (head)->tqh_last = &(elm)->field.tqe_next; \
11487 + (listelm)->field.tqe_next = (elm); \
11488 + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
11489 +} while (0)
11490 +
11491 +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
11492 + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
11493 + (elm)->field.tqe_next = (listelm); \
11494 + *(listelm)->field.tqe_prev = (elm); \
11495 + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
11496 +} while (0)
11497 +
11498 +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
11499 + if (((elm)->field.tqe_next) != NULL) \
11500 + (elm)->field.tqe_next->field.tqe_prev = \
11501 + (elm)->field.tqe_prev; \
11502 + else \
11503 + (head)->tqh_last = (elm)->field.tqe_prev; \
11504 + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
11505 +} while (0)
11506 +
11507 +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
11508 + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
11509 + (elm2)->field.tqe_next->field.tqe_prev = \
11510 + &(elm2)->field.tqe_next; \
11511 + else \
11512 + (head)->tqh_last = &(elm2)->field.tqe_next; \
11513 + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
11514 + *(elm2)->field.tqe_prev = (elm2); \
11515 +} while (0)
11516 +
11517 +/*
11518 + * Circular queue definitions.
11519 + */
11520 +#define DWC_CIRCLEQ_HEAD(name, type) \
11521 +struct name { \
11522 + struct type *cqh_first; /* first element */ \
11523 + struct type *cqh_last; /* last element */ \
11524 +}
11525 +
11526 +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
11527 + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
11528 +
11529 +#define DWC_CIRCLEQ_ENTRY(type) \
11530 +struct { \
11531 + struct type *cqe_next; /* next element */ \
11532 + struct type *cqe_prev; /* previous element */ \
11533 +}
11534 +
11535 +/*
11536 + * Circular queue access methods
11537 + */
11538 +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
11539 +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
11540 +#define DWC_CIRCLEQ_END(head) ((void *)(head))
11541 +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
11542 +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
11543 +#define DWC_CIRCLEQ_EMPTY(head) \
11544 + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
11545 +
11546 +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
11547 +
11548 +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
11549 + for((var) = DWC_CIRCLEQ_FIRST(head); \
11550 + (var) != DWC_CIRCLEQ_END(head); \
11551 + (var) = DWC_CIRCLEQ_NEXT(var, field))
11552 +
11553 +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
11554 + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
11555 + (var) != DWC_CIRCLEQ_END(head); \
11556 + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
11557 +
11558 +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
11559 + for((var) = DWC_CIRCLEQ_LAST(head); \
11560 + (var) != DWC_CIRCLEQ_END(head); \
11561 + (var) = DWC_CIRCLEQ_PREV(var, field))
11562 +
11563 +/*
11564 + * Circular queue functions.
11565 + */
11566 +#define DWC_CIRCLEQ_INIT(head) do { \
11567 + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
11568 + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
11569 +} while (0)
11570 +
11571 +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
11572 + (elm)->field.cqe_next = NULL; \
11573 + (elm)->field.cqe_prev = NULL; \
11574 +} while (0)
11575 +
11576 +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
11577 + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
11578 + (elm)->field.cqe_prev = (listelm); \
11579 + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
11580 + (head)->cqh_last = (elm); \
11581 + else \
11582 + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
11583 + (listelm)->field.cqe_next = (elm); \
11584 +} while (0)
11585 +
11586 +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
11587 + (elm)->field.cqe_next = (listelm); \
11588 + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
11589 + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
11590 + (head)->cqh_first = (elm); \
11591 + else \
11592 + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
11593 + (listelm)->field.cqe_prev = (elm); \
11594 +} while (0)
11595 +
11596 +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
11597 + (elm)->field.cqe_next = (head)->cqh_first; \
11598 + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
11599 + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
11600 + (head)->cqh_last = (elm); \
11601 + else \
11602 + (head)->cqh_first->field.cqe_prev = (elm); \
11603 + (head)->cqh_first = (elm); \
11604 +} while (0)
11605 +
11606 +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
11607 + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
11608 + (elm)->field.cqe_prev = (head)->cqh_last; \
11609 + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
11610 + (head)->cqh_first = (elm); \
11611 + else \
11612 + (head)->cqh_last->field.cqe_next = (elm); \
11613 + (head)->cqh_last = (elm); \
11614 +} while (0)
11615 +
11616 +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
11617 + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
11618 + (head)->cqh_last = (elm)->field.cqe_prev; \
11619 + else \
11620 + (elm)->field.cqe_next->field.cqe_prev = \
11621 + (elm)->field.cqe_prev; \
11622 + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
11623 + (head)->cqh_first = (elm)->field.cqe_next; \
11624 + else \
11625 + (elm)->field.cqe_prev->field.cqe_next = \
11626 + (elm)->field.cqe_next; \
11627 +} while (0)
11628 +
11629 +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
11630 + DWC_CIRCLEQ_REMOVE(head, elm, field); \
11631 + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
11632 +} while (0)
11633 +
11634 +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
11635 + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
11636 + DWC_CIRCLEQ_END(head)) \
11637 + (head).cqh_last = (elm2); \
11638 + else \
11639 + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
11640 + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
11641 + DWC_CIRCLEQ_END(head)) \
11642 + (head).cqh_first = (elm2); \
11643 + else \
11644 + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
11645 +} while (0)
11646 +
11647 +#ifdef __cplusplus
11648 +}
11649 +#endif
11650 +
11651 +#endif /* _DWC_LIST_H_ */
11652 --- /dev/null
11653 +++ b/drivers/usb/host/dwc_common_port/dwc_mem.c
11654 @@ -0,0 +1,245 @@
11655 +/* Memory Debugging */
11656 +#ifdef DWC_DEBUG_MEMORY
11657 +
11658 +#include "dwc_os.h"
11659 +#include "dwc_list.h"
11660 +
11661 +struct allocation {
11662 + void *addr;
11663 + void *ctx;
11664 + char *func;
11665 + int line;
11666 + uint32_t size;
11667 + int dma;
11668 + DWC_CIRCLEQ_ENTRY(allocation) entry;
11669 +};
11670 +
11671 +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
11672 +
11673 +struct allocation_manager {
11674 + void *mem_ctx;
11675 + struct allocation_queue allocations;
11676 +
11677 + /* statistics */
11678 + int num;
11679 + int num_freed;
11680 + int num_active;
11681 + uint32_t total;
11682 + uint32_t cur;
11683 + uint32_t max;
11684 +};
11685 +
11686 +static struct allocation_manager *manager = NULL;
11687 +
11688 +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
11689 + int dma)
11690 +{
11691 + struct allocation *a;
11692 +
11693 + DWC_ASSERT(manager != NULL, "manager not allocated");
11694 +
11695 + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
11696 + if (!a) {
11697 + return -DWC_E_NO_MEMORY;
11698 + }
11699 +
11700 + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
11701 + if (!a->func) {
11702 + __DWC_FREE(manager->mem_ctx, a);
11703 + return -DWC_E_NO_MEMORY;
11704 + }
11705 +
11706 + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
11707 + a->addr = addr;
11708 + a->ctx = ctx;
11709 + a->line = line;
11710 + a->size = size;
11711 + a->dma = dma;
11712 + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
11713 +
11714 + /* Update stats */
11715 + manager->num++;
11716 + manager->num_active++;
11717 + manager->total += size;
11718 + manager->cur += size;
11719 +
11720 + if (manager->max < manager->cur) {
11721 + manager->max = manager->cur;
11722 + }
11723 +
11724 + return 0;
11725 +}
11726 +
11727 +static struct allocation *find_allocation(void *ctx, void *addr)
11728 +{
11729 + struct allocation *a;
11730 +
11731 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11732 + if (a->ctx == ctx && a->addr == addr) {
11733 + return a;
11734 + }
11735 + }
11736 +
11737 + return NULL;
11738 +}
11739 +
11740 +static void free_allocation(void *ctx, void *addr, char const *func, int line)
11741 +{
11742 + struct allocation *a = find_allocation(ctx, addr);
11743 +
11744 + if (!a) {
11745 + DWC_ASSERT(0,
11746 + "Free of address %p that was never allocated or already freed %s:%d",
11747 + addr, func, line);
11748 + return;
11749 + }
11750 +
11751 + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
11752 +
11753 + manager->num_active--;
11754 + manager->num_freed++;
11755 + manager->cur -= a->size;
11756 + __DWC_FREE(manager->mem_ctx, a->func);
11757 + __DWC_FREE(manager->mem_ctx, a);
11758 +}
11759 +
11760 +int dwc_memory_debug_start(void *mem_ctx)
11761 +{
11762 + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
11763 +
11764 + if (manager) {
11765 + return -DWC_E_BUSY;
11766 + }
11767 +
11768 + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
11769 + if (!manager) {
11770 + return -DWC_E_NO_MEMORY;
11771 + }
11772 +
11773 + DWC_CIRCLEQ_INIT(&manager->allocations);
11774 + manager->mem_ctx = mem_ctx;
11775 + manager->num = 0;
11776 + manager->num_freed = 0;
11777 + manager->num_active = 0;
11778 + manager->total = 0;
11779 + manager->cur = 0;
11780 + manager->max = 0;
11781 +
11782 + return 0;
11783 +}
11784 +
11785 +void dwc_memory_debug_stop(void)
11786 +{
11787 + struct allocation *a;
11788 +
11789 + dwc_memory_debug_report();
11790 +
11791 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11792 + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
11793 + free_allocation(a->ctx, a->addr, NULL, -1);
11794 + }
11795 +
11796 + __DWC_FREE(manager->mem_ctx, manager);
11797 +}
11798 +
11799 +void dwc_memory_debug_report(void)
11800 +{
11801 + struct allocation *a;
11802 +
11803 + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
11804 + DWC_PRINTF("Num Allocations = %d\n", manager->num);
11805 + DWC_PRINTF("Freed = %d\n", manager->num_freed);
11806 + DWC_PRINTF("Active = %d\n", manager->num_active);
11807 + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
11808 + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
11809 + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
11810 + DWC_PRINTF("Unfreed allocations:\n");
11811 +
11812 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11813 + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
11814 + a->addr, a->size, a->func, a->line, a->dma);
11815 + }
11816 +}
11817 +
11818 +/* The replacement functions */
11819 +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
11820 +{
11821 + void *addr = __DWC_ALLOC(mem_ctx, size);
11822 +
11823 + if (!addr) {
11824 + return NULL;
11825 + }
11826 +
11827 + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
11828 + __DWC_FREE(mem_ctx, addr);
11829 + return NULL;
11830 + }
11831 +
11832 + return addr;
11833 +}
11834 +
11835 +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
11836 + int line)
11837 +{
11838 + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
11839 +
11840 + if (!addr) {
11841 + return NULL;
11842 + }
11843 +
11844 + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
11845 + __DWC_FREE(mem_ctx, addr);
11846 + return NULL;
11847 + }
11848 +
11849 + return addr;
11850 +}
11851 +
11852 +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
11853 +{
11854 + free_allocation(mem_ctx, addr, func, line);
11855 + __DWC_FREE(mem_ctx, addr);
11856 +}
11857 +
11858 +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
11859 + char const *func, int line)
11860 +{
11861 + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
11862 +
11863 + if (!addr) {
11864 + return NULL;
11865 + }
11866 +
11867 + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
11868 + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
11869 + return NULL;
11870 + }
11871 +
11872 + return addr;
11873 +}
11874 +
11875 +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
11876 + dwc_dma_t *dma_addr, char const *func, int line)
11877 +{
11878 + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
11879 +
11880 + if (!addr) {
11881 + return NULL;
11882 + }
11883 +
11884 + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
11885 + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
11886 + return NULL;
11887 + }
11888 +
11889 + return addr;
11890 +}
11891 +
11892 +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
11893 + dwc_dma_t dma_addr, char const *func, int line)
11894 +{
11895 + free_allocation(dma_ctx, virt_addr, func, line);
11896 + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
11897 +}
11898 +
11899 +#endif /* DWC_DEBUG_MEMORY */
11900 --- /dev/null
11901 +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.c
11902 @@ -0,0 +1,636 @@
11903 +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
11904 + *
11905 + * PuTTY is copyright 1997-2007 Simon Tatham.
11906 + *
11907 + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
11908 + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
11909 + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
11910 + * Kuhn, and CORE SDI S.A.
11911 + *
11912 + * Permission is hereby granted, free of charge, to any person
11913 + * obtaining a copy of this software and associated documentation files
11914 + * (the "Software"), to deal in the Software without restriction,
11915 + * including without limitation the rights to use, copy, modify, merge,
11916 + * publish, distribute, sublicense, and/or sell copies of the Software,
11917 + * and to permit persons to whom the Software is furnished to do so,
11918 + * subject to the following conditions:
11919 + *
11920 + * The above copyright notice and this permission notice shall be
11921 + * included in all copies or substantial portions of the Software.
11922 +
11923 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11924 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11925 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11926 + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
11927 + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
11928 + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
11929 + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11930 + *
11931 + */
11932 +#ifdef DWC_CRYPTOLIB
11933 +
11934 +#ifndef CONFIG_MACH_IPMATE
11935 +
11936 +#include "dwc_modpow.h"
11937 +
11938 +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
11939 +#define BIGNUM_TOP_BIT 0x80000000UL
11940 +#define BIGNUM_INT_BITS 32
11941 +
11942 +
11943 +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
11944 +{
11945 + void *p;
11946 + size *= n;
11947 + if (size == 0) size = 1;
11948 + p = dwc_alloc(mem_ctx, size);
11949 + return p;
11950 +}
11951 +
11952 +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
11953 +#define sfree dwc_free
11954 +
11955 +/*
11956 + * Usage notes:
11957 + * * Do not call the DIVMOD_WORD macro with expressions such as array
11958 + * subscripts, as some implementations object to this (see below).
11959 + * * Note that none of the division methods below will cope if the
11960 + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
11961 + * to avoid this case.
11962 + * If this condition occurs, in the case of the x86 DIV instruction,
11963 + * an overflow exception will occur, which (according to a correspondent)
11964 + * will manifest on Windows as something like
11965 + * 0xC0000095: Integer overflow
11966 + * The C variant won't give the right answer, either.
11967 + */
11968 +
11969 +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
11970 +
11971 +#if defined __GNUC__ && defined __i386__
11972 +#define DIVMOD_WORD(q, r, hi, lo, w) \
11973 + __asm__("div %2" : \
11974 + "=d" (r), "=a" (q) : \
11975 + "r" (w), "d" (hi), "a" (lo))
11976 +#else
11977 +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
11978 + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
11979 + q = n / w; \
11980 + r = n % w; \
11981 +} while (0)
11982 +#endif
11983 +
11984 +// q = n / w;
11985 +// r = n % w;
11986 +
11987 +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
11988 +
11989 +#define BIGNUM_INTERNAL
11990 +
11991 +static Bignum newbn(void *mem_ctx, int length)
11992 +{
11993 + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
11994 + //if (!b)
11995 + //abort(); /* FIXME */
11996 + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
11997 + b[0] = length;
11998 + return b;
11999 +}
12000 +
12001 +void freebn(void *mem_ctx, Bignum b)
12002 +{
12003 + /*
12004 + * Burn the evidence, just in case.
12005 + */
12006 + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
12007 + sfree(mem_ctx, b);
12008 +}
12009 +
12010 +/*
12011 + * Compute c = a * b.
12012 + * Input is in the first len words of a and b.
12013 + * Result is returned in the first 2*len words of c.
12014 + */
12015 +static void internal_mul(BignumInt *a, BignumInt *b,
12016 + BignumInt *c, int len)
12017 +{
12018 + int i, j;
12019 + BignumDblInt t;
12020 +
12021 + for (j = 0; j < 2 * len; j++)
12022 + c[j] = 0;
12023 +
12024 + for (i = len - 1; i >= 0; i--) {
12025 + t = 0;
12026 + for (j = len - 1; j >= 0; j--) {
12027 + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
12028 + t += (BignumDblInt) c[i + j + 1];
12029 + c[i + j + 1] = (BignumInt) t;
12030 + t = t >> BIGNUM_INT_BITS;
12031 + }
12032 + c[i] = (BignumInt) t;
12033 + }
12034 +}
12035 +
12036 +static void internal_add_shifted(BignumInt *number,
12037 + unsigned n, int shift)
12038 +{
12039 + int word = 1 + (shift / BIGNUM_INT_BITS);
12040 + int bshift = shift % BIGNUM_INT_BITS;
12041 + BignumDblInt addend;
12042 +
12043 + addend = (BignumDblInt)n << bshift;
12044 +
12045 + while (addend) {
12046 + addend += number[word];
12047 + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
12048 + addend >>= BIGNUM_INT_BITS;
12049 + word++;
12050 + }
12051 +}
12052 +
12053 +/*
12054 + * Compute a = a % m.
12055 + * Input in first alen words of a and first mlen words of m.
12056 + * Output in first alen words of a
12057 + * (of which first alen-mlen words will be zero).
12058 + * The MSW of m MUST have its high bit set.
12059 + * Quotient is accumulated in the `quotient' array, which is a Bignum
12060 + * rather than the internal bigendian format. Quotient parts are shifted
12061 + * left by `qshift' before adding into quot.
12062 + */
12063 +static void internal_mod(BignumInt *a, int alen,
12064 + BignumInt *m, int mlen,
12065 + BignumInt *quot, int qshift)
12066 +{
12067 + BignumInt m0, m1;
12068 + unsigned int h;
12069 + int i, k;
12070 +
12071 + m0 = m[0];
12072 + if (mlen > 1)
12073 + m1 = m[1];
12074 + else
12075 + m1 = 0;
12076 +
12077 + for (i = 0; i <= alen - mlen; i++) {
12078 + BignumDblInt t;
12079 + unsigned int q, r, c, ai1;
12080 +
12081 + if (i == 0) {
12082 + h = 0;
12083 + } else {
12084 + h = a[i - 1];
12085 + a[i - 1] = 0;
12086 + }
12087 +
12088 + if (i == alen - 1)
12089 + ai1 = 0;
12090 + else
12091 + ai1 = a[i + 1];
12092 +
12093 + /* Find q = h:a[i] / m0 */
12094 + if (h >= m0) {
12095 + /*
12096 + * Special case.
12097 + *
12098 + * To illustrate it, suppose a BignumInt is 8 bits, and
12099 + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
12100 + * our initial division will be 0xA123 / 0xA1, which
12101 + * will give a quotient of 0x100 and a divide overflow.
12102 + * However, the invariants in this division algorithm
12103 + * are not violated, since the full number A1:23:... is
12104 + * _less_ than the quotient prefix A1:B2:... and so the
12105 + * following correction loop would have sorted it out.
12106 + *
12107 + * In this situation we set q to be the largest
12108 + * quotient we _can_ stomach (0xFF, of course).
12109 + */
12110 + q = BIGNUM_INT_MASK;
12111 + } else {
12112 + /* Macro doesn't want an array subscript expression passed
12113 + * into it (see definition), so use a temporary. */
12114 + BignumInt tmplo = a[i];
12115 + DIVMOD_WORD(q, r, h, tmplo, m0);
12116 +
12117 + /* Refine our estimate of q by looking at
12118 + h:a[i]:a[i+1] / m0:m1 */
12119 + t = MUL_WORD(m1, q);
12120 + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
12121 + q--;
12122 + t -= m1;
12123 + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
12124 + if (r >= (BignumDblInt) m0 &&
12125 + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
12126 + }
12127 + }
12128 +
12129 + /* Subtract q * m from a[i...] */
12130 + c = 0;
12131 + for (k = mlen - 1; k >= 0; k--) {
12132 + t = MUL_WORD(q, m[k]);
12133 + t += c;
12134 + c = (unsigned)(t >> BIGNUM_INT_BITS);
12135 + if ((BignumInt) t > a[i + k])
12136 + c++;
12137 + a[i + k] -= (BignumInt) t;
12138 + }
12139 +
12140 + /* Add back m in case of borrow */
12141 + if (c != h) {
12142 + t = 0;
12143 + for (k = mlen - 1; k >= 0; k--) {
12144 + t += m[k];
12145 + t += a[i + k];
12146 + a[i + k] = (BignumInt) t;
12147 + t = t >> BIGNUM_INT_BITS;
12148 + }
12149 + q--;
12150 + }
12151 + if (quot)
12152 + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
12153 + }
12154 +}
12155 +
12156 +/*
12157 + * Compute p % mod.
12158 + * The most significant word of mod MUST be non-zero.
12159 + * We assume that the result array is the same size as the mod array.
12160 + * We optionally write out a quotient if `quotient' is non-NULL.
12161 + * We can avoid writing out the result if `result' is NULL.
12162 + */
12163 +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
12164 +{
12165 + BignumInt *n, *m;
12166 + int mshift;
12167 + int plen, mlen, i, j;
12168 +
12169 + /* Allocate m of size mlen, copy mod to m */
12170 + /* We use big endian internally */
12171 + mlen = mod[0];
12172 + m = snewn(mem_ctx, mlen, BignumInt);
12173 + //if (!m)
12174 + //abort(); /* FIXME */
12175 + for (j = 0; j < mlen; j++)
12176 + m[j] = mod[mod[0] - j];
12177 +
12178 + /* Shift m left to make msb bit set */
12179 + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
12180 + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
12181 + break;
12182 + if (mshift) {
12183 + for (i = 0; i < mlen - 1; i++)
12184 + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
12185 + m[mlen - 1] = m[mlen - 1] << mshift;
12186 + }
12187 +
12188 + plen = p[0];
12189 + /* Ensure plen > mlen */
12190 + if (plen <= mlen)
12191 + plen = mlen + 1;
12192 +
12193 + /* Allocate n of size plen, copy p to n */
12194 + n = snewn(mem_ctx, plen, BignumInt);
12195 + //if (!n)
12196 + //abort(); /* FIXME */
12197 + for (j = 0; j < plen; j++)
12198 + n[j] = 0;
12199 + for (j = 1; j <= (int)p[0]; j++)
12200 + n[plen - j] = p[j];
12201 +
12202 + /* Main computation */
12203 + internal_mod(n, plen, m, mlen, quotient, mshift);
12204 +
12205 + /* Fixup result in case the modulus was shifted */
12206 + if (mshift) {
12207 + for (i = plen - mlen - 1; i < plen - 1; i++)
12208 + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
12209 + n[plen - 1] = n[plen - 1] << mshift;
12210 + internal_mod(n, plen, m, mlen, quotient, 0);
12211 + for (i = plen - 1; i >= plen - mlen; i--)
12212 + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
12213 + }
12214 +
12215 + /* Copy result to buffer */
12216 + if (result) {
12217 + for (i = 1; i <= (int)result[0]; i++) {
12218 + int j = plen - i;
12219 + result[i] = j >= 0 ? n[j] : 0;
12220 + }
12221 + }
12222 +
12223 + /* Free temporary arrays */
12224 + for (i = 0; i < mlen; i++)
12225 + m[i] = 0;
12226 + sfree(mem_ctx, m);
12227 + for (i = 0; i < plen; i++)
12228 + n[i] = 0;
12229 + sfree(mem_ctx, n);
12230 +}
12231 +
12232 +/*
12233 + * Simple remainder.
12234 + */
12235 +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
12236 +{
12237 + Bignum r = newbn(mem_ctx, b[0]);
12238 + bigdivmod(mem_ctx, a, b, r, NULL);
12239 + return r;
12240 +}
12241 +
12242 +/*
12243 + * Compute (base ^ exp) % mod.
12244 + */
12245 +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
12246 +{
12247 + BignumInt *a, *b, *n, *m;
12248 + int mshift;
12249 + int mlen, i, j;
12250 + Bignum base, result;
12251 +
12252 + /*
12253 + * The most significant word of mod needs to be non-zero. It
12254 + * should already be, but let's make sure.
12255 + */
12256 + //assert(mod[mod[0]] != 0);
12257 +
12258 + /*
12259 + * Make sure the base is smaller than the modulus, by reducing
12260 + * it modulo the modulus if not.
12261 + */
12262 + base = bigmod(mem_ctx, base_in, mod);
12263 +
12264 + /* Allocate m of size mlen, copy mod to m */
12265 + /* We use big endian internally */
12266 + mlen = mod[0];
12267 + m = snewn(mem_ctx, mlen, BignumInt);
12268 + //if (!m)
12269 + //abort(); /* FIXME */
12270 + for (j = 0; j < mlen; j++)
12271 + m[j] = mod[mod[0] - j];
12272 +
12273 + /* Shift m left to make msb bit set */
12274 + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
12275 + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
12276 + break;
12277 + if (mshift) {
12278 + for (i = 0; i < mlen - 1; i++)
12279 + m[i] =
12280 + (m[i] << mshift) | (m[i + 1] >>
12281 + (BIGNUM_INT_BITS - mshift));
12282 + m[mlen - 1] = m[mlen - 1] << mshift;
12283 + }
12284 +
12285 + /* Allocate n of size mlen, copy base to n */
12286 + n = snewn(mem_ctx, mlen, BignumInt);
12287 + //if (!n)
12288 + //abort(); /* FIXME */
12289 + i = mlen - base[0];
12290 + for (j = 0; j < i; j++)
12291 + n[j] = 0;
12292 + for (j = 0; j < base[0]; j++)
12293 + n[i + j] = base[base[0] - j];
12294 +
12295 + /* Allocate a and b of size 2*mlen. Set a = 1 */
12296 + a = snewn(mem_ctx, 2 * mlen, BignumInt);
12297 + //if (!a)
12298 + //abort(); /* FIXME */
12299 + b = snewn(mem_ctx, 2 * mlen, BignumInt);
12300 + //if (!b)
12301 + //abort(); /* FIXME */
12302 + for (i = 0; i < 2 * mlen; i++)
12303 + a[i] = 0;
12304 + a[2 * mlen - 1] = 1;
12305 +
12306 + /* Skip leading zero bits of exp. */
12307 + i = 0;
12308 + j = BIGNUM_INT_BITS - 1;
12309 + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
12310 + j--;
12311 + if (j < 0) {
12312 + i++;
12313 + j = BIGNUM_INT_BITS - 1;
12314 + }
12315 + }
12316 +
12317 + /* Main computation */
12318 + while (i < exp[0]) {
12319 + while (j >= 0) {
12320 + internal_mul(a + mlen, a + mlen, b, mlen);
12321 + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
12322 + if ((exp[exp[0] - i] & (1 << j)) != 0) {
12323 + internal_mul(b + mlen, n, a, mlen);
12324 + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
12325 + } else {
12326 + BignumInt *t;
12327 + t = a;
12328 + a = b;
12329 + b = t;
12330 + }
12331 + j--;
12332 + }
12333 + i++;
12334 + j = BIGNUM_INT_BITS - 1;
12335 + }
12336 +
12337 + /* Fixup result in case the modulus was shifted */
12338 + if (mshift) {
12339 + for (i = mlen - 1; i < 2 * mlen - 1; i++)
12340 + a[i] =
12341 + (a[i] << mshift) | (a[i + 1] >>
12342 + (BIGNUM_INT_BITS - mshift));
12343 + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
12344 + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
12345 + for (i = 2 * mlen - 1; i >= mlen; i--)
12346 + a[i] =
12347 + (a[i] >> mshift) | (a[i - 1] <<
12348 + (BIGNUM_INT_BITS - mshift));
12349 + }
12350 +
12351 + /* Copy result to buffer */
12352 + result = newbn(mem_ctx, mod[0]);
12353 + for (i = 0; i < mlen; i++)
12354 + result[result[0] - i] = a[i + mlen];
12355 + while (result[0] > 1 && result[result[0]] == 0)
12356 + result[0]--;
12357 +
12358 + /* Free temporary arrays */
12359 + for (i = 0; i < 2 * mlen; i++)
12360 + a[i] = 0;
12361 + sfree(mem_ctx, a);
12362 + for (i = 0; i < 2 * mlen; i++)
12363 + b[i] = 0;
12364 + sfree(mem_ctx, b);
12365 + for (i = 0; i < mlen; i++)
12366 + m[i] = 0;
12367 + sfree(mem_ctx, m);
12368 + for (i = 0; i < mlen; i++)
12369 + n[i] = 0;
12370 + sfree(mem_ctx, n);
12371 +
12372 + freebn(mem_ctx, base);
12373 +
12374 + return result;
12375 +}
12376 +
12377 +
12378 +#ifdef UNITTEST
12379 +
12380 +static __u32 dh_p[] = {
12381 + 96,
12382 + 0xFFFFFFFF,
12383 + 0xFFFFFFFF,
12384 + 0xA93AD2CA,
12385 + 0x4B82D120,
12386 + 0xE0FD108E,
12387 + 0x43DB5BFC,
12388 + 0x74E5AB31,
12389 + 0x08E24FA0,
12390 + 0xBAD946E2,
12391 + 0x770988C0,
12392 + 0x7A615D6C,
12393 + 0xBBE11757,
12394 + 0x177B200C,
12395 + 0x521F2B18,
12396 + 0x3EC86A64,
12397 + 0xD8760273,
12398 + 0xD98A0864,
12399 + 0xF12FFA06,
12400 + 0x1AD2EE6B,
12401 + 0xCEE3D226,
12402 + 0x4A25619D,
12403 + 0x1E8C94E0,
12404 + 0xDB0933D7,
12405 + 0xABF5AE8C,
12406 + 0xA6E1E4C7,
12407 + 0xB3970F85,
12408 + 0x5D060C7D,
12409 + 0x8AEA7157,
12410 + 0x58DBEF0A,
12411 + 0xECFB8504,
12412 + 0xDF1CBA64,
12413 + 0xA85521AB,
12414 + 0x04507A33,
12415 + 0xAD33170D,
12416 + 0x8AAAC42D,
12417 + 0x15728E5A,
12418 + 0x98FA0510,
12419 + 0x15D22618,
12420 + 0xEA956AE5,
12421 + 0x3995497C,
12422 + 0x95581718,
12423 + 0xDE2BCBF6,
12424 + 0x6F4C52C9,
12425 + 0xB5C55DF0,
12426 + 0xEC07A28F,
12427 + 0x9B2783A2,
12428 + 0x180E8603,
12429 + 0xE39E772C,
12430 + 0x2E36CE3B,
12431 + 0x32905E46,
12432 + 0xCA18217C,
12433 + 0xF1746C08,
12434 + 0x4ABC9804,
12435 + 0x670C354E,
12436 + 0x7096966D,
12437 + 0x9ED52907,
12438 + 0x208552BB,
12439 + 0x1C62F356,
12440 + 0xDCA3AD96,
12441 + 0x83655D23,
12442 + 0xFD24CF5F,
12443 + 0x69163FA8,
12444 + 0x1C55D39A,
12445 + 0x98DA4836,
12446 + 0xA163BF05,
12447 + 0xC2007CB8,
12448 + 0xECE45B3D,
12449 + 0x49286651,
12450 + 0x7C4B1FE6,
12451 + 0xAE9F2411,
12452 + 0x5A899FA5,
12453 + 0xEE386BFB,
12454 + 0xF406B7ED,
12455 + 0x0BFF5CB6,
12456 + 0xA637ED6B,
12457 + 0xF44C42E9,
12458 + 0x625E7EC6,
12459 + 0xE485B576,
12460 + 0x6D51C245,
12461 + 0x4FE1356D,
12462 + 0xF25F1437,
12463 + 0x302B0A6D,
12464 + 0xCD3A431B,
12465 + 0xEF9519B3,
12466 + 0x8E3404DD,
12467 + 0x514A0879,
12468 + 0x3B139B22,
12469 + 0x020BBEA6,
12470 + 0x8A67CC74,
12471 + 0x29024E08,
12472 + 0x80DC1CD1,
12473 + 0xC4C6628B,
12474 + 0x2168C234,
12475 + 0xC90FDAA2,
12476 + 0xFFFFFFFF,
12477 + 0xFFFFFFFF,
12478 +};
12479 +
12480 +static __u32 dh_a[] = {
12481 + 8,
12482 + 0xdf367516,
12483 + 0x86459caa,
12484 + 0xe2d459a4,
12485 + 0xd910dae0,
12486 + 0x8a8b5e37,
12487 + 0x67ab31c6,
12488 + 0xf0b55ea9,
12489 + 0x440051d6,
12490 +};
12491 +
12492 +static __u32 dh_b[] = {
12493 + 8,
12494 + 0xded92656,
12495 + 0xe07a048a,
12496 + 0x6fa452cd,
12497 + 0x2df89d30,
12498 + 0xc75f1b0f,
12499 + 0x8ce3578f,
12500 + 0x7980a324,
12501 + 0x5daec786,
12502 +};
12503 +
12504 +static __u32 dh_g[] = {
12505 + 1,
12506 + 2,
12507 +};
12508 +
12509 +int main(void)
12510 +{
12511 + int i;
12512 + __u32 *k;
12513 + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
12514 +
12515 + printf("\n\n");
12516 + for (i=0; i<k[0]; i++) {
12517 + __u32 word32 = k[k[0] - i];
12518 + __u16 l = word32 & 0xffff;
12519 + __u16 m = (word32 & 0xffff0000) >> 16;
12520 + printf("%04x %04x ", m, l);
12521 + if (!((i + 1)%13)) printf("\n");
12522 + }
12523 + printf("\n\n");
12524 +
12525 + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
12526 + printf("PASS\n\n");
12527 + }
12528 + else {
12529 + printf("FAIL\n\n");
12530 + }
12531 +
12532 +}
12533 +
12534 +#endif /* UNITTEST */
12535 +
12536 +#endif /* CONFIG_MACH_IPMATE */
12537 +
12538 +#endif /*DWC_CRYPTOLIB */
12539 --- /dev/null
12540 +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.h
12541 @@ -0,0 +1,34 @@
12542 +/*
12543 + * dwc_modpow.h
12544 + * See dwc_modpow.c for license and changes
12545 + */
12546 +#ifndef _DWC_MODPOW_H
12547 +#define _DWC_MODPOW_H
12548 +
12549 +#ifdef __cplusplus
12550 +extern "C" {
12551 +#endif
12552 +
12553 +#include "dwc_os.h"
12554 +
12555 +/** @file
12556 + *
12557 + * This file defines the module exponentiation function which is only used
12558 + * internally by the DWC UWB modules for calculation of PKs during numeric
12559 + * association. The routine is taken from the PUTTY, an open source terminal
12560 + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
12561 + *
12562 + */
12563 +
12564 +typedef uint32_t BignumInt;
12565 +typedef uint64_t BignumDblInt;
12566 +typedef BignumInt *Bignum;
12567 +
12568 +/* Compute modular exponentiaion */
12569 +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
12570 +
12571 +#ifdef __cplusplus
12572 +}
12573 +#endif
12574 +
12575 +#endif /* _LINUX_BIGNUM_H */
12576 --- /dev/null
12577 +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.c
12578 @@ -0,0 +1,319 @@
12579 +#ifdef DWC_NOTIFYLIB
12580 +
12581 +#include "dwc_notifier.h"
12582 +#include "dwc_list.h"
12583 +
12584 +typedef struct dwc_observer {
12585 + void *observer;
12586 + dwc_notifier_callback_t callback;
12587 + void *data;
12588 + char *notification;
12589 + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
12590 +} observer_t;
12591 +
12592 +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
12593 +
12594 +typedef struct dwc_notifier {
12595 + void *mem_ctx;
12596 + void *object;
12597 + struct observer_queue observers;
12598 + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
12599 +} notifier_t;
12600 +
12601 +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
12602 +
12603 +typedef struct manager {
12604 + void *mem_ctx;
12605 + void *wkq_ctx;
12606 + dwc_workq_t *wq;
12607 +// dwc_mutex_t *mutex;
12608 + struct notifier_queue notifiers;
12609 +} manager_t;
12610 +
12611 +static manager_t *manager = NULL;
12612 +
12613 +static int create_manager(void *mem_ctx, void *wkq_ctx)
12614 +{
12615 + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
12616 + if (!manager) {
12617 + return -DWC_E_NO_MEMORY;
12618 + }
12619 +
12620 + DWC_CIRCLEQ_INIT(&manager->notifiers);
12621 +
12622 + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
12623 + if (!manager->wq) {
12624 + return -DWC_E_NO_MEMORY;
12625 + }
12626 +
12627 + return 0;
12628 +}
12629 +
12630 +static void free_manager(void)
12631 +{
12632 + dwc_workq_free(manager->wq);
12633 +
12634 + /* All notifiers must have unregistered themselves before this module
12635 + * can be removed. Hitting this assertion indicates a programmer
12636 + * error. */
12637 + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
12638 + "Notification manager being freed before all notifiers have been removed");
12639 + dwc_free(manager->mem_ctx, manager);
12640 +}
12641 +
12642 +#ifdef DEBUG
12643 +static void dump_manager(void)
12644 +{
12645 + notifier_t *n;
12646 + observer_t *o;
12647 +
12648 + DWC_ASSERT(manager, "Notification manager not found");
12649 +
12650 + DWC_DEBUG("List of all notifiers and observers:\n");
12651 + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
12652 + DWC_DEBUG("Notifier %p has observers:\n", n->object);
12653 + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
12654 + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
12655 + }
12656 + }
12657 +}
12658 +#else
12659 +#define dump_manager(...)
12660 +#endif
12661 +
12662 +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
12663 + dwc_notifier_callback_t callback, void *data)
12664 +{
12665 + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
12666 +
12667 + if (!new_observer) {
12668 + return NULL;
12669 + }
12670 +
12671 + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
12672 + new_observer->observer = observer;
12673 + new_observer->notification = notification;
12674 + new_observer->callback = callback;
12675 + new_observer->data = data;
12676 + return new_observer;
12677 +}
12678 +
12679 +static void free_observer(void *mem_ctx, observer_t *observer)
12680 +{
12681 + dwc_free(mem_ctx, observer);
12682 +}
12683 +
12684 +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
12685 +{
12686 + notifier_t *notifier;
12687 +
12688 + if (!object) {
12689 + return NULL;
12690 + }
12691 +
12692 + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
12693 + if (!notifier) {
12694 + return NULL;
12695 + }
12696 +
12697 + DWC_CIRCLEQ_INIT(&notifier->observers);
12698 + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
12699 +
12700 + notifier->mem_ctx = mem_ctx;
12701 + notifier->object = object;
12702 + return notifier;
12703 +}
12704 +
12705 +static void free_notifier(notifier_t *notifier)
12706 +{
12707 + observer_t *observer;
12708 +
12709 + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
12710 + free_observer(notifier->mem_ctx, observer);
12711 + }
12712 +
12713 + dwc_free(notifier->mem_ctx, notifier);
12714 +}
12715 +
12716 +static notifier_t *find_notifier(void *object)
12717 +{
12718 + notifier_t *notifier;
12719 +
12720 + DWC_ASSERT(manager, "Notification manager not found");
12721 +
12722 + if (!object) {
12723 + return NULL;
12724 + }
12725 +
12726 + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
12727 + if (notifier->object == object) {
12728 + return notifier;
12729 + }
12730 + }
12731 +
12732 + return NULL;
12733 +}
12734 +
12735 +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
12736 +{
12737 + return create_manager(mem_ctx, wkq_ctx);
12738 +}
12739 +
12740 +void dwc_free_notification_manager(void)
12741 +{
12742 + free_manager();
12743 +}
12744 +
12745 +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
12746 +{
12747 + notifier_t *notifier;
12748 +
12749 + DWC_ASSERT(manager, "Notification manager not found");
12750 +
12751 + notifier = find_notifier(object);
12752 + if (notifier) {
12753 + DWC_ERROR("Notifier %p is already registered\n", object);
12754 + return NULL;
12755 + }
12756 +
12757 + notifier = alloc_notifier(mem_ctx, object);
12758 + if (!notifier) {
12759 + return NULL;
12760 + }
12761 +
12762 + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
12763 +
12764 + DWC_INFO("Notifier %p registered", object);
12765 + dump_manager();
12766 +
12767 + return notifier;
12768 +}
12769 +
12770 +void dwc_unregister_notifier(dwc_notifier_t *notifier)
12771 +{
12772 + DWC_ASSERT(manager, "Notification manager not found");
12773 +
12774 + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
12775 + observer_t *o;
12776 +
12777 + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
12778 + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
12779 + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
12780 + }
12781 +
12782 + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
12783 + "Notifier %p has active observers when removing", notifier);
12784 + }
12785 +
12786 + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
12787 + free_notifier(notifier);
12788 +
12789 + DWC_INFO("Notifier unregistered");
12790 + dump_manager();
12791 +}
12792 +
12793 +/* Add an observer to observe the notifier for a particular state, event, or notification. */
12794 +int dwc_add_observer(void *observer, void *object, char *notification,
12795 + dwc_notifier_callback_t callback, void *data)
12796 +{
12797 + notifier_t *notifier = find_notifier(object);
12798 + observer_t *new_observer;
12799 +
12800 + if (!notifier) {
12801 + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
12802 + return -DWC_E_INVALID;
12803 + }
12804 +
12805 + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
12806 + if (!new_observer) {
12807 + return -DWC_E_NO_MEMORY;
12808 + }
12809 +
12810 + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
12811 +
12812 + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
12813 + observer, object, notification, callback, data);
12814 +
12815 + dump_manager();
12816 + return 0;
12817 +}
12818 +
12819 +int dwc_remove_observer(void *observer)
12820 +{
12821 + notifier_t *n;
12822 +
12823 + DWC_ASSERT(manager, "Notification manager not found");
12824 +
12825 + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
12826 + observer_t *o;
12827 + observer_t *o2;
12828 +
12829 + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
12830 + if (o->observer == observer) {
12831 + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
12832 + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
12833 + o->observer, n->object, o->notification);
12834 + free_observer(n->mem_ctx, o);
12835 + }
12836 + }
12837 + }
12838 +
12839 + dump_manager();
12840 + return 0;
12841 +}
12842 +
12843 +typedef struct callback_data {
12844 + void *mem_ctx;
12845 + dwc_notifier_callback_t cb;
12846 + void *observer;
12847 + void *data;
12848 + void *object;
12849 + char *notification;
12850 + void *notification_data;
12851 +} cb_data_t;
12852 +
12853 +static void cb_task(void *data)
12854 +{
12855 + cb_data_t *cb = (cb_data_t *)data;
12856 +
12857 + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
12858 + dwc_free(cb->mem_ctx, cb);
12859 +}
12860 +
12861 +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
12862 +{
12863 + observer_t *o;
12864 +
12865 + DWC_ASSERT(manager, "Notification manager not found");
12866 +
12867 + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
12868 + int len = DWC_STRLEN(notification);
12869 +
12870 + if (DWC_STRLEN(o->notification) != len) {
12871 + continue;
12872 + }
12873 +
12874 + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
12875 + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
12876 +
12877 + if (!cb_data) {
12878 + DWC_ERROR("Failed to allocate callback data\n");
12879 + return;
12880 + }
12881 +
12882 + cb_data->mem_ctx = notifier->mem_ctx;
12883 + cb_data->cb = o->callback;
12884 + cb_data->observer = o->observer;
12885 + cb_data->data = o->data;
12886 + cb_data->object = notifier->object;
12887 + cb_data->notification = notification;
12888 + cb_data->notification_data = notification_data;
12889 + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
12890 + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
12891 + "Notify callback from %p for Notification %s, to observer %p",
12892 + cb_data->object, notification, cb_data->observer);
12893 + }
12894 + }
12895 +}
12896 +
12897 +#endif /* DWC_NOTIFYLIB */
12898 --- /dev/null
12899 +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.h
12900 @@ -0,0 +1,122 @@
12901 +
12902 +#ifndef __DWC_NOTIFIER_H__
12903 +#define __DWC_NOTIFIER_H__
12904 +
12905 +#ifdef __cplusplus
12906 +extern "C" {
12907 +#endif
12908 +
12909 +#include "dwc_os.h"
12910 +
12911 +/** @file
12912 + *
12913 + * A simple implementation of the Observer pattern. Any "module" can
12914 + * register as an observer or notifier. The notion of "module" is abstract and
12915 + * can mean anything used to identify either an observer or notifier. Usually
12916 + * it will be a pointer to a data structure which contains some state, ie an
12917 + * object.
12918 + *
12919 + * Before any notifiers can be added, the global notification manager must be
12920 + * brought up with dwc_alloc_notification_manager().
12921 + * dwc_free_notification_manager() will bring it down and free all resources.
12922 + * These would typically be called upon module load and unload. The
12923 + * notification manager is a single global instance that handles all registered
12924 + * observable modules and observers so this should be done only once.
12925 + *
12926 + * A module can be observable by using Notifications to publicize some general
12927 + * information about it's state or operation. It does not care who listens, or
12928 + * even if anyone listens, or what they do with the information. The observable
12929 + * modules do not need to know any information about it's observers or their
12930 + * interface, or their state or data.
12931 + *
12932 + * Any module can register to emit Notifications. It should publish a list of
12933 + * notifications that it can emit and their behavior, such as when they will get
12934 + * triggered, and what information will be provided to the observer. Then it
12935 + * should register itself as an observable module. See dwc_register_notifier().
12936 + *
12937 + * Any module can observe any observable, registered module, provided it has a
12938 + * handle to the other module and knows what notifications to observe. See
12939 + * dwc_add_observer().
12940 + *
12941 + * A function of type dwc_notifier_callback_t is called whenever a notification
12942 + * is triggered with one or more observers observing it. This function is
12943 + * called in it's own process so it may sleep or block if needed. It is
12944 + * guaranteed to be called sometime after the notification has occurred and will
12945 + * be called once per each time the notification is triggered. It will NOT be
12946 + * called in the same process context used to trigger the notification.
12947 + *
12948 + * @section Limitiations
12949 + *
12950 + * Keep in mind that Notifications that can be triggered in rapid sucession may
12951 + * schedule too many processes too handle. Be aware of this limitation when
12952 + * designing to use notifications, and only add notifications for appropriate
12953 + * observable information.
12954 + *
12955 + * Also Notification callbacks are not synchronous. If you need to synchronize
12956 + * the behavior between module/observer you must use other means. And perhaps
12957 + * that will mean Notifications are not the proper solution.
12958 + */
12959 +
12960 +struct dwc_notifier;
12961 +typedef struct dwc_notifier dwc_notifier_t;
12962 +
12963 +/** The callback function must be of this type.
12964 + *
12965 + * @param object This is the object that is being observed.
12966 + * @param notification This is the notification that was triggered.
12967 + * @param observer This is the observer
12968 + * @param notification_data This is notification-specific data that the notifier
12969 + * has included in this notification. The value of this should be published in
12970 + * the documentation of the observable module with the notifications.
12971 + * @param user_data This is any custom data that the observer provided when
12972 + * adding itself as an observer to the notification. */
12973 +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
12974 + void *notification_data, void *user_data);
12975 +
12976 +/** Brings up the notification manager. */
12977 +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
12978 +/** Brings down the notification manager. */
12979 +extern void dwc_free_notification_manager(void);
12980 +
12981 +/** This function registers an observable module. A dwc_notifier_t object is
12982 + * returned to the observable module. This is an opaque object that is used by
12983 + * the observable module to trigger notifications. This object should only be
12984 + * accessible to functions that are authorized to trigger notifications for this
12985 + * module. Observers do not need this object. */
12986 +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
12987 +
12988 +/** This function unregisters an observable module. All observers have to be
12989 + * removed prior to unregistration. */
12990 +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
12991 +
12992 +/** Add a module as an observer to the observable module. The observable module
12993 + * needs to have previously registered with the notification manager.
12994 + *
12995 + * @param observer The observer module
12996 + * @param object The module to observe
12997 + * @param notification The notification to observe
12998 + * @param callback The callback function to call
12999 + * @param user_data Any additional user data to pass into the callback function */
13000 +extern int dwc_add_observer(void *observer, void *object, char *notification,
13001 + dwc_notifier_callback_t callback, void *user_data);
13002 +
13003 +/** Removes the specified observer from all notifications that it is currently
13004 + * observing. */
13005 +extern int dwc_remove_observer(void *observer);
13006 +
13007 +/** This function triggers a Notification. It should be called by the
13008 + * observable module, or any module or library which the observable module
13009 + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
13010 + *
13011 + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
13012 + * their own process context for each trigger. Callbacks can be blocking.
13013 + * dwc_notify can be called from interrupt context if needed.
13014 + *
13015 + */
13016 +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
13017 +
13018 +#ifdef __cplusplus
13019 +}
13020 +#endif
13021 +
13022 +#endif /* __DWC_NOTIFIER_H__ */
13023 --- /dev/null
13024 +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
13025 @@ -0,0 +1,1276 @@
13026 +/* =========================================================================
13027 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
13028 + * $Revision: #14 $
13029 + * $Date: 2010/11/04 $
13030 + * $Change: 1621695 $
13031 + *
13032 + * Synopsys Portability Library Software and documentation
13033 + * (hereinafter, "Software") is an Unsupported proprietary work of
13034 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
13035 + * between Synopsys and you.
13036 + *
13037 + * The Software IS NOT an item of Licensed Software or Licensed Product
13038 + * under any End User Software License Agreement or Agreement for
13039 + * Licensed Product with Synopsys or any supplement thereto. You are
13040 + * permitted to use and redistribute this Software in source and binary
13041 + * forms, with or without modification, provided that redistributions
13042 + * of source code must retain this notice. You may not view, use,
13043 + * disclose, copy or distribute this file or any information contained
13044 + * herein except pursuant to this license grant from Synopsys. If you
13045 + * do not agree with this notice, including the disclaimer below, then
13046 + * you are not authorized to use the Software.
13047 + *
13048 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
13049 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13050 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13051 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
13052 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
13053 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
13054 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
13055 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
13056 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13057 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
13058 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
13059 + * DAMAGE.
13060 + * ========================================================================= */
13061 +#ifndef _DWC_OS_H_
13062 +#define _DWC_OS_H_
13063 +
13064 +#ifdef __cplusplus
13065 +extern "C" {
13066 +#endif
13067 +
13068 +/** @file
13069 + *
13070 + * DWC portability library, low level os-wrapper functions
13071 + *
13072 + */
13073 +
13074 +/* These basic types need to be defined by some OS header file or custom header
13075 + * file for your specific target architecture.
13076 + *
13077 + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
13078 + *
13079 + * Any custom or alternate header file must be added and enabled here.
13080 + */
13081 +
13082 +#ifdef DWC_LINUX
13083 +# include <linux/types.h>
13084 +# ifdef CONFIG_DEBUG_MUTEXES
13085 +# include <linux/mutex.h>
13086 +# endif
13087 +# include <linux/spinlock.h>
13088 +# include <linux/errno.h>
13089 +# include <stdarg.h>
13090 +#endif
13091 +
13092 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13093 +# include <os_dep.h>
13094 +#endif
13095 +
13096 +
13097 +/** @name Primitive Types and Values */
13098 +
13099 +/** We define a boolean type for consistency. Can be either YES or NO */
13100 +typedef uint8_t dwc_bool_t;
13101 +#define YES 1
13102 +#define NO 0
13103 +
13104 +#ifdef DWC_LINUX
13105 +
13106 +/** @name Error Codes */
13107 +#define DWC_E_INVALID EINVAL
13108 +#define DWC_E_NO_MEMORY ENOMEM
13109 +#define DWC_E_NO_DEVICE ENODEV
13110 +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
13111 +#define DWC_E_TIMEOUT ETIMEDOUT
13112 +#define DWC_E_BUSY EBUSY
13113 +#define DWC_E_AGAIN EAGAIN
13114 +#define DWC_E_RESTART ERESTART
13115 +#define DWC_E_ABORT ECONNABORTED
13116 +#define DWC_E_SHUTDOWN ESHUTDOWN
13117 +#define DWC_E_NO_DATA ENODATA
13118 +#define DWC_E_DISCONNECT ECONNRESET
13119 +#define DWC_E_UNKNOWN EINVAL
13120 +#define DWC_E_NO_STREAM_RES ENOSR
13121 +#define DWC_E_COMMUNICATION ECOMM
13122 +#define DWC_E_OVERFLOW EOVERFLOW
13123 +#define DWC_E_PROTOCOL EPROTO
13124 +#define DWC_E_IN_PROGRESS EINPROGRESS
13125 +#define DWC_E_PIPE EPIPE
13126 +#define DWC_E_IO EIO
13127 +#define DWC_E_NO_SPACE ENOSPC
13128 +
13129 +#else
13130 +
13131 +/** @name Error Codes */
13132 +#define DWC_E_INVALID 1001
13133 +#define DWC_E_NO_MEMORY 1002
13134 +#define DWC_E_NO_DEVICE 1003
13135 +#define DWC_E_NOT_SUPPORTED 1004
13136 +#define DWC_E_TIMEOUT 1005
13137 +#define DWC_E_BUSY 1006
13138 +#define DWC_E_AGAIN 1007
13139 +#define DWC_E_RESTART 1008
13140 +#define DWC_E_ABORT 1009
13141 +#define DWC_E_SHUTDOWN 1010
13142 +#define DWC_E_NO_DATA 1011
13143 +#define DWC_E_DISCONNECT 2000
13144 +#define DWC_E_UNKNOWN 3000
13145 +#define DWC_E_NO_STREAM_RES 4001
13146 +#define DWC_E_COMMUNICATION 4002
13147 +#define DWC_E_OVERFLOW 4003
13148 +#define DWC_E_PROTOCOL 4004
13149 +#define DWC_E_IN_PROGRESS 4005
13150 +#define DWC_E_PIPE 4006
13151 +#define DWC_E_IO 4007
13152 +#define DWC_E_NO_SPACE 4008
13153 +
13154 +#endif
13155 +
13156 +
13157 +/** @name Tracing/Logging Functions
13158 + *
13159 + * These function provide the capability to add tracing, debugging, and error
13160 + * messages, as well exceptions as assertions. The WUDEV uses these
13161 + * extensively. These could be logged to the main console, the serial port, an
13162 + * internal buffer, etc. These functions could also be no-op if they are too
13163 + * expensive on your system. By default undefining the DEBUG macro already
13164 + * no-ops some of these functions. */
13165 +
13166 +/** Returns non-zero if in interrupt context. */
13167 +extern dwc_bool_t DWC_IN_IRQ(void);
13168 +#define dwc_in_irq DWC_IN_IRQ
13169 +
13170 +/** Returns "IRQ" if DWC_IN_IRQ is true. */
13171 +static inline char *dwc_irq(void) {
13172 + return DWC_IN_IRQ() ? "IRQ" : "";
13173 +}
13174 +
13175 +/** Returns non-zero if in bottom-half context. */
13176 +extern dwc_bool_t DWC_IN_BH(void);
13177 +#define dwc_in_bh DWC_IN_BH
13178 +
13179 +/** Returns "BH" if DWC_IN_BH is true. */
13180 +static inline char *dwc_bh(void) {
13181 + return DWC_IN_BH() ? "BH" : "";
13182 +}
13183 +
13184 +/**
13185 + * A vprintf() clone. Just call vprintf if you've got it.
13186 + */
13187 +extern void DWC_VPRINTF(char *format, va_list args);
13188 +#define dwc_vprintf DWC_VPRINTF
13189 +
13190 +/**
13191 + * A vsnprintf() clone. Just call vprintf if you've got it.
13192 + */
13193 +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
13194 +#define dwc_vsnprintf DWC_VSNPRINTF
13195 +
13196 +/**
13197 + * printf() clone. Just call printf if you've go it.
13198 + */
13199 +extern void DWC_PRINTF(char *format, ...)
13200 +/* This provides compiler level static checking of the parameters if you're
13201 + * using GCC. */
13202 +#ifdef __GNUC__
13203 + __attribute__ ((format(printf, 1, 2)));
13204 +#else
13205 + ;
13206 +#endif
13207 +#define dwc_printf DWC_PRINTF
13208 +
13209 +/**
13210 + * sprintf() clone. Just call sprintf if you've got it.
13211 + */
13212 +extern int DWC_SPRINTF(char *string, char *format, ...)
13213 +#ifdef __GNUC__
13214 + __attribute__ ((format(printf, 2, 3)));
13215 +#else
13216 + ;
13217 +#endif
13218 +#define dwc_sprintf DWC_SPRINTF
13219 +
13220 +/**
13221 + * snprintf() clone. Just call snprintf if you've got it.
13222 + */
13223 +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
13224 +#ifdef __GNUC__
13225 + __attribute__ ((format(printf, 3, 4)));
13226 +#else
13227 + ;
13228 +#endif
13229 +#define dwc_snprintf DWC_SNPRINTF
13230 +
13231 +/**
13232 + * Prints a WARNING message. On systems that don't differentiate between
13233 + * warnings and regular log messages, just print it. Indicates that something
13234 + * may be wrong with the driver. Works like printf().
13235 + *
13236 + * Use the DWC_WARN macro to call this function.
13237 + */
13238 +extern void __DWC_WARN(char *format, ...)
13239 +#ifdef __GNUC__
13240 + __attribute__ ((format(printf, 1, 2)));
13241 +#else
13242 + ;
13243 +#endif
13244 +
13245 +/**
13246 + * Prints an error message. On systems that don't differentiate between errors
13247 + * and regular log messages, just print it. Indicates that something went wrong
13248 + * with the driver. Works like printf().
13249 + *
13250 + * Use the DWC_ERROR macro to call this function.
13251 + */
13252 +extern void __DWC_ERROR(char *format, ...)
13253 +#ifdef __GNUC__
13254 + __attribute__ ((format(printf, 1, 2)));
13255 +#else
13256 + ;
13257 +#endif
13258 +
13259 +/**
13260 + * Prints an exception error message and takes some user-defined action such as
13261 + * print out a backtrace or trigger a breakpoint. Indicates that something went
13262 + * abnormally wrong with the driver such as programmer error, or other
13263 + * exceptional condition. It should not be ignored so even on systems without
13264 + * printing capability, some action should be taken to notify the developer of
13265 + * it. Works like printf().
13266 + */
13267 +extern void DWC_EXCEPTION(char *format, ...)
13268 +#ifdef __GNUC__
13269 + __attribute__ ((format(printf, 1, 2)));
13270 +#else
13271 + ;
13272 +#endif
13273 +#define dwc_exception DWC_EXCEPTION
13274 +
13275 +#ifndef DWC_OTG_DEBUG_LEV
13276 +#define DWC_OTG_DEBUG_LEV 0
13277 +#endif
13278 +
13279 +#ifdef DEBUG
13280 +/**
13281 + * Prints out a debug message. Used for logging/trace messages.
13282 + *
13283 + * Use the DWC_DEBUG macro to call this function
13284 + */
13285 +extern void __DWC_DEBUG(char *format, ...)
13286 +#ifdef __GNUC__
13287 + __attribute__ ((format(printf, 1, 2)));
13288 +#else
13289 + ;
13290 +#endif
13291 +#else
13292 +#define __DWC_DEBUG printk
13293 +#endif
13294 +
13295 +/**
13296 + * Prints out a Debug message.
13297 + */
13298 +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
13299 + __func__, dwc_irq(), ## _args)
13300 +#define dwc_debug DWC_DEBUG
13301 +/**
13302 + * Prints out a Debug message if enabled at compile time.
13303 + */
13304 +#if DWC_OTG_DEBUG_LEV > 0
13305 +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
13306 +#else
13307 +#define DWC_DEBUGC(_format, _args...)
13308 +#endif
13309 +#define dwc_debugc DWC_DEBUGC
13310 +/**
13311 + * Prints out an informative message.
13312 + */
13313 +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
13314 + dwc_irq(), ## _args)
13315 +#define dwc_info DWC_INFO
13316 +/**
13317 + * Prints out an informative message if enabled at compile time.
13318 + */
13319 +#if DWC_OTG_DEBUG_LEV > 1
13320 +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
13321 +#else
13322 +#define DWC_INFOC(_format, _args...)
13323 +#endif
13324 +#define dwc_infoc DWC_INFOC
13325 +/**
13326 + * Prints out a warning message.
13327 + */
13328 +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
13329 + dwc_irq(), __func__, __LINE__, ## _args)
13330 +#define dwc_warn DWC_WARN
13331 +/**
13332 + * Prints out an error message.
13333 + */
13334 +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
13335 + dwc_irq(), __func__, __LINE__, ## _args)
13336 +#define dwc_error DWC_ERROR
13337 +
13338 +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
13339 + dwc_irq(), __func__, __LINE__, ## _args)
13340 +#define dwc_proto_error DWC_PROTO_ERROR
13341 +
13342 +#ifdef DEBUG
13343 +/** Prints out a exception error message if the _expr expression fails. Disabled
13344 + * if DEBUG is not enabled. */
13345 +#define DWC_ASSERT(_expr, _format, _args...) do { \
13346 + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
13347 + __FILE__, __LINE__, ## _args); } \
13348 + } while (0)
13349 +#else
13350 +#define DWC_ASSERT(_x...)
13351 +#endif
13352 +#define dwc_assert DWC_ASSERT
13353 +
13354 +
13355 +/** @name Byte Ordering
13356 + * The following functions are for conversions between processor's byte ordering
13357 + * and specific ordering you want.
13358 + */
13359 +
13360 +/** Converts 32 bit data in CPU byte ordering to little endian. */
13361 +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
13362 +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
13363 +
13364 +/** Converts 32 bit data in CPU byte orderint to big endian. */
13365 +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
13366 +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
13367 +
13368 +/** Converts 32 bit little endian data to CPU byte ordering. */
13369 +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
13370 +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
13371 +
13372 +/** Converts 32 bit big endian data to CPU byte ordering. */
13373 +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
13374 +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
13375 +
13376 +/** Converts 16 bit data in CPU byte ordering to little endian. */
13377 +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
13378 +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
13379 +
13380 +/** Converts 16 bit data in CPU byte orderint to big endian. */
13381 +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
13382 +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
13383 +
13384 +/** Converts 16 bit little endian data to CPU byte ordering. */
13385 +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
13386 +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
13387 +
13388 +/** Converts 16 bit bi endian data to CPU byte ordering. */
13389 +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
13390 +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
13391 +
13392 +
13393 +/** @name Register Read/Write
13394 + *
13395 + * The following six functions should be implemented to read/write registers of
13396 + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
13397 + * The reg value is a pointer to the register calculated from the void *base
13398 + * variable passed into the driver when it is started. */
13399 +
13400 +#ifdef DWC_LINUX
13401 +/* Linux doesn't need any extra parameters for register read/write, so we
13402 + * just throw away the IO context parameter.
13403 + */
13404 +/** Reads the content of a 32-bit register. */
13405 +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
13406 +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
13407 +
13408 +/** Reads the content of a 64-bit register. */
13409 +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
13410 +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
13411 +
13412 +/** Writes to a 32-bit register. */
13413 +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
13414 +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
13415 +
13416 +/** Writes to a 64-bit register. */
13417 +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
13418 +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
13419 +
13420 +/**
13421 + * Modify bit values in a register. Using the
13422 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13423 + */
13424 +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
13425 +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
13426 +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
13427 +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
13428 +
13429 +#endif /* DWC_LINUX */
13430 +
13431 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13432 +typedef struct dwc_ioctx {
13433 + struct device *dev;
13434 + bus_space_tag_t iot;
13435 + bus_space_handle_t ioh;
13436 +} dwc_ioctx_t;
13437 +
13438 +/** BSD needs two extra parameters for register read/write, so we pass
13439 + * them in using the IO context parameter.
13440 + */
13441 +/** Reads the content of a 32-bit register. */
13442 +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
13443 +#define dwc_read_reg32 DWC_READ_REG32
13444 +
13445 +/** Reads the content of a 64-bit register. */
13446 +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
13447 +#define dwc_read_reg64 DWC_READ_REG64
13448 +
13449 +/** Writes to a 32-bit register. */
13450 +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
13451 +#define dwc_write_reg32 DWC_WRITE_REG32
13452 +
13453 +/** Writes to a 64-bit register. */
13454 +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
13455 +#define dwc_write_reg64 DWC_WRITE_REG64
13456 +
13457 +/**
13458 + * Modify bit values in a register. Using the
13459 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13460 + */
13461 +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
13462 +#define dwc_modify_reg32 DWC_MODIFY_REG32
13463 +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
13464 +#define dwc_modify_reg64 DWC_MODIFY_REG64
13465 +
13466 +#endif /* DWC_FREEBSD || DWC_NETBSD */
13467 +
13468 +/** @cond */
13469 +
13470 +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
13471 + * register writes. */
13472 +
13473 +#ifdef DWC_LINUX
13474 +
13475 +# ifdef DWC_DEBUG_REGS
13476 +
13477 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13478 +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
13479 + return DWC_READ_REG32(&container->regs->_reg[num]); \
13480 +} \
13481 +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
13482 + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
13483 + &(((uint32_t*)container->regs->_reg)[num]), data); \
13484 + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
13485 +}
13486 +
13487 +#define dwc_define_read_write_reg(_reg,_container_type) \
13488 +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
13489 + return DWC_READ_REG32(&container->regs->_reg); \
13490 +} \
13491 +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
13492 + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
13493 + DWC_WRITE_REG32(&container->regs->_reg, data); \
13494 +}
13495 +
13496 +# else /* DWC_DEBUG_REGS */
13497 +
13498 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13499 +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
13500 + return DWC_READ_REG32(&container->regs->_reg[num]); \
13501 +} \
13502 +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
13503 + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
13504 +}
13505 +
13506 +#define dwc_define_read_write_reg(_reg,_container_type) \
13507 +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
13508 + return DWC_READ_REG32(&container->regs->_reg); \
13509 +} \
13510 +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
13511 + DWC_WRITE_REG32(&container->regs->_reg, data); \
13512 +}
13513 +
13514 +# endif /* DWC_DEBUG_REGS */
13515 +
13516 +#endif /* DWC_LINUX */
13517 +
13518 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13519 +
13520 +# ifdef DWC_DEBUG_REGS
13521 +
13522 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13523 +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
13524 + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
13525 +} \
13526 +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
13527 + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
13528 + &(((uint32_t*)container->regs->_reg)[num]), data); \
13529 + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
13530 +}
13531 +
13532 +#define dwc_define_read_write_reg(_reg,_container_type) \
13533 +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
13534 + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
13535 +} \
13536 +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
13537 + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
13538 + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
13539 +}
13540 +
13541 +# else /* DWC_DEBUG_REGS */
13542 +
13543 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13544 +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
13545 + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
13546 +} \
13547 +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
13548 + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
13549 +}
13550 +
13551 +#define dwc_define_read_write_reg(_reg,_container_type) \
13552 +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
13553 + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
13554 +} \
13555 +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
13556 + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
13557 +}
13558 +
13559 +# endif /* DWC_DEBUG_REGS */
13560 +
13561 +#endif /* DWC_FREEBSD || DWC_NETBSD */
13562 +
13563 +/** @endcond */
13564 +
13565 +
13566 +#ifdef DWC_CRYPTOLIB
13567 +/** @name Crypto Functions
13568 + *
13569 + * These are the low-level cryptographic functions used by the driver. */
13570 +
13571 +/** Perform AES CBC */
13572 +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
13573 +#define dwc_aes_cbc DWC_AES_CBC
13574 +
13575 +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
13576 +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
13577 +#define dwc_random_bytes DWC_RANDOM_BYTES
13578 +
13579 +/** Perform the SHA-256 hash function */
13580 +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
13581 +#define dwc_sha256 DWC_SHA256
13582 +
13583 +/** Calculated the HMAC-SHA256 */
13584 +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
13585 +#define dwc_hmac_sha256 DWC_HMAC_SHA256
13586 +
13587 +#endif /* DWC_CRYPTOLIB */
13588 +
13589 +
13590 +/** @name Memory Allocation
13591 + *
13592 + * These function provide access to memory allocation. There are only 2 DMA
13593 + * functions and 3 Regular memory functions that need to be implemented. None
13594 + * of the memory debugging routines need to be implemented. The allocation
13595 + * routines all ZERO the contents of the memory.
13596 + *
13597 + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
13598 + * This checks for memory leaks, keeping track of alloc/free pairs. It also
13599 + * keeps track of how much memory the driver is using at any given time. */
13600 +
13601 +#define DWC_PAGE_SIZE 4096
13602 +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
13603 +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
13604 +
13605 +#define DWC_INVALID_DMA_ADDR 0x0
13606 +
13607 +#ifdef DWC_LINUX
13608 +/** Type for a DMA address */
13609 +typedef dma_addr_t dwc_dma_t;
13610 +#endif
13611 +
13612 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13613 +typedef bus_addr_t dwc_dma_t;
13614 +#endif
13615 +
13616 +#ifdef DWC_FREEBSD
13617 +typedef struct dwc_dmactx {
13618 + struct device *dev;
13619 + bus_dma_tag_t dma_tag;
13620 + bus_dmamap_t dma_map;
13621 + bus_addr_t dma_paddr;
13622 + void *dma_vaddr;
13623 +} dwc_dmactx_t;
13624 +#endif
13625 +
13626 +#ifdef DWC_NETBSD
13627 +typedef struct dwc_dmactx {
13628 + struct device *dev;
13629 + bus_dma_tag_t dma_tag;
13630 + bus_dmamap_t dma_map;
13631 + bus_dma_segment_t segs[1];
13632 + int nsegs;
13633 + bus_addr_t dma_paddr;
13634 + void *dma_vaddr;
13635 +} dwc_dmactx_t;
13636 +#endif
13637 +
13638 +/* @todo these functions will be added in the future */
13639 +#if 0
13640 +/**
13641 + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
13642 + * allocated from this pool will be guaranteed to meet the size, alignment, and
13643 + * boundary requirements specified.
13644 + *
13645 + * @param[in] size Specifies the size of the buffers that will be allocated from
13646 + * this pool.
13647 + * @param[in] align Specifies the byte alignment requirements of the buffers
13648 + * allocated from this pool. Must be a power of 2.
13649 + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
13650 + * this pool must not cross.
13651 + *
13652 + * @returns A pointer to an internal opaque structure which is not to be
13653 + * accessed outside of these library functions. Use this handle to specify
13654 + * which pools to allocate/free DMA buffers from and also to destroy the pool,
13655 + * when you are done with it.
13656 + */
13657 +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
13658 +
13659 +/**
13660 + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
13661 + */
13662 +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
13663 +
13664 +/**
13665 + * Allocate a buffer from the specified DMA pool and zeros its contents.
13666 + */
13667 +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
13668 +
13669 +/**
13670 + * Free a previously allocated buffer from the DMA pool.
13671 + */
13672 +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
13673 +#endif
13674 +
13675 +/** Allocates a DMA capable buffer and zeroes its contents. */
13676 +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
13677 +
13678 +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
13679 +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
13680 +
13681 +/** Frees a previously allocated buffer. */
13682 +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
13683 +
13684 +/** Allocates a block of memory and zeroes its contents. */
13685 +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
13686 +
13687 +/** Allocates a block of memory and zeroes its contents, in an atomic manner
13688 + * which can be used inside interrupt context. The size should be sufficiently
13689 + * small, a few KB at most, such that failures are not likely to occur. Can just call
13690 + * __DWC_ALLOC if it is atomic. */
13691 +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
13692 +
13693 +/** Frees a previously allocated buffer. */
13694 +extern void __DWC_FREE(void *mem_ctx, void *addr);
13695 +
13696 +#ifndef DWC_DEBUG_MEMORY
13697 +
13698 +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
13699 +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
13700 +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
13701 +
13702 +# ifdef DWC_LINUX
13703 +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) __DWC_DMA_ALLOC(_dev, _size_, _dma_)
13704 +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) __DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_)
13705 +#define DWC_DMA_FREE(_dev, _size_,_virt_, _dma_) __DWC_DMA_FREE(_dev, _size_, _virt_, _dma_)
13706 +# endif
13707 +
13708 +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13709 +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
13710 +#define DWC_DMA_FREE __DWC_DMA_FREE
13711 +# endif
13712 +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
13713 +
13714 +#else /* DWC_DEBUG_MEMORY */
13715 +
13716 +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
13717 +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
13718 +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
13719 +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
13720 + char const *func, int line);
13721 +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
13722 + char const *func, int line);
13723 +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
13724 + dwc_dma_t dma_addr, char const *func, int line);
13725 +
13726 +extern int dwc_memory_debug_start(void *mem_ctx);
13727 +extern void dwc_memory_debug_stop(void);
13728 +extern void dwc_memory_debug_report(void);
13729 +
13730 +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
13731 +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
13732 + __func__, __LINE__)
13733 +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
13734 +
13735 +# ifdef DWC_LINUX
13736 +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) \
13737 + dwc_dma_alloc_debug(_dev, _size_, _dma_, __func__, __LINE__)
13738 +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) \
13739 + dwc_dma_alloc_atomic_debug(_dev, _size_, _dma_, __func__, __LINE__)
13740 +#define DWC_DMA_FREE(_dev, _size_, _virt_, _dma_) \
13741 + dwc_dma_free_debug(_dev, _size_, _virt_, _dma_, __func__, __LINE__)
13742 +# endif
13743 +
13744 +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13745 +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
13746 + _dma_, __func__, __LINE__)
13747 +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
13748 + _virt_, _dma_, __func__, __LINE__)
13749 +# endif
13750 +
13751 +#endif /* DWC_DEBUG_MEMORY */
13752 +
13753 +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
13754 +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
13755 +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
13756 +
13757 +#ifdef DWC_LINUX
13758 +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
13759 + * just throw away the DMA context parameter.
13760 + */
13761 +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
13762 +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
13763 +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
13764 +#endif
13765 +
13766 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13767 +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
13768 + * them in using the DMA context parameter.
13769 + */
13770 +#define dwc_dma_alloc DWC_DMA_ALLOC
13771 +#define dwc_dma_free DWC_DMA_FREE
13772 +#endif
13773 +
13774 +
13775 +/** @name Memory and String Processing */
13776 +
13777 +/** memset() clone */
13778 +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
13779 +#define dwc_memset DWC_MEMSET
13780 +
13781 +/** memcpy() clone */
13782 +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
13783 +#define dwc_memcpy DWC_MEMCPY
13784 +
13785 +/** memmove() clone */
13786 +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
13787 +#define dwc_memmove DWC_MEMMOVE
13788 +
13789 +/** memcmp() clone */
13790 +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
13791 +#define dwc_memcmp DWC_MEMCMP
13792 +
13793 +/** strcmp() clone */
13794 +extern int DWC_STRCMP(void *s1, void *s2);
13795 +#define dwc_strcmp DWC_STRCMP
13796 +
13797 +/** strncmp() clone */
13798 +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
13799 +#define dwc_strncmp DWC_STRNCMP
13800 +
13801 +/** strlen() clone, for NULL terminated ASCII strings */
13802 +extern int DWC_STRLEN(char const *str);
13803 +#define dwc_strlen DWC_STRLEN
13804 +
13805 +/** strcpy() clone, for NULL terminated ASCII strings */
13806 +extern char *DWC_STRCPY(char *to, const char *from);
13807 +#define dwc_strcpy DWC_STRCPY
13808 +
13809 +/** strdup() clone. If you wish to use memory allocation debugging, this
13810 + * implementation of strdup should use the DWC_* memory routines instead of
13811 + * calling a predefined strdup. Otherwise the memory allocated by this routine
13812 + * will not be seen by the debugging routines. */
13813 +extern char *DWC_STRDUP(char const *str);
13814 +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
13815 +
13816 +/** NOT an atoi() clone. Read the description carefully. Returns an integer
13817 + * converted from the string str in base 10 unless the string begins with a "0x"
13818 + * in which case it is base 16. String must be a NULL terminated sequence of
13819 + * ASCII characters and may optionally begin with whitespace, a + or -, and a
13820 + * "0x" prefix if base 16. The remaining characters must be valid digits for
13821 + * the number and end with a NULL character. If any invalid characters are
13822 + * encountered or it returns with a negative error code and the results of the
13823 + * conversion are undefined. On sucess it returns 0. Overflow conditions are
13824 + * undefined. An example implementation using atoi() can be referenced from the
13825 + * Linux implementation. */
13826 +extern int DWC_ATOI(const char *str, int32_t *value);
13827 +#define dwc_atoi DWC_ATOI
13828 +
13829 +/** Same as above but for unsigned. */
13830 +extern int DWC_ATOUI(const char *str, uint32_t *value);
13831 +#define dwc_atoui DWC_ATOUI
13832 +
13833 +#ifdef DWC_UTFLIB
13834 +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
13835 +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
13836 +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
13837 +#endif
13838 +
13839 +
13840 +/** @name Wait queues
13841 + *
13842 + * Wait queues provide a means of synchronizing between threads or processes. A
13843 + * process can block on a waitq if some condition is not true, waiting for it to
13844 + * become true. When the waitq is triggered all waiting process will get
13845 + * unblocked and the condition will be check again. Waitqs should be triggered
13846 + * every time a condition can potentially change.*/
13847 +struct dwc_waitq;
13848 +
13849 +/** Type for a waitq */
13850 +typedef struct dwc_waitq dwc_waitq_t;
13851 +
13852 +/** The type of waitq condition callback function. This is called every time
13853 + * condition is evaluated. */
13854 +typedef int (*dwc_waitq_condition_t)(void *data);
13855 +
13856 +/** Allocate a waitq */
13857 +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
13858 +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
13859 +
13860 +/** Free a waitq */
13861 +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
13862 +#define dwc_waitq_free DWC_WAITQ_FREE
13863 +
13864 +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
13865 + * condition again. The function returns when the condition becomes true. The return value
13866 + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
13867 +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
13868 +#define dwc_waitq_wait DWC_WAITQ_WAIT
13869 +
13870 +/** Check the condition and if it is false, block on the waitq. When unblocked,
13871 + * check the condition again. The function returns when the condition become
13872 + * true or the timeout has passed. The return value is 0 on condition true or
13873 + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
13874 + * error. */
13875 +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
13876 + void *data, int32_t msecs);
13877 +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
13878 +
13879 +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
13880 + * has potentially changed. */
13881 +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
13882 +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
13883 +
13884 +/** Unblock all processes waiting on the waitq with an ABORTED result. */
13885 +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
13886 +#define dwc_waitq_abort DWC_WAITQ_ABORT
13887 +
13888 +
13889 +/** @name Threads
13890 + *
13891 + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
13892 + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
13893 + * returns the value from the thread.
13894 + */
13895 +
13896 +struct dwc_thread;
13897 +
13898 +/** Type for a thread */
13899 +typedef struct dwc_thread dwc_thread_t;
13900 +
13901 +/** The thread function */
13902 +typedef int (*dwc_thread_function_t)(void *data);
13903 +
13904 +/** Create a thread and start it running the thread_function. Returns a handle
13905 + * to the thread */
13906 +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
13907 +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
13908 +
13909 +/** Stops a thread. Return the value returned by the thread. Or will return
13910 + * DWC_ABORT if the thread never started. */
13911 +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
13912 +#define dwc_thread_stop DWC_THREAD_STOP
13913 +
13914 +/** Signifies to the thread that it must stop. */
13915 +#ifdef DWC_LINUX
13916 +/* Linux doesn't need any parameters for kthread_should_stop() */
13917 +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
13918 +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
13919 +
13920 +/* No thread_exit function in Linux */
13921 +#define dwc_thread_exit(_thrd_)
13922 +#endif
13923 +
13924 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13925 +/** BSD needs the thread pointer for kthread_suspend_check() */
13926 +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
13927 +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
13928 +
13929 +/** The thread must call this to exit. */
13930 +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
13931 +#define dwc_thread_exit DWC_THREAD_EXIT
13932 +#endif
13933 +
13934 +
13935 +/** @name Work queues
13936 + *
13937 + * Workqs are used to queue a callback function to be called at some later time,
13938 + * in another thread. */
13939 +struct dwc_workq;
13940 +
13941 +/** Type for a workq */
13942 +typedef struct dwc_workq dwc_workq_t;
13943 +
13944 +/** The type of the callback function to be called. */
13945 +typedef void (*dwc_work_callback_t)(void *data);
13946 +
13947 +/** Allocate a workq */
13948 +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
13949 +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
13950 +
13951 +/** Free a workq. All work must be completed before being freed. */
13952 +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
13953 +#define dwc_workq_free DWC_WORKQ_FREE
13954 +
13955 +/** Schedule a callback on the workq, passing in data. The function will be
13956 + * scheduled at some later time. */
13957 +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
13958 + void *data, char *format, ...)
13959 +#ifdef __GNUC__
13960 + __attribute__ ((format(printf, 4, 5)));
13961 +#else
13962 + ;
13963 +#endif
13964 +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
13965 +
13966 +/** Schedule a callback on the workq, that will be called until at least
13967 + * given number miliseconds have passed. */
13968 +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
13969 + void *data, uint32_t time, char *format, ...)
13970 +#ifdef __GNUC__
13971 + __attribute__ ((format(printf, 5, 6)));
13972 +#else
13973 + ;
13974 +#endif
13975 +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
13976 +
13977 +/** The number of processes in the workq */
13978 +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
13979 +#define dwc_workq_pending DWC_WORKQ_PENDING
13980 +
13981 +/** Blocks until all the work in the workq is complete or timed out. Returns <
13982 + * 0 on timeout. */
13983 +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
13984 +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
13985 +
13986 +
13987 +/** @name Tasklets
13988 + *
13989 + */
13990 +struct dwc_tasklet;
13991 +
13992 +/** Type for a tasklet */
13993 +typedef struct dwc_tasklet dwc_tasklet_t;
13994 +
13995 +/** The type of the callback function to be called */
13996 +typedef void (*dwc_tasklet_callback_t)(void *data);
13997 +
13998 +/** Allocates a tasklet */
13999 +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
14000 +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
14001 +
14002 +/** Frees a tasklet */
14003 +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
14004 +#define dwc_task_free DWC_TASK_FREE
14005 +
14006 +/** Schedules a tasklet to run */
14007 +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
14008 +#define dwc_task_schedule DWC_TASK_SCHEDULE
14009 +
14010 +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
14011 +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
14012 +
14013 +/** @name Timer
14014 + *
14015 + * Callbacks must be small and atomic.
14016 + */
14017 +struct dwc_timer;
14018 +
14019 +/** Type for a timer */
14020 +typedef struct dwc_timer dwc_timer_t;
14021 +
14022 +/** The type of the callback function to be called */
14023 +typedef void (*dwc_timer_callback_t)(void *data);
14024 +
14025 +/** Allocates a timer */
14026 +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
14027 +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
14028 +
14029 +/** Frees a timer */
14030 +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
14031 +#define dwc_timer_free DWC_TIMER_FREE
14032 +
14033 +/** Schedules the timer to run at time ms from now. And will repeat at every
14034 + * repeat_interval msec therafter
14035 + *
14036 + * Modifies a timer that is still awaiting execution to a new expiration time.
14037 + * The mod_time is added to the old time. */
14038 +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
14039 +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
14040 +
14041 +/** Disables the timer from execution. */
14042 +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
14043 +#define dwc_timer_cancel DWC_TIMER_CANCEL
14044 +
14045 +
14046 +/** @name Spinlocks
14047 + *
14048 + * These locks are used when the work between the lock/unlock is atomic and
14049 + * short. Interrupts are also disabled during the lock/unlock and thus they are
14050 + * suitable to lock between interrupt/non-interrupt context. They also lock
14051 + * between processes if you have multiple CPUs or Preemption. If you don't have
14052 + * multiple CPUS or Preemption, then the you can simply implement the
14053 + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
14054 + * the work between the lock/unlock is atomic, the process context will never
14055 + * change, and so you never have to lock between processes. */
14056 +
14057 +struct dwc_spinlock;
14058 +
14059 +/** Type for a spinlock */
14060 +typedef struct dwc_spinlock dwc_spinlock_t;
14061 +
14062 +/** Type for the 'flags' argument to spinlock funtions */
14063 +typedef unsigned long dwc_irqflags_t;
14064 +
14065 +/** Returns an initialized lock variable. This function should allocate and
14066 + * initialize the OS-specific data structure used for locking. This data
14067 + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
14068 + * be freed by the DWC_FREE_LOCK when it is no longer used.
14069 + *
14070 + * For Linux Spinlock Debugging make it macro because the debugging routines use
14071 + * the symbol name to determine recursive locking. Using a wrapper function
14072 + * makes it falsely think recursive locking occurs. */
14073 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
14074 +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
14075 + lock = DWC_ALLOC(sizeof(spinlock_t)); \
14076 + if (lock) { \
14077 + spin_lock_init((spinlock_t *)lock); \
14078 + } \
14079 +})
14080 +#else
14081 +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
14082 +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
14083 +#endif
14084 +
14085 +/** Frees an initialized lock variable. */
14086 +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
14087 +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
14088 +
14089 +/** Disables interrupts and blocks until it acquires the lock.
14090 + *
14091 + * @param lock Pointer to the spinlock.
14092 + * @param flags Unsigned long for irq flags storage.
14093 + */
14094 +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
14095 +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
14096 +
14097 +/** Re-enables the interrupt and releases the lock.
14098 + *
14099 + * @param lock Pointer to the spinlock.
14100 + * @param flags Unsigned long for irq flags storage. Must be the same as was
14101 + * passed into DWC_LOCK.
14102 + */
14103 +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
14104 +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
14105 +
14106 +/** Blocks until it acquires the lock.
14107 + *
14108 + * @param lock Pointer to the spinlock.
14109 + */
14110 +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
14111 +#define dwc_spinlock DWC_SPINLOCK
14112 +
14113 +/** Releases the lock.
14114 + *
14115 + * @param lock Pointer to the spinlock.
14116 + */
14117 +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
14118 +#define dwc_spinunlock DWC_SPINUNLOCK
14119 +
14120 +
14121 +/** @name Mutexes
14122 + *
14123 + * Unlike spinlocks Mutexes lock only between processes and the work between the
14124 + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
14125 + */
14126 +
14127 +struct dwc_mutex;
14128 +
14129 +/** Type for a mutex */
14130 +typedef struct dwc_mutex dwc_mutex_t;
14131 +
14132 +/* For Linux Mutex Debugging make it inline because the debugging routines use
14133 + * the symbol to determine recursive locking. This makes it falsely think
14134 + * recursive locking occurs. */
14135 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
14136 +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
14137 + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
14138 + mutex_init((struct mutex *)__mutexp); \
14139 +})
14140 +#endif
14141 +
14142 +/** Allocate a mutex */
14143 +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
14144 +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
14145 +
14146 +/* For memory leak debugging when using Linux Mutex Debugging */
14147 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
14148 +#define DWC_MUTEX_FREE(__mutexp) do { \
14149 + mutex_destroy((struct mutex *)__mutexp); \
14150 + DWC_FREE(__mutexp); \
14151 +} while(0)
14152 +#else
14153 +/** Free a mutex */
14154 +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
14155 +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
14156 +#endif
14157 +
14158 +/** Lock a mutex */
14159 +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
14160 +#define dwc_mutex_lock DWC_MUTEX_LOCK
14161 +
14162 +/** Non-blocking lock returns 1 on successful lock. */
14163 +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
14164 +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
14165 +
14166 +/** Unlock a mutex */
14167 +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
14168 +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
14169 +
14170 +
14171 +/** @name Time */
14172 +
14173 +/** Microsecond delay.
14174 + *
14175 + * @param usecs Microseconds to delay.
14176 + */
14177 +extern void DWC_UDELAY(uint32_t usecs);
14178 +#define dwc_udelay DWC_UDELAY
14179 +
14180 +/** Millisecond delay.
14181 + *
14182 + * @param msecs Milliseconds to delay.
14183 + */
14184 +extern void DWC_MDELAY(uint32_t msecs);
14185 +#define dwc_mdelay DWC_MDELAY
14186 +
14187 +/** Non-busy waiting.
14188 + * Sleeps for specified number of milliseconds.
14189 + *
14190 + * @param msecs Milliseconds to sleep.
14191 + */
14192 +extern void DWC_MSLEEP(uint32_t msecs);
14193 +#define dwc_msleep DWC_MSLEEP
14194 +
14195 +/**
14196 + * Returns number of milliseconds since boot.
14197 + */
14198 +extern uint32_t DWC_TIME(void);
14199 +#define dwc_time DWC_TIME
14200 +
14201 +
14202 +
14203 +
14204 +/* @mainpage DWC Portability and Common Library
14205 + *
14206 + * This is the documentation for the DWC Portability and Common Library.
14207 + *
14208 + * @section intro Introduction
14209 + *
14210 + * The DWC Portability library consists of wrapper calls and data structures to
14211 + * all low-level functions which are typically provided by the OS. The WUDEV
14212 + * driver uses only these functions. In order to port the WUDEV driver, only
14213 + * the functions in this library need to be re-implemented, with the same
14214 + * behavior as documented here.
14215 + *
14216 + * The Common library consists of higher level functions, which rely only on
14217 + * calling the functions from the DWC Portability library. These common
14218 + * routines are shared across modules. Some of the common libraries need to be
14219 + * used directly by the driver programmer when porting WUDEV. Such as the
14220 + * parameter and notification libraries.
14221 + *
14222 + * @section low Portability Library OS Wrapper Functions
14223 + *
14224 + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
14225 + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
14226 + * these functions are included in the dwc_os.h file.
14227 + *
14228 + * There are many functions here covering a wide array of OS services. Please
14229 + * see dwc_os.h for details, and implementation notes for each function.
14230 + *
14231 + * @section common Common Library Functions
14232 + *
14233 + * Any function starting with dwc and in all lowercase is a common library
14234 + * routine. These functions have a portable implementation and do not need to
14235 + * be reimplemented when porting. The common routines can be used by any
14236 + * driver, and some must be used by the end user to control the drivers. For
14237 + * example, you must use the Parameter common library in order to set the
14238 + * parameters in the WUDEV module.
14239 + *
14240 + * The common libraries consist of the following:
14241 + *
14242 + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
14243 + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
14244 + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
14245 + * - Lists - Used internally and can be used by end-user. See dwc_list.h
14246 + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
14247 + * - Modpow - Used internally only. See dwc_modpow.h
14248 + * - DH - Used internally only. See dwc_dh.h
14249 + * - Crypto - Used internally only. See dwc_crypto.h
14250 + *
14251 + *
14252 + * @section prereq Prerequistes For dwc_os.h
14253 + * @subsection types Data Types
14254 + *
14255 + * The dwc_os.h file assumes that several low-level data types are pre defined for the
14256 + * compilation environment. These data types are:
14257 + *
14258 + * - uint8_t - unsigned 8-bit data type
14259 + * - int8_t - signed 8-bit data type
14260 + * - uint16_t - unsigned 16-bit data type
14261 + * - int16_t - signed 16-bit data type
14262 + * - uint32_t - unsigned 32-bit data type
14263 + * - int32_t - signed 32-bit data type
14264 + * - uint64_t - unsigned 64-bit data type
14265 + * - int64_t - signed 64-bit data type
14266 + *
14267 + * Ensure that these are defined before using dwc_os.h. The easiest way to do
14268 + * that is to modify the top of the file to include the appropriate header.
14269 + * This is already done for the Linux environment. If the DWC_LINUX macro is
14270 + * defined, the correct header will be added. A standard header <stdint.h> is
14271 + * also used for environments where standard C headers are available.
14272 + *
14273 + * @subsection stdarg Variable Arguments
14274 + *
14275 + * Variable arguments are provided by a standard C header <stdarg.h>. it is
14276 + * available in Both the Linux and ANSI C enviornment. An equivalent must be
14277 + * provided in your enviornment in order to use dwc_os.h with the debug and
14278 + * tracing message functionality.
14279 + *
14280 + * @subsection thread Threading
14281 + *
14282 + * WUDEV Core must be run on an operating system that provides for multiple
14283 + * threads/processes. Threading can be implemented in many ways, even in
14284 + * embedded systems without an operating system. At the bare minimum, the
14285 + * system should be able to start any number of processes at any time to handle
14286 + * special work. It need not be a pre-emptive system. Process context can
14287 + * change upon a call to a blocking function. The hardware interrupt context
14288 + * that calls the module's ISR() function must be differentiable from process
14289 + * context, even if your processes are impemented via a hardware interrupt.
14290 + * Further locking mechanism between process must exist (or be implemented), and
14291 + * process context must have a way to disable interrupts for a period of time to
14292 + * lock them out. If all of this exists, the functions in dwc_os.h related to
14293 + * threading should be able to be implemented with the defined behavior.
14294 + *
14295 + */
14296 +
14297 +#ifdef __cplusplus
14298 +}
14299 +#endif
14300 +
14301 +#endif /* _DWC_OS_H_ */
14302 --- /dev/null
14303 +++ b/drivers/usb/host/dwc_common_port/usb.h
14304 @@ -0,0 +1,946 @@
14305 +/*
14306 + * Copyright (c) 1998 The NetBSD Foundation, Inc.
14307 + * All rights reserved.
14308 + *
14309 + * This code is derived from software contributed to The NetBSD Foundation
14310 + * by Lennart Augustsson (lennart@augustsson.net) at
14311 + * Carlstedt Research & Technology.
14312 + *
14313 + * Redistribution and use in source and binary forms, with or without
14314 + * modification, are permitted provided that the following conditions
14315 + * are met:
14316 + * 1. Redistributions of source code must retain the above copyright
14317 + * notice, this list of conditions and the following disclaimer.
14318 + * 2. Redistributions in binary form must reproduce the above copyright
14319 + * notice, this list of conditions and the following disclaimer in the
14320 + * documentation and/or other materials provided with the distribution.
14321 + * 3. All advertising materials mentioning features or use of this software
14322 + * must display the following acknowledgement:
14323 + * This product includes software developed by the NetBSD
14324 + * Foundation, Inc. and its contributors.
14325 + * 4. Neither the name of The NetBSD Foundation nor the names of its
14326 + * contributors may be used to endorse or promote products derived
14327 + * from this software without specific prior written permission.
14328 + *
14329 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14330 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
14331 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14332 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
14333 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
14334 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
14335 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
14336 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
14337 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
14338 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
14339 + * POSSIBILITY OF SUCH DAMAGE.
14340 + */
14341 +
14342 +/* Modified by Synopsys, Inc, 12/12/2007 */
14343 +
14344 +
14345 +#ifndef _USB_H_
14346 +#define _USB_H_
14347 +
14348 +#ifdef __cplusplus
14349 +extern "C" {
14350 +#endif
14351 +
14352 +/*
14353 + * The USB records contain some unaligned little-endian word
14354 + * components. The U[SG]ETW macros take care of both the alignment
14355 + * and endian problem and should always be used to access non-byte
14356 + * values.
14357 + */
14358 +typedef u_int8_t uByte;
14359 +typedef u_int8_t uWord[2];
14360 +typedef u_int8_t uDWord[4];
14361 +
14362 +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
14363 +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
14364 +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
14365 + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
14366 +
14367 +#if 1
14368 +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
14369 +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
14370 +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
14371 +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
14372 + (w)[1] = (u_int8_t)((v) >> 8), \
14373 + (w)[2] = (u_int8_t)((v) >> 16), \
14374 + (w)[3] = (u_int8_t)((v) >> 24))
14375 +#else
14376 +/*
14377 + * On little-endian machines that can handle unanliged accesses
14378 + * (e.g. i386) these macros can be replaced by the following.
14379 + */
14380 +#define UGETW(w) (*(u_int16_t *)(w))
14381 +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
14382 +#define UGETDW(w) (*(u_int32_t *)(w))
14383 +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
14384 +#endif
14385 +
14386 +/*
14387 + * Macros for accessing UAS IU fields, which are big-endian
14388 + */
14389 +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
14390 +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
14391 +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
14392 + ((x) >> 8) & 0xff, (x) & 0xff }
14393 +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
14394 +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
14395 +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
14396 +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
14397 + (w)[1] = (u_int8_t)((v) >> 16), \
14398 + (w)[2] = (u_int8_t)((v) >> 8), \
14399 + (w)[3] = (u_int8_t)(v))
14400 +
14401 +#define UPACKED __attribute__((__packed__))
14402 +
14403 +typedef struct {
14404 + uByte bmRequestType;
14405 + uByte bRequest;
14406 + uWord wValue;
14407 + uWord wIndex;
14408 + uWord wLength;
14409 +} UPACKED usb_device_request_t;
14410 +
14411 +#define UT_GET_DIR(a) ((a) & 0x80)
14412 +#define UT_WRITE 0x00
14413 +#define UT_READ 0x80
14414 +
14415 +#define UT_GET_TYPE(a) ((a) & 0x60)
14416 +#define UT_STANDARD 0x00
14417 +#define UT_CLASS 0x20
14418 +#define UT_VENDOR 0x40
14419 +
14420 +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
14421 +#define UT_DEVICE 0x00
14422 +#define UT_INTERFACE 0x01
14423 +#define UT_ENDPOINT 0x02
14424 +#define UT_OTHER 0x03
14425 +
14426 +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
14427 +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
14428 +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
14429 +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
14430 +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
14431 +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
14432 +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
14433 +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
14434 +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
14435 +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
14436 +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
14437 +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
14438 +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
14439 +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
14440 +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
14441 +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
14442 +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
14443 +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
14444 +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
14445 +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
14446 +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
14447 +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
14448 +
14449 +/* Requests */
14450 +#define UR_GET_STATUS 0x00
14451 +#define USTAT_STANDARD_STATUS 0x00
14452 +#define WUSTAT_WUSB_FEATURE 0x01
14453 +#define WUSTAT_CHANNEL_INFO 0x02
14454 +#define WUSTAT_RECEIVED_DATA 0x03
14455 +#define WUSTAT_MAS_AVAILABILITY 0x04
14456 +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
14457 +#define UR_CLEAR_FEATURE 0x01
14458 +#define UR_SET_FEATURE 0x03
14459 +#define UR_SET_AND_TEST_FEATURE 0x0c
14460 +#define UR_SET_ADDRESS 0x05
14461 +#define UR_GET_DESCRIPTOR 0x06
14462 +#define UDESC_DEVICE 0x01
14463 +#define UDESC_CONFIG 0x02
14464 +#define UDESC_STRING 0x03
14465 +#define UDESC_INTERFACE 0x04
14466 +#define UDESC_ENDPOINT 0x05
14467 +#define UDESC_SS_USB_COMPANION 0x30
14468 +#define UDESC_DEVICE_QUALIFIER 0x06
14469 +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
14470 +#define UDESC_INTERFACE_POWER 0x08
14471 +#define UDESC_OTG 0x09
14472 +#define WUDESC_SECURITY 0x0c
14473 +#define WUDESC_KEY 0x0d
14474 +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
14475 +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
14476 +#define WUD_KEY_TYPE_ASSOC 0x01
14477 +#define WUD_KEY_TYPE_GTK 0x02
14478 +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
14479 +#define WUD_KEY_ORIGIN_HOST 0x00
14480 +#define WUD_KEY_ORIGIN_DEVICE 0x01
14481 +#define WUDESC_ENCRYPTION_TYPE 0x0e
14482 +#define WUDESC_BOS 0x0f
14483 +#define WUDESC_DEVICE_CAPABILITY 0x10
14484 +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
14485 +#define UDESC_BOS 0x0f
14486 +#define UDESC_DEVICE_CAPABILITY 0x10
14487 +#define UDESC_CS_DEVICE 0x21 /* class specific */
14488 +#define UDESC_CS_CONFIG 0x22
14489 +#define UDESC_CS_STRING 0x23
14490 +#define UDESC_CS_INTERFACE 0x24
14491 +#define UDESC_CS_ENDPOINT 0x25
14492 +#define UDESC_HUB 0x29
14493 +#define UR_SET_DESCRIPTOR 0x07
14494 +#define UR_GET_CONFIG 0x08
14495 +#define UR_SET_CONFIG 0x09
14496 +#define UR_GET_INTERFACE 0x0a
14497 +#define UR_SET_INTERFACE 0x0b
14498 +#define UR_SYNCH_FRAME 0x0c
14499 +#define WUR_SET_ENCRYPTION 0x0d
14500 +#define WUR_GET_ENCRYPTION 0x0e
14501 +#define WUR_SET_HANDSHAKE 0x0f
14502 +#define WUR_GET_HANDSHAKE 0x10
14503 +#define WUR_SET_CONNECTION 0x11
14504 +#define WUR_SET_SECURITY_DATA 0x12
14505 +#define WUR_GET_SECURITY_DATA 0x13
14506 +#define WUR_SET_WUSB_DATA 0x14
14507 +#define WUDATA_DRPIE_INFO 0x01
14508 +#define WUDATA_TRANSMIT_DATA 0x02
14509 +#define WUDATA_TRANSMIT_PARAMS 0x03
14510 +#define WUDATA_RECEIVE_PARAMS 0x04
14511 +#define WUDATA_TRANSMIT_POWER 0x05
14512 +#define WUR_LOOPBACK_DATA_WRITE 0x15
14513 +#define WUR_LOOPBACK_DATA_READ 0x16
14514 +#define WUR_SET_INTERFACE_DS 0x17
14515 +
14516 +/* Feature numbers */
14517 +#define UF_ENDPOINT_HALT 0
14518 +#define UF_DEVICE_REMOTE_WAKEUP 1
14519 +#define UF_TEST_MODE 2
14520 +#define UF_DEVICE_B_HNP_ENABLE 3
14521 +#define UF_DEVICE_A_HNP_SUPPORT 4
14522 +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
14523 +#define WUF_WUSB 3
14524 +#define WUF_TX_DRPIE 0x0
14525 +#define WUF_DEV_XMIT_PACKET 0x1
14526 +#define WUF_COUNT_PACKETS 0x2
14527 +#define WUF_CAPTURE_PACKETS 0x3
14528 +#define UF_FUNCTION_SUSPEND 0
14529 +#define UF_U1_ENABLE 48
14530 +#define UF_U2_ENABLE 49
14531 +#define UF_LTM_ENABLE 50
14532 +
14533 +/* Class requests from the USB 2.0 hub spec, table 11-15 */
14534 +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
14535 +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
14536 +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
14537 +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
14538 +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
14539 +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
14540 +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
14541 +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
14542 +
14543 +#ifdef _MSC_VER
14544 +#include <pshpack1.h>
14545 +#endif
14546 +
14547 +typedef struct {
14548 + uByte bLength;
14549 + uByte bDescriptorType;
14550 + uByte bDescriptorSubtype;
14551 +} UPACKED usb_descriptor_t;
14552 +
14553 +typedef struct {
14554 + uByte bLength;
14555 + uByte bDescriptorType;
14556 +} UPACKED usb_descriptor_header_t;
14557 +
14558 +typedef struct {
14559 + uByte bLength;
14560 + uByte bDescriptorType;
14561 + uWord bcdUSB;
14562 +#define UD_USB_2_0 0x0200
14563 +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
14564 + uByte bDeviceClass;
14565 + uByte bDeviceSubClass;
14566 + uByte bDeviceProtocol;
14567 + uByte bMaxPacketSize;
14568 + /* The fields below are not part of the initial descriptor. */
14569 + uWord idVendor;
14570 + uWord idProduct;
14571 + uWord bcdDevice;
14572 + uByte iManufacturer;
14573 + uByte iProduct;
14574 + uByte iSerialNumber;
14575 + uByte bNumConfigurations;
14576 +} UPACKED usb_device_descriptor_t;
14577 +#define USB_DEVICE_DESCRIPTOR_SIZE 18
14578 +
14579 +typedef struct {
14580 + uByte bLength;
14581 + uByte bDescriptorType;
14582 + uWord wTotalLength;
14583 + uByte bNumInterface;
14584 + uByte bConfigurationValue;
14585 + uByte iConfiguration;
14586 +#define UC_ATT_ONE (1 << 7) /* must be set */
14587 +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
14588 +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
14589 +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
14590 + uByte bmAttributes;
14591 +#define UC_BUS_POWERED 0x80
14592 +#define UC_SELF_POWERED 0x40
14593 +#define UC_REMOTE_WAKEUP 0x20
14594 + uByte bMaxPower; /* max current in 2 mA units */
14595 +#define UC_POWER_FACTOR 2
14596 +} UPACKED usb_config_descriptor_t;
14597 +#define USB_CONFIG_DESCRIPTOR_SIZE 9
14598 +
14599 +typedef struct {
14600 + uByte bLength;
14601 + uByte bDescriptorType;
14602 + uByte bInterfaceNumber;
14603 + uByte bAlternateSetting;
14604 + uByte bNumEndpoints;
14605 + uByte bInterfaceClass;
14606 + uByte bInterfaceSubClass;
14607 + uByte bInterfaceProtocol;
14608 + uByte iInterface;
14609 +} UPACKED usb_interface_descriptor_t;
14610 +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
14611 +
14612 +typedef struct {
14613 + uByte bLength;
14614 + uByte bDescriptorType;
14615 + uByte bEndpointAddress;
14616 +#define UE_GET_DIR(a) ((a) & 0x80)
14617 +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
14618 +#define UE_DIR_IN 0x80
14619 +#define UE_DIR_OUT 0x00
14620 +#define UE_ADDR 0x0f
14621 +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
14622 + uByte bmAttributes;
14623 +#define UE_XFERTYPE 0x03
14624 +#define UE_CONTROL 0x00
14625 +#define UE_ISOCHRONOUS 0x01
14626 +#define UE_BULK 0x02
14627 +#define UE_INTERRUPT 0x03
14628 +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
14629 +#define UE_ISO_TYPE 0x0c
14630 +#define UE_ISO_ASYNC 0x04
14631 +#define UE_ISO_ADAPT 0x08
14632 +#define UE_ISO_SYNC 0x0c
14633 +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
14634 + uWord wMaxPacketSize;
14635 + uByte bInterval;
14636 +} UPACKED usb_endpoint_descriptor_t;
14637 +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
14638 +
14639 +typedef struct ss_endpoint_companion_descriptor {
14640 + uByte bLength;
14641 + uByte bDescriptorType;
14642 + uByte bMaxBurst;
14643 +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
14644 +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
14645 +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
14646 +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
14647 + uByte bmAttributes;
14648 + uWord wBytesPerInterval;
14649 +} UPACKED ss_endpoint_companion_descriptor_t;
14650 +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
14651 +
14652 +typedef struct {
14653 + uByte bLength;
14654 + uByte bDescriptorType;
14655 + uWord bString[127];
14656 +} UPACKED usb_string_descriptor_t;
14657 +#define USB_MAX_STRING_LEN 128
14658 +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
14659 +
14660 +/* Hub specific request */
14661 +#define UR_GET_BUS_STATE 0x02
14662 +#define UR_CLEAR_TT_BUFFER 0x08
14663 +#define UR_RESET_TT 0x09
14664 +#define UR_GET_TT_STATE 0x0a
14665 +#define UR_STOP_TT 0x0b
14666 +
14667 +/* Hub features */
14668 +#define UHF_C_HUB_LOCAL_POWER 0
14669 +#define UHF_C_HUB_OVER_CURRENT 1
14670 +#define UHF_PORT_CONNECTION 0
14671 +#define UHF_PORT_ENABLE 1
14672 +#define UHF_PORT_SUSPEND 2
14673 +#define UHF_PORT_OVER_CURRENT 3
14674 +#define UHF_PORT_RESET 4
14675 +#define UHF_PORT_L1 5
14676 +#define UHF_PORT_POWER 8
14677 +#define UHF_PORT_LOW_SPEED 9
14678 +#define UHF_PORT_HIGH_SPEED 10
14679 +#define UHF_C_PORT_CONNECTION 16
14680 +#define UHF_C_PORT_ENABLE 17
14681 +#define UHF_C_PORT_SUSPEND 18
14682 +#define UHF_C_PORT_OVER_CURRENT 19
14683 +#define UHF_C_PORT_RESET 20
14684 +#define UHF_C_PORT_L1 23
14685 +#define UHF_PORT_TEST 21
14686 +#define UHF_PORT_INDICATOR 22
14687 +
14688 +typedef struct {
14689 + uByte bDescLength;
14690 + uByte bDescriptorType;
14691 + uByte bNbrPorts;
14692 + uWord wHubCharacteristics;
14693 +#define UHD_PWR 0x0003
14694 +#define UHD_PWR_GANGED 0x0000
14695 +#define UHD_PWR_INDIVIDUAL 0x0001
14696 +#define UHD_PWR_NO_SWITCH 0x0002
14697 +#define UHD_COMPOUND 0x0004
14698 +#define UHD_OC 0x0018
14699 +#define UHD_OC_GLOBAL 0x0000
14700 +#define UHD_OC_INDIVIDUAL 0x0008
14701 +#define UHD_OC_NONE 0x0010
14702 +#define UHD_TT_THINK 0x0060
14703 +#define UHD_TT_THINK_8 0x0000
14704 +#define UHD_TT_THINK_16 0x0020
14705 +#define UHD_TT_THINK_24 0x0040
14706 +#define UHD_TT_THINK_32 0x0060
14707 +#define UHD_PORT_IND 0x0080
14708 + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
14709 +#define UHD_PWRON_FACTOR 2
14710 + uByte bHubContrCurrent;
14711 + uByte DeviceRemovable[32]; /* max 255 ports */
14712 +#define UHD_NOT_REMOV(desc, i) \
14713 + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
14714 + /* deprecated */ uByte PortPowerCtrlMask[1];
14715 +} UPACKED usb_hub_descriptor_t;
14716 +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
14717 +
14718 +typedef struct {
14719 + uByte bLength;
14720 + uByte bDescriptorType;
14721 + uWord bcdUSB;
14722 + uByte bDeviceClass;
14723 + uByte bDeviceSubClass;
14724 + uByte bDeviceProtocol;
14725 + uByte bMaxPacketSize0;
14726 + uByte bNumConfigurations;
14727 + uByte bReserved;
14728 +} UPACKED usb_device_qualifier_t;
14729 +#define USB_DEVICE_QUALIFIER_SIZE 10
14730 +
14731 +typedef struct {
14732 + uByte bLength;
14733 + uByte bDescriptorType;
14734 + uByte bmAttributes;
14735 +#define UOTG_SRP 0x01
14736 +#define UOTG_HNP 0x02
14737 +} UPACKED usb_otg_descriptor_t;
14738 +
14739 +/* OTG feature selectors */
14740 +#define UOTG_B_HNP_ENABLE 3
14741 +#define UOTG_A_HNP_SUPPORT 4
14742 +#define UOTG_A_ALT_HNP_SUPPORT 5
14743 +
14744 +typedef struct {
14745 + uWord wStatus;
14746 +/* Device status flags */
14747 +#define UDS_SELF_POWERED 0x0001
14748 +#define UDS_REMOTE_WAKEUP 0x0002
14749 +/* Endpoint status flags */
14750 +#define UES_HALT 0x0001
14751 +} UPACKED usb_status_t;
14752 +
14753 +typedef struct {
14754 + uWord wHubStatus;
14755 +#define UHS_LOCAL_POWER 0x0001
14756 +#define UHS_OVER_CURRENT 0x0002
14757 + uWord wHubChange;
14758 +} UPACKED usb_hub_status_t;
14759 +
14760 +typedef struct {
14761 + uWord wPortStatus;
14762 +#define UPS_CURRENT_CONNECT_STATUS 0x0001
14763 +#define UPS_PORT_ENABLED 0x0002
14764 +#define UPS_SUSPEND 0x0004
14765 +#define UPS_OVERCURRENT_INDICATOR 0x0008
14766 +#define UPS_RESET 0x0010
14767 +#define UPS_PORT_POWER 0x0100
14768 +#define UPS_LOW_SPEED 0x0200
14769 +#define UPS_HIGH_SPEED 0x0400
14770 +#define UPS_PORT_TEST 0x0800
14771 +#define UPS_PORT_INDICATOR 0x1000
14772 + uWord wPortChange;
14773 +#define UPS_C_CONNECT_STATUS 0x0001
14774 +#define UPS_C_PORT_ENABLED 0x0002
14775 +#define UPS_C_SUSPEND 0x0004
14776 +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
14777 +#define UPS_C_PORT_RESET 0x0010
14778 +} UPACKED usb_port_status_t;
14779 +
14780 +#ifdef _MSC_VER
14781 +#include <poppack.h>
14782 +#endif
14783 +
14784 +/* Device class codes */
14785 +#define UDCLASS_IN_INTERFACE 0x00
14786 +#define UDCLASS_COMM 0x02
14787 +#define UDCLASS_HUB 0x09
14788 +#define UDSUBCLASS_HUB 0x00
14789 +#define UDPROTO_FSHUB 0x00
14790 +#define UDPROTO_HSHUBSTT 0x01
14791 +#define UDPROTO_HSHUBMTT 0x02
14792 +#define UDCLASS_DIAGNOSTIC 0xdc
14793 +#define UDCLASS_WIRELESS 0xe0
14794 +#define UDSUBCLASS_RF 0x01
14795 +#define UDPROTO_BLUETOOTH 0x01
14796 +#define UDCLASS_VENDOR 0xff
14797 +
14798 +/* Interface class codes */
14799 +#define UICLASS_UNSPEC 0x00
14800 +
14801 +#define UICLASS_AUDIO 0x01
14802 +#define UISUBCLASS_AUDIOCONTROL 1
14803 +#define UISUBCLASS_AUDIOSTREAM 2
14804 +#define UISUBCLASS_MIDISTREAM 3
14805 +
14806 +#define UICLASS_CDC 0x02 /* communication */
14807 +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
14808 +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
14809 +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
14810 +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
14811 +#define UISUBCLASS_CAPI_CONTROLMODEL 5
14812 +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
14813 +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
14814 +#define UIPROTO_CDC_AT 1
14815 +
14816 +#define UICLASS_HID 0x03
14817 +#define UISUBCLASS_BOOT 1
14818 +#define UIPROTO_BOOT_KEYBOARD 1
14819 +
14820 +#define UICLASS_PHYSICAL 0x05
14821 +
14822 +#define UICLASS_IMAGE 0x06
14823 +
14824 +#define UICLASS_PRINTER 0x07
14825 +#define UISUBCLASS_PRINTER 1
14826 +#define UIPROTO_PRINTER_UNI 1
14827 +#define UIPROTO_PRINTER_BI 2
14828 +#define UIPROTO_PRINTER_1284 3
14829 +
14830 +#define UICLASS_MASS 0x08
14831 +#define UISUBCLASS_RBC 1
14832 +#define UISUBCLASS_SFF8020I 2
14833 +#define UISUBCLASS_QIC157 3
14834 +#define UISUBCLASS_UFI 4
14835 +#define UISUBCLASS_SFF8070I 5
14836 +#define UISUBCLASS_SCSI 6
14837 +#define UIPROTO_MASS_CBI_I 0
14838 +#define UIPROTO_MASS_CBI 1
14839 +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
14840 +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
14841 +
14842 +#define UICLASS_HUB 0x09
14843 +#define UISUBCLASS_HUB 0
14844 +#define UIPROTO_FSHUB 0
14845 +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
14846 +#define UIPROTO_HSHUBMTT 1
14847 +
14848 +#define UICLASS_CDC_DATA 0x0a
14849 +#define UISUBCLASS_DATA 0
14850 +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
14851 +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
14852 +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
14853 +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
14854 +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
14855 +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
14856 +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
14857 +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
14858 +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
14859 +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
14860 +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
14861 +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
14862 +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
14863 +
14864 +#define UICLASS_SMARTCARD 0x0b
14865 +
14866 +/*#define UICLASS_FIRM_UPD 0x0c*/
14867 +
14868 +#define UICLASS_SECURITY 0x0d
14869 +
14870 +#define UICLASS_DIAGNOSTIC 0xdc
14871 +
14872 +#define UICLASS_WIRELESS 0xe0
14873 +#define UISUBCLASS_RF 0x01
14874 +#define UIPROTO_BLUETOOTH 0x01
14875 +
14876 +#define UICLASS_APPL_SPEC 0xfe
14877 +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
14878 +#define UISUBCLASS_IRDA 2
14879 +#define UIPROTO_IRDA 0
14880 +
14881 +#define UICLASS_VENDOR 0xff
14882 +
14883 +#define USB_HUB_MAX_DEPTH 5
14884 +
14885 +/*
14886 + * Minimum time a device needs to be powered down to go through
14887 + * a power cycle. XXX Are these time in the spec?
14888 + */
14889 +#define USB_POWER_DOWN_TIME 200 /* ms */
14890 +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
14891 +
14892 +#if 0
14893 +/* These are the values from the spec. */
14894 +#define USB_PORT_RESET_DELAY 10 /* ms */
14895 +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
14896 +#define USB_PORT_RESET_RECOVERY 10 /* ms */
14897 +#define USB_PORT_POWERUP_DELAY 100 /* ms */
14898 +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
14899 +#define USB_RESUME_DELAY (20*5) /* ms */
14900 +#define USB_RESUME_WAIT 10 /* ms */
14901 +#define USB_RESUME_RECOVERY 10 /* ms */
14902 +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
14903 +#else
14904 +/* Allow for marginal (i.e. non-conforming) devices. */
14905 +#define USB_PORT_RESET_DELAY 50 /* ms */
14906 +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
14907 +#define USB_PORT_RESET_RECOVERY 250 /* ms */
14908 +#define USB_PORT_POWERUP_DELAY 300 /* ms */
14909 +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
14910 +#define USB_RESUME_DELAY (50*5) /* ms */
14911 +#define USB_RESUME_WAIT 50 /* ms */
14912 +#define USB_RESUME_RECOVERY 50 /* ms */
14913 +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
14914 +#endif
14915 +
14916 +#define USB_MIN_POWER 100 /* mA */
14917 +#define USB_MAX_POWER 500 /* mA */
14918 +
14919 +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
14920 +
14921 +#define USB_UNCONFIG_NO 0
14922 +#define USB_UNCONFIG_INDEX (-1)
14923 +
14924 +/*** ioctl() related stuff ***/
14925 +
14926 +struct usb_ctl_request {
14927 + int ucr_addr;
14928 + usb_device_request_t ucr_request;
14929 + void *ucr_data;
14930 + int ucr_flags;
14931 +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
14932 + int ucr_actlen; /* actual length transferred */
14933 +};
14934 +
14935 +struct usb_alt_interface {
14936 + int uai_config_index;
14937 + int uai_interface_index;
14938 + int uai_alt_no;
14939 +};
14940 +
14941 +#define USB_CURRENT_CONFIG_INDEX (-1)
14942 +#define USB_CURRENT_ALT_INDEX (-1)
14943 +
14944 +struct usb_config_desc {
14945 + int ucd_config_index;
14946 + usb_config_descriptor_t ucd_desc;
14947 +};
14948 +
14949 +struct usb_interface_desc {
14950 + int uid_config_index;
14951 + int uid_interface_index;
14952 + int uid_alt_index;
14953 + usb_interface_descriptor_t uid_desc;
14954 +};
14955 +
14956 +struct usb_endpoint_desc {
14957 + int ued_config_index;
14958 + int ued_interface_index;
14959 + int ued_alt_index;
14960 + int ued_endpoint_index;
14961 + usb_endpoint_descriptor_t ued_desc;
14962 +};
14963 +
14964 +struct usb_full_desc {
14965 + int ufd_config_index;
14966 + u_int ufd_size;
14967 + u_char *ufd_data;
14968 +};
14969 +
14970 +struct usb_string_desc {
14971 + int usd_string_index;
14972 + int usd_language_id;
14973 + usb_string_descriptor_t usd_desc;
14974 +};
14975 +
14976 +struct usb_ctl_report_desc {
14977 + int ucrd_size;
14978 + u_char ucrd_data[1024]; /* filled data size will vary */
14979 +};
14980 +
14981 +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
14982 +
14983 +#define USB_MAX_DEVNAMES 4
14984 +#define USB_MAX_DEVNAMELEN 16
14985 +struct usb_device_info {
14986 + u_int8_t udi_bus;
14987 + u_int8_t udi_addr; /* device address */
14988 + usb_event_cookie_t udi_cookie;
14989 + char udi_product[USB_MAX_STRING_LEN];
14990 + char udi_vendor[USB_MAX_STRING_LEN];
14991 + char udi_release[8];
14992 + u_int16_t udi_productNo;
14993 + u_int16_t udi_vendorNo;
14994 + u_int16_t udi_releaseNo;
14995 + u_int8_t udi_class;
14996 + u_int8_t udi_subclass;
14997 + u_int8_t udi_protocol;
14998 + u_int8_t udi_config;
14999 + u_int8_t udi_speed;
15000 +#define USB_SPEED_UNKNOWN 0
15001 +#define USB_SPEED_LOW 1
15002 +#define USB_SPEED_FULL 2
15003 +#define USB_SPEED_HIGH 3
15004 +#define USB_SPEED_VARIABLE 4
15005 +#define USB_SPEED_SUPER 5
15006 + int udi_power; /* power consumption in mA, 0 if selfpowered */
15007 + int udi_nports;
15008 + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
15009 + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
15010 +#define USB_PORT_ENABLED 0xff
15011 +#define USB_PORT_SUSPENDED 0xfe
15012 +#define USB_PORT_POWERED 0xfd
15013 +#define USB_PORT_DISABLED 0xfc
15014 +};
15015 +
15016 +struct usb_ctl_report {
15017 + int ucr_report;
15018 + u_char ucr_data[1024]; /* filled data size will vary */
15019 +};
15020 +
15021 +struct usb_device_stats {
15022 + u_long uds_requests[4]; /* indexed by transfer type UE_* */
15023 +};
15024 +
15025 +#define WUSB_MIN_IE 0x80
15026 +#define WUSB_WCTA_IE 0x80
15027 +#define WUSB_WCONNECTACK_IE 0x81
15028 +#define WUSB_WHOSTINFO_IE 0x82
15029 +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
15030 +#define WUHI_CA_RECONN 0x00
15031 +#define WUHI_CA_LIMITED 0x01
15032 +#define WUHI_CA_ALL 0x03
15033 +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
15034 +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
15035 +#define WUSB_WDEV_DISCONNECT_IE 0x84
15036 +#define WUSB_WHOST_DISCONNECT_IE 0x85
15037 +#define WUSB_WRELEASE_CHANNEL_IE 0x86
15038 +#define WUSB_WWORK_IE 0x87
15039 +#define WUSB_WCHANNEL_STOP_IE 0x88
15040 +#define WUSB_WDEV_KEEPALIVE_IE 0x89
15041 +#define WUSB_WISOCH_DISCARD_IE 0x8A
15042 +#define WUSB_WRESETDEVICE_IE 0x8B
15043 +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
15044 +#define WUSB_MAX_IE 0x8C
15045 +
15046 +/* Device Notification Types */
15047 +
15048 +#define WUSB_DN_MIN 0x01
15049 +#define WUSB_DN_CONNECT 0x01
15050 +# define WUSB_DA_OLDCONN 0x00
15051 +# define WUSB_DA_NEWCONN 0x01
15052 +# define WUSB_DA_SELF_BEACON 0x02
15053 +# define WUSB_DA_DIR_BEACON 0x04
15054 +# define WUSB_DA_NO_BEACON 0x06
15055 +#define WUSB_DN_DISCONNECT 0x02
15056 +#define WUSB_DN_EPRDY 0x03
15057 +#define WUSB_DN_MASAVAILCHANGED 0x04
15058 +#define WUSB_DN_REMOTEWAKEUP 0x05
15059 +#define WUSB_DN_SLEEP 0x06
15060 +#define WUSB_DN_ALIVE 0x07
15061 +#define WUSB_DN_MAX 0x07
15062 +
15063 +#ifdef _MSC_VER
15064 +#include <pshpack1.h>
15065 +#endif
15066 +
15067 +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
15068 +typedef struct wusb_hndshk_data {
15069 + uByte bMessageNumber;
15070 + uByte bStatus;
15071 + uByte tTKID[3];
15072 + uByte bReserved;
15073 + uByte CDID[16];
15074 + uByte Nonce[16];
15075 + uByte MIC[8];
15076 +} UPACKED wusb_hndshk_data_t;
15077 +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
15078 +
15079 +/* WUSB Connection Context */
15080 +typedef struct wusb_conn_context {
15081 + uByte CHID [16];
15082 + uByte CDID [16];
15083 + uByte CK [16];
15084 +} UPACKED wusb_conn_context_t;
15085 +
15086 +/* WUSB Security Descriptor */
15087 +typedef struct wusb_security_desc {
15088 + uByte bLength;
15089 + uByte bDescriptorType;
15090 + uWord wTotalLength;
15091 + uByte bNumEncryptionTypes;
15092 +} UPACKED wusb_security_desc_t;
15093 +
15094 +/* WUSB Encryption Type Descriptor */
15095 +typedef struct wusb_encrypt_type_desc {
15096 + uByte bLength;
15097 + uByte bDescriptorType;
15098 +
15099 + uByte bEncryptionType;
15100 +#define WUETD_UNSECURE 0
15101 +#define WUETD_WIRED 1
15102 +#define WUETD_CCM_1 2
15103 +#define WUETD_RSA_1 3
15104 +
15105 + uByte bEncryptionValue;
15106 + uByte bAuthKeyIndex;
15107 +} UPACKED wusb_encrypt_type_desc_t;
15108 +
15109 +/* WUSB Key Descriptor */
15110 +typedef struct wusb_key_desc {
15111 + uByte bLength;
15112 + uByte bDescriptorType;
15113 + uByte tTKID[3];
15114 + uByte bReserved;
15115 + uByte KeyData[1]; /* variable length */
15116 +} UPACKED wusb_key_desc_t;
15117 +
15118 +/* WUSB BOS Descriptor (Binary device Object Store) */
15119 +typedef struct wusb_bos_desc {
15120 + uByte bLength;
15121 + uByte bDescriptorType;
15122 + uWord wTotalLength;
15123 + uByte bNumDeviceCaps;
15124 +} UPACKED wusb_bos_desc_t;
15125 +
15126 +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
15127 +typedef struct usb_dev_cap_20_ext_desc {
15128 + uByte bLength;
15129 + uByte bDescriptorType;
15130 + uByte bDevCapabilityType;
15131 +#define USB_20_EXT_LPM 0x02
15132 + uDWord bmAttributes;
15133 +} UPACKED usb_dev_cap_20_ext_desc_t;
15134 +
15135 +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
15136 +typedef struct usb_dev_cap_ss_usb {
15137 + uByte bLength;
15138 + uByte bDescriptorType;
15139 + uByte bDevCapabilityType;
15140 +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
15141 + uByte bmAttributes;
15142 +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
15143 +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
15144 +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
15145 +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
15146 + uWord wSpeedsSupported;
15147 + uByte bFunctionalitySupport;
15148 + uByte bU1DevExitLat;
15149 + uWord wU2DevExitLat;
15150 +} UPACKED usb_dev_cap_ss_usb_t;
15151 +
15152 +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
15153 +typedef struct usb_dev_cap_container_id {
15154 + uByte bLength;
15155 + uByte bDescriptorType;
15156 + uByte bDevCapabilityType;
15157 + uByte bReserved;
15158 + uByte containerID[16];
15159 +} UPACKED usb_dev_cap_container_id_t;
15160 +
15161 +/* Device Capability Type Codes */
15162 +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
15163 +
15164 +/* Device Capability Descriptor */
15165 +typedef struct wusb_dev_cap_desc {
15166 + uByte bLength;
15167 + uByte bDescriptorType;
15168 + uByte bDevCapabilityType;
15169 + uByte caps[1]; /* Variable length */
15170 +} UPACKED wusb_dev_cap_desc_t;
15171 +
15172 +/* Device Capability Descriptor */
15173 +typedef struct wusb_dev_cap_uwb_desc {
15174 + uByte bLength;
15175 + uByte bDescriptorType;
15176 + uByte bDevCapabilityType;
15177 + uByte bmAttributes;
15178 + uWord wPHYRates; /* Bitmap */
15179 + uByte bmTFITXPowerInfo;
15180 + uByte bmFFITXPowerInfo;
15181 + uWord bmBandGroup;
15182 + uByte bReserved;
15183 +} UPACKED wusb_dev_cap_uwb_desc_t;
15184 +
15185 +/* Wireless USB Endpoint Companion Descriptor */
15186 +typedef struct wusb_endpoint_companion_desc {
15187 + uByte bLength;
15188 + uByte bDescriptorType;
15189 + uByte bMaxBurst;
15190 + uByte bMaxSequence;
15191 + uWord wMaxStreamDelay;
15192 + uWord wOverTheAirPacketSize;
15193 + uByte bOverTheAirInterval;
15194 + uByte bmCompAttributes;
15195 +} UPACKED wusb_endpoint_companion_desc_t;
15196 +
15197 +/* Wireless USB Numeric Association M1 Data Structure */
15198 +typedef struct wusb_m1_data {
15199 + uByte version;
15200 + uWord langId;
15201 + uByte deviceFriendlyNameLength;
15202 + uByte sha_256_m3[32];
15203 + uByte deviceFriendlyName[256];
15204 +} UPACKED wusb_m1_data_t;
15205 +
15206 +typedef struct wusb_m2_data {
15207 + uByte version;
15208 + uWord langId;
15209 + uByte hostFriendlyNameLength;
15210 + uByte pkh[384];
15211 + uByte hostFriendlyName[256];
15212 +} UPACKED wusb_m2_data_t;
15213 +
15214 +typedef struct wusb_m3_data {
15215 + uByte pkd[384];
15216 + uByte nd;
15217 +} UPACKED wusb_m3_data_t;
15218 +
15219 +typedef struct wusb_m4_data {
15220 + uDWord _attributeTypeIdAndLength_1;
15221 + uWord associationTypeId;
15222 +
15223 + uDWord _attributeTypeIdAndLength_2;
15224 + uWord associationSubTypeId;
15225 +
15226 + uDWord _attributeTypeIdAndLength_3;
15227 + uDWord length;
15228 +
15229 + uDWord _attributeTypeIdAndLength_4;
15230 + uDWord associationStatus;
15231 +
15232 + uDWord _attributeTypeIdAndLength_5;
15233 + uByte chid[16];
15234 +
15235 + uDWord _attributeTypeIdAndLength_6;
15236 + uByte cdid[16];
15237 +
15238 + uDWord _attributeTypeIdAndLength_7;
15239 + uByte bandGroups[2];
15240 +} UPACKED wusb_m4_data_t;
15241 +
15242 +#ifdef _MSC_VER
15243 +#include <poppack.h>
15244 +#endif
15245 +
15246 +#ifdef __cplusplus
15247 +}
15248 +#endif
15249 +
15250 +#endif /* _USB_H_ */
15251 --- /dev/null
15252 +++ b/drivers/usb/host/dwc_otg/Makefile
15253 @@ -0,0 +1,82 @@
15254 +#
15255 +# Makefile for DWC_otg Highspeed USB controller driver
15256 +#
15257 +
15258 +ifneq ($(KERNELRELEASE),)
15259 +
15260 +# Use the BUS_INTERFACE variable to compile the software for either
15261 +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
15262 +ifeq ($(BUS_INTERFACE),)
15263 +# BUS_INTERFACE = -DPCI_INTERFACE
15264 +# BUS_INTERFACE = -DLM_INTERFACE
15265 + BUS_INTERFACE = -DPLATFORM_INTERFACE
15266 +endif
15267 +
15268 +#ccflags-y += -DDEBUG
15269 +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
15270 +
15271 +# Use one of the following flags to compile the software in host-only or
15272 +# device-only mode.
15273 +#ccflags-y += -DDWC_HOST_ONLY
15274 +#ccflags-y += -DDWC_DEVICE_ONLY
15275 +
15276 +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
15277 +#ccflags-y += -DDWC_EN_ISOC
15278 +ccflags-y += -I$(obj)/../dwc_common_port
15279 +#ccflags-y += -I$(PORTLIB)
15280 +ccflags-y += -DDWC_LINUX
15281 +ccflags-y += $(CFI)
15282 +ccflags-y += $(BUS_INTERFACE)
15283 +#ccflags-y += -DDWC_DEV_SRPCAP
15284 +
15285 +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
15286 +
15287 +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
15288 +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
15289 +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
15290 +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
15291 +dwc_otg-objs += dwc_otg_adp.o
15292 +dwc_otg-objs += dwc_otg_fiq_fsm.o
15293 +dwc_otg-objs += dwc_otg_fiq_stub.o
15294 +ifneq ($(CFI),)
15295 +dwc_otg-objs += dwc_otg_cfi.o
15296 +endif
15297 +
15298 +kernrelwd := $(subst ., ,$(KERNELRELEASE))
15299 +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
15300 +
15301 +ifneq ($(kernrel3),2.6.20)
15302 +ccflags-y += $(CPPFLAGS)
15303 +endif
15304 +
15305 +else
15306 +
15307 +PWD := $(shell pwd)
15308 +PORTLIB := $(PWD)/../dwc_common_port
15309 +
15310 +# Command paths
15311 +CTAGS := $(CTAGS)
15312 +DOXYGEN := $(DOXYGEN)
15313 +
15314 +default: portlib
15315 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
15316 +
15317 +install: default
15318 + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
15319 + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
15320 +
15321 +portlib:
15322 + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
15323 + cp $(PORTLIB)/Module.symvers $(PWD)/
15324 +
15325 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
15326 + $(DOXYGEN) doc/doxygen.cfg
15327 +
15328 +tags: $(wildcard *.[hc])
15329 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
15330 +
15331 +
15332 +clean:
15333 + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
15334 +
15335 +endif
15336 --- /dev/null
15337 +++ b/drivers/usb/host/dwc_otg/doc/doxygen.cfg
15338 @@ -0,0 +1,224 @@
15339 +# Doxyfile 1.3.9.1
15340 +
15341 +#---------------------------------------------------------------------------
15342 +# Project related configuration options
15343 +#---------------------------------------------------------------------------
15344 +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
15345 +PROJECT_NUMBER = v3.00a
15346 +OUTPUT_DIRECTORY = ./doc/
15347 +CREATE_SUBDIRS = NO
15348 +OUTPUT_LANGUAGE = English
15349 +BRIEF_MEMBER_DESC = YES
15350 +REPEAT_BRIEF = YES
15351 +ABBREVIATE_BRIEF = "The $name class" \
15352 + "The $name widget" \
15353 + "The $name file" \
15354 + is \
15355 + provides \
15356 + specifies \
15357 + contains \
15358 + represents \
15359 + a \
15360 + an \
15361 + the
15362 +ALWAYS_DETAILED_SEC = NO
15363 +INLINE_INHERITED_MEMB = NO
15364 +FULL_PATH_NAMES = NO
15365 +STRIP_FROM_PATH =
15366 +STRIP_FROM_INC_PATH =
15367 +SHORT_NAMES = NO
15368 +JAVADOC_AUTOBRIEF = YES
15369 +MULTILINE_CPP_IS_BRIEF = NO
15370 +INHERIT_DOCS = YES
15371 +DISTRIBUTE_GROUP_DOC = NO
15372 +TAB_SIZE = 8
15373 +ALIASES =
15374 +OPTIMIZE_OUTPUT_FOR_C = YES
15375 +OPTIMIZE_OUTPUT_JAVA = NO
15376 +SUBGROUPING = YES
15377 +#---------------------------------------------------------------------------
15378 +# Build related configuration options
15379 +#---------------------------------------------------------------------------
15380 +EXTRACT_ALL = NO
15381 +EXTRACT_PRIVATE = YES
15382 +EXTRACT_STATIC = YES
15383 +EXTRACT_LOCAL_CLASSES = YES
15384 +EXTRACT_LOCAL_METHODS = NO
15385 +HIDE_UNDOC_MEMBERS = NO
15386 +HIDE_UNDOC_CLASSES = NO
15387 +HIDE_FRIEND_COMPOUNDS = NO
15388 +HIDE_IN_BODY_DOCS = NO
15389 +INTERNAL_DOCS = NO
15390 +CASE_SENSE_NAMES = NO
15391 +HIDE_SCOPE_NAMES = NO
15392 +SHOW_INCLUDE_FILES = YES
15393 +INLINE_INFO = YES
15394 +SORT_MEMBER_DOCS = NO
15395 +SORT_BRIEF_DOCS = NO
15396 +SORT_BY_SCOPE_NAME = NO
15397 +GENERATE_TODOLIST = YES
15398 +GENERATE_TESTLIST = YES
15399 +GENERATE_BUGLIST = YES
15400 +GENERATE_DEPRECATEDLIST= YES
15401 +ENABLED_SECTIONS =
15402 +MAX_INITIALIZER_LINES = 30
15403 +SHOW_USED_FILES = YES
15404 +SHOW_DIRECTORIES = YES
15405 +#---------------------------------------------------------------------------
15406 +# configuration options related to warning and progress messages
15407 +#---------------------------------------------------------------------------
15408 +QUIET = YES
15409 +WARNINGS = YES
15410 +WARN_IF_UNDOCUMENTED = NO
15411 +WARN_IF_DOC_ERROR = YES
15412 +WARN_FORMAT = "$file:$line: $text"
15413 +WARN_LOGFILE =
15414 +#---------------------------------------------------------------------------
15415 +# configuration options related to the input files
15416 +#---------------------------------------------------------------------------
15417 +INPUT = .
15418 +FILE_PATTERNS = *.c \
15419 + *.h \
15420 + ./linux/*.c \
15421 + ./linux/*.h
15422 +RECURSIVE = NO
15423 +EXCLUDE = ./test/ \
15424 + ./dwc_otg/.AppleDouble/
15425 +EXCLUDE_SYMLINKS = YES
15426 +EXCLUDE_PATTERNS = *.mod.*
15427 +EXAMPLE_PATH =
15428 +EXAMPLE_PATTERNS = *
15429 +EXAMPLE_RECURSIVE = NO
15430 +IMAGE_PATH =
15431 +INPUT_FILTER =
15432 +FILTER_PATTERNS =
15433 +FILTER_SOURCE_FILES = NO
15434 +#---------------------------------------------------------------------------
15435 +# configuration options related to source browsing
15436 +#---------------------------------------------------------------------------
15437 +SOURCE_BROWSER = YES
15438 +INLINE_SOURCES = NO
15439 +STRIP_CODE_COMMENTS = YES
15440 +REFERENCED_BY_RELATION = NO
15441 +REFERENCES_RELATION = NO
15442 +VERBATIM_HEADERS = NO
15443 +#---------------------------------------------------------------------------
15444 +# configuration options related to the alphabetical class index
15445 +#---------------------------------------------------------------------------
15446 +ALPHABETICAL_INDEX = NO
15447 +COLS_IN_ALPHA_INDEX = 5
15448 +IGNORE_PREFIX =
15449 +#---------------------------------------------------------------------------
15450 +# configuration options related to the HTML output
15451 +#---------------------------------------------------------------------------
15452 +GENERATE_HTML = YES
15453 +HTML_OUTPUT = html
15454 +HTML_FILE_EXTENSION = .html
15455 +HTML_HEADER =
15456 +HTML_FOOTER =
15457 +HTML_STYLESHEET =
15458 +HTML_ALIGN_MEMBERS = YES
15459 +GENERATE_HTMLHELP = NO
15460 +CHM_FILE =
15461 +HHC_LOCATION =
15462 +GENERATE_CHI = NO
15463 +BINARY_TOC = NO
15464 +TOC_EXPAND = NO
15465 +DISABLE_INDEX = NO
15466 +ENUM_VALUES_PER_LINE = 4
15467 +GENERATE_TREEVIEW = YES
15468 +TREEVIEW_WIDTH = 250
15469 +#---------------------------------------------------------------------------
15470 +# configuration options related to the LaTeX output
15471 +#---------------------------------------------------------------------------
15472 +GENERATE_LATEX = NO
15473 +LATEX_OUTPUT = latex
15474 +LATEX_CMD_NAME = latex
15475 +MAKEINDEX_CMD_NAME = makeindex
15476 +COMPACT_LATEX = NO
15477 +PAPER_TYPE = a4wide
15478 +EXTRA_PACKAGES =
15479 +LATEX_HEADER =
15480 +PDF_HYPERLINKS = NO
15481 +USE_PDFLATEX = NO
15482 +LATEX_BATCHMODE = NO
15483 +LATEX_HIDE_INDICES = NO
15484 +#---------------------------------------------------------------------------
15485 +# configuration options related to the RTF output
15486 +#---------------------------------------------------------------------------
15487 +GENERATE_RTF = NO
15488 +RTF_OUTPUT = rtf
15489 +COMPACT_RTF = NO
15490 +RTF_HYPERLINKS = NO
15491 +RTF_STYLESHEET_FILE =
15492 +RTF_EXTENSIONS_FILE =
15493 +#---------------------------------------------------------------------------
15494 +# configuration options related to the man page output
15495 +#---------------------------------------------------------------------------
15496 +GENERATE_MAN = NO
15497 +MAN_OUTPUT = man
15498 +MAN_EXTENSION = .3
15499 +MAN_LINKS = NO
15500 +#---------------------------------------------------------------------------
15501 +# configuration options related to the XML output
15502 +#---------------------------------------------------------------------------
15503 +GENERATE_XML = NO
15504 +XML_OUTPUT = xml
15505 +XML_SCHEMA =
15506 +XML_DTD =
15507 +XML_PROGRAMLISTING = YES
15508 +#---------------------------------------------------------------------------
15509 +# configuration options for the AutoGen Definitions output
15510 +#---------------------------------------------------------------------------
15511 +GENERATE_AUTOGEN_DEF = NO
15512 +#---------------------------------------------------------------------------
15513 +# configuration options related to the Perl module output
15514 +#---------------------------------------------------------------------------
15515 +GENERATE_PERLMOD = NO
15516 +PERLMOD_LATEX = NO
15517 +PERLMOD_PRETTY = YES
15518 +PERLMOD_MAKEVAR_PREFIX =
15519 +#---------------------------------------------------------------------------
15520 +# Configuration options related to the preprocessor
15521 +#---------------------------------------------------------------------------
15522 +ENABLE_PREPROCESSING = YES
15523 +MACRO_EXPANSION = YES
15524 +EXPAND_ONLY_PREDEF = YES
15525 +SEARCH_INCLUDES = YES
15526 +INCLUDE_PATH =
15527 +INCLUDE_FILE_PATTERNS =
15528 +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
15529 +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
15530 +SKIP_FUNCTION_MACROS = NO
15531 +#---------------------------------------------------------------------------
15532 +# Configuration::additions related to external references
15533 +#---------------------------------------------------------------------------
15534 +TAGFILES =
15535 +GENERATE_TAGFILE =
15536 +ALLEXTERNALS = NO
15537 +EXTERNAL_GROUPS = YES
15538 +PERL_PATH = /usr/bin/perl
15539 +#---------------------------------------------------------------------------
15540 +# Configuration options related to the dot tool
15541 +#---------------------------------------------------------------------------
15542 +CLASS_DIAGRAMS = YES
15543 +HIDE_UNDOC_RELATIONS = YES
15544 +HAVE_DOT = NO
15545 +CLASS_GRAPH = YES
15546 +COLLABORATION_GRAPH = YES
15547 +UML_LOOK = NO
15548 +TEMPLATE_RELATIONS = NO
15549 +INCLUDE_GRAPH = YES
15550 +INCLUDED_BY_GRAPH = YES
15551 +CALL_GRAPH = NO
15552 +GRAPHICAL_HIERARCHY = YES
15553 +DOT_IMAGE_FORMAT = png
15554 +DOT_PATH =
15555 +DOTFILE_DIRS =
15556 +MAX_DOT_GRAPH_DEPTH = 1000
15557 +GENERATE_LEGEND = YES
15558 +DOT_CLEANUP = YES
15559 +#---------------------------------------------------------------------------
15560 +# Configuration::additions related to the search engine
15561 +#---------------------------------------------------------------------------
15562 +SEARCHENGINE = NO
15563 --- /dev/null
15564 +++ b/drivers/usb/host/dwc_otg/dummy_audio.c
15565 @@ -0,0 +1,1574 @@
15566 +/*
15567 + * zero.c -- Gadget Zero, for USB development
15568 + *
15569 + * Copyright (C) 2003-2004 David Brownell
15570 + * All rights reserved.
15571 + *
15572 + * Redistribution and use in source and binary forms, with or without
15573 + * modification, are permitted provided that the following conditions
15574 + * are met:
15575 + * 1. Redistributions of source code must retain the above copyright
15576 + * notice, this list of conditions, and the following disclaimer,
15577 + * without modification.
15578 + * 2. Redistributions in binary form must reproduce the above copyright
15579 + * notice, this list of conditions and the following disclaimer in the
15580 + * documentation and/or other materials provided with the distribution.
15581 + * 3. The names of the above-listed copyright holders may not be used
15582 + * to endorse or promote products derived from this software without
15583 + * specific prior written permission.
15584 + *
15585 + * ALTERNATIVELY, this software may be distributed under the terms of the
15586 + * GNU General Public License ("GPL") as published by the Free Software
15587 + * Foundation, either version 2 of that License or (at your option) any
15588 + * later version.
15589 + *
15590 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
15591 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15592 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15593 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
15594 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15595 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
15596 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
15597 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
15598 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
15599 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
15600 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15601 + */
15602 +
15603 +
15604 +/*
15605 + * Gadget Zero only needs two bulk endpoints, and is an example of how you
15606 + * can write a hardware-agnostic gadget driver running inside a USB device.
15607 + *
15608 + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
15609 + * affect most of the driver.
15610 + *
15611 + * Use it with the Linux host/master side "usbtest" driver to get a basic
15612 + * functional test of your device-side usb stack, or with "usb-skeleton".
15613 + *
15614 + * It supports two similar configurations. One sinks whatever the usb host
15615 + * writes, and in return sources zeroes. The other loops whatever the host
15616 + * writes back, so the host can read it. Module options include:
15617 + *
15618 + * buflen=N default N=4096, buffer size used
15619 + * qlen=N default N=32, how many buffers in the loopback queue
15620 + * loopdefault default false, list loopback config first
15621 + *
15622 + * Many drivers will only have one configuration, letting them be much
15623 + * simpler if they also don't support high speed operation (like this
15624 + * driver does).
15625 + */
15626 +
15627 +#include <linux/config.h>
15628 +#include <linux/module.h>
15629 +#include <linux/kernel.h>
15630 +#include <linux/delay.h>
15631 +#include <linux/ioport.h>
15632 +#include <linux/sched.h>
15633 +#include <linux/slab.h>
15634 +#include <linux/smp_lock.h>
15635 +#include <linux/errno.h>
15636 +#include <linux/init.h>
15637 +#include <linux/timer.h>
15638 +#include <linux/list.h>
15639 +#include <linux/interrupt.h>
15640 +#include <linux/uts.h>
15641 +#include <linux/version.h>
15642 +#include <linux/device.h>
15643 +#include <linux/moduleparam.h>
15644 +#include <linux/proc_fs.h>
15645 +
15646 +#include <asm/byteorder.h>
15647 +#include <asm/io.h>
15648 +#include <asm/irq.h>
15649 +#include <asm/system.h>
15650 +#include <asm/unaligned.h>
15651 +
15652 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
15653 +# include <linux/usb/ch9.h>
15654 +#else
15655 +# include <linux/usb_ch9.h>
15656 +#endif
15657 +
15658 +#include <linux/usb_gadget.h>
15659 +
15660 +
15661 +/*-------------------------------------------------------------------------*/
15662 +/*-------------------------------------------------------------------------*/
15663 +
15664 +
15665 +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
15666 +{
15667 + int count = 0;
15668 + u8 c;
15669 + u16 uchar;
15670 +
15671 + /* this insists on correct encodings, though not minimal ones.
15672 + * BUT it currently rejects legit 4-byte UTF-8 code points,
15673 + * which need surrogate pairs. (Unicode 3.1 can use them.)
15674 + */
15675 + while (len != 0 && (c = (u8) *s++) != 0) {
15676 + if (unlikely(c & 0x80)) {
15677 + // 2-byte sequence:
15678 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
15679 + if ((c & 0xe0) == 0xc0) {
15680 + uchar = (c & 0x1f) << 6;
15681 +
15682 + c = (u8) *s++;
15683 + if ((c & 0xc0) != 0xc0)
15684 + goto fail;
15685 + c &= 0x3f;
15686 + uchar |= c;
15687 +
15688 + // 3-byte sequence (most CJKV characters):
15689 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
15690 + } else if ((c & 0xf0) == 0xe0) {
15691 + uchar = (c & 0x0f) << 12;
15692 +
15693 + c = (u8) *s++;
15694 + if ((c & 0xc0) != 0xc0)
15695 + goto fail;
15696 + c &= 0x3f;
15697 + uchar |= c << 6;
15698 +
15699 + c = (u8) *s++;
15700 + if ((c & 0xc0) != 0xc0)
15701 + goto fail;
15702 + c &= 0x3f;
15703 + uchar |= c;
15704 +
15705 + /* no bogus surrogates */
15706 + if (0xd800 <= uchar && uchar <= 0xdfff)
15707 + goto fail;
15708 +
15709 + // 4-byte sequence (surrogate pairs, currently rare):
15710 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
15711 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
15712 + // (uuuuu = wwww + 1)
15713 + // FIXME accept the surrogate code points (only)
15714 +
15715 + } else
15716 + goto fail;
15717 + } else
15718 + uchar = c;
15719 + put_unaligned (cpu_to_le16 (uchar), cp++);
15720 + count++;
15721 + len--;
15722 + }
15723 + return count;
15724 +fail:
15725 + return -1;
15726 +}
15727 +
15728 +
15729 +/**
15730 + * usb_gadget_get_string - fill out a string descriptor
15731 + * @table: of c strings encoded using UTF-8
15732 + * @id: string id, from low byte of wValue in get string descriptor
15733 + * @buf: at least 256 bytes
15734 + *
15735 + * Finds the UTF-8 string matching the ID, and converts it into a
15736 + * string descriptor in utf16-le.
15737 + * Returns length of descriptor (always even) or negative errno
15738 + *
15739 + * If your driver needs stings in multiple languages, you'll probably
15740 + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
15741 + * using this routine after choosing which set of UTF-8 strings to use.
15742 + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
15743 + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
15744 + * characters (which are also widely used in C strings).
15745 + */
15746 +int
15747 +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
15748 +{
15749 + struct usb_string *s;
15750 + int len;
15751 +
15752 + /* descriptor 0 has the language id */
15753 + if (id == 0) {
15754 + buf [0] = 4;
15755 + buf [1] = USB_DT_STRING;
15756 + buf [2] = (u8) table->language;
15757 + buf [3] = (u8) (table->language >> 8);
15758 + return 4;
15759 + }
15760 + for (s = table->strings; s && s->s; s++)
15761 + if (s->id == id)
15762 + break;
15763 +
15764 + /* unrecognized: stall. */
15765 + if (!s || !s->s)
15766 + return -EINVAL;
15767 +
15768 + /* string descriptors have length, tag, then UTF16-LE text */
15769 + len = min ((size_t) 126, strlen (s->s));
15770 + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
15771 + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
15772 + if (len < 0)
15773 + return -EINVAL;
15774 + buf [0] = (len + 1) * 2;
15775 + buf [1] = USB_DT_STRING;
15776 + return buf [0];
15777 +}
15778 +
15779 +
15780 +/*-------------------------------------------------------------------------*/
15781 +/*-------------------------------------------------------------------------*/
15782 +
15783 +
15784 +/**
15785 + * usb_descriptor_fillbuf - fill buffer with descriptors
15786 + * @buf: Buffer to be filled
15787 + * @buflen: Size of buf
15788 + * @src: Array of descriptor pointers, terminated by null pointer.
15789 + *
15790 + * Copies descriptors into the buffer, returning the length or a
15791 + * negative error code if they can't all be copied. Useful when
15792 + * assembling descriptors for an associated set of interfaces used
15793 + * as part of configuring a composite device; or in other cases where
15794 + * sets of descriptors need to be marshaled.
15795 + */
15796 +int
15797 +usb_descriptor_fillbuf(void *buf, unsigned buflen,
15798 + const struct usb_descriptor_header **src)
15799 +{
15800 + u8 *dest = buf;
15801 +
15802 + if (!src)
15803 + return -EINVAL;
15804 +
15805 + /* fill buffer from src[] until null descriptor ptr */
15806 + for (; 0 != *src; src++) {
15807 + unsigned len = (*src)->bLength;
15808 +
15809 + if (len > buflen)
15810 + return -EINVAL;
15811 + memcpy(dest, *src, len);
15812 + buflen -= len;
15813 + dest += len;
15814 + }
15815 + return dest - (u8 *)buf;
15816 +}
15817 +
15818 +
15819 +/**
15820 + * usb_gadget_config_buf - builts a complete configuration descriptor
15821 + * @config: Header for the descriptor, including characteristics such
15822 + * as power requirements and number of interfaces.
15823 + * @desc: Null-terminated vector of pointers to the descriptors (interface,
15824 + * endpoint, etc) defining all functions in this device configuration.
15825 + * @buf: Buffer for the resulting configuration descriptor.
15826 + * @length: Length of buffer. If this is not big enough to hold the
15827 + * entire configuration descriptor, an error code will be returned.
15828 + *
15829 + * This copies descriptors into the response buffer, building a descriptor
15830 + * for that configuration. It returns the buffer length or a negative
15831 + * status code. The config.wTotalLength field is set to match the length
15832 + * of the result, but other descriptor fields (including power usage and
15833 + * interface count) must be set by the caller.
15834 + *
15835 + * Gadget drivers could use this when constructing a config descriptor
15836 + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
15837 + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
15838 + */
15839 +int usb_gadget_config_buf(
15840 + const struct usb_config_descriptor *config,
15841 + void *buf,
15842 + unsigned length,
15843 + const struct usb_descriptor_header **desc
15844 +)
15845 +{
15846 + struct usb_config_descriptor *cp = buf;
15847 + int len;
15848 +
15849 + /* config descriptor first */
15850 + if (length < USB_DT_CONFIG_SIZE || !desc)
15851 + return -EINVAL;
15852 + *cp = *config;
15853 +
15854 + /* then interface/endpoint/class/vendor/... */
15855 + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
15856 + length - USB_DT_CONFIG_SIZE, desc);
15857 + if (len < 0)
15858 + return len;
15859 + len += USB_DT_CONFIG_SIZE;
15860 + if (len > 0xffff)
15861 + return -EINVAL;
15862 +
15863 + /* patch up the config descriptor */
15864 + cp->bLength = USB_DT_CONFIG_SIZE;
15865 + cp->bDescriptorType = USB_DT_CONFIG;
15866 + cp->wTotalLength = cpu_to_le16(len);
15867 + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
15868 + return len;
15869 +}
15870 +
15871 +/*-------------------------------------------------------------------------*/
15872 +/*-------------------------------------------------------------------------*/
15873 +
15874 +
15875 +#define RBUF_LEN (1024*1024)
15876 +static int rbuf_start;
15877 +static int rbuf_len;
15878 +static __u8 rbuf[RBUF_LEN];
15879 +
15880 +/*-------------------------------------------------------------------------*/
15881 +
15882 +#define DRIVER_VERSION "St Patrick's Day 2004"
15883 +
15884 +static const char shortname [] = "zero";
15885 +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
15886 +
15887 +static const char source_sink [] = "source and sink data";
15888 +static const char loopback [] = "loop input to output";
15889 +
15890 +/*-------------------------------------------------------------------------*/
15891 +
15892 +/*
15893 + * driver assumes self-powered hardware, and
15894 + * has no way for users to trigger remote wakeup.
15895 + *
15896 + * this version autoconfigures as much as possible,
15897 + * which is reasonable for most "bulk-only" drivers.
15898 + */
15899 +static const char *EP_IN_NAME; /* source */
15900 +static const char *EP_OUT_NAME; /* sink */
15901 +
15902 +/*-------------------------------------------------------------------------*/
15903 +
15904 +/* big enough to hold our biggest descriptor */
15905 +#define USB_BUFSIZ 512
15906 +
15907 +struct zero_dev {
15908 + spinlock_t lock;
15909 + struct usb_gadget *gadget;
15910 + struct usb_request *req; /* for control responses */
15911 +
15912 + /* when configured, we have one of two configs:
15913 + * - source data (in to host) and sink it (out from host)
15914 + * - or loop it back (out from host back in to host)
15915 + */
15916 + u8 config;
15917 + struct usb_ep *in_ep, *out_ep;
15918 +
15919 + /* autoresume timer */
15920 + struct timer_list resume;
15921 +};
15922 +
15923 +#define xprintk(d,level,fmt,args...) \
15924 + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
15925 +
15926 +#ifdef DEBUG
15927 +#define DBG(dev,fmt,args...) \
15928 + xprintk(dev , KERN_DEBUG , fmt , ## args)
15929 +#else
15930 +#define DBG(dev,fmt,args...) \
15931 + do { } while (0)
15932 +#endif /* DEBUG */
15933 +
15934 +#ifdef VERBOSE
15935 +#define VDBG DBG
15936 +#else
15937 +#define VDBG(dev,fmt,args...) \
15938 + do { } while (0)
15939 +#endif /* VERBOSE */
15940 +
15941 +#define ERROR(dev,fmt,args...) \
15942 + xprintk(dev , KERN_ERR , fmt , ## args)
15943 +#define WARN(dev,fmt,args...) \
15944 + xprintk(dev , KERN_WARNING , fmt , ## args)
15945 +#define INFO(dev,fmt,args...) \
15946 + xprintk(dev , KERN_INFO , fmt , ## args)
15947 +
15948 +/*-------------------------------------------------------------------------*/
15949 +
15950 +static unsigned buflen = 4096;
15951 +static unsigned qlen = 32;
15952 +static unsigned pattern = 0;
15953 +
15954 +module_param (buflen, uint, S_IRUGO|S_IWUSR);
15955 +module_param (qlen, uint, S_IRUGO|S_IWUSR);
15956 +module_param (pattern, uint, S_IRUGO|S_IWUSR);
15957 +
15958 +/*
15959 + * if it's nonzero, autoresume says how many seconds to wait
15960 + * before trying to wake up the host after suspend.
15961 + */
15962 +static unsigned autoresume = 0;
15963 +module_param (autoresume, uint, 0);
15964 +
15965 +/*
15966 + * Normally the "loopback" configuration is second (index 1) so
15967 + * it's not the default. Here's where to change that order, to
15968 + * work better with hosts where config changes are problematic.
15969 + * Or controllers (like superh) that only support one config.
15970 + */
15971 +static int loopdefault = 0;
15972 +
15973 +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
15974 +
15975 +/*-------------------------------------------------------------------------*/
15976 +
15977 +/* Thanks to NetChip Technologies for donating this product ID.
15978 + *
15979 + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
15980 + * Instead: allocate your own, using normal USB-IF procedures.
15981 + */
15982 +#ifndef CONFIG_USB_ZERO_HNPTEST
15983 +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
15984 +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
15985 +#else
15986 +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
15987 +#define DRIVER_PRODUCT_NUM 0xbadd
15988 +#endif
15989 +
15990 +/*-------------------------------------------------------------------------*/
15991 +
15992 +/*
15993 + * DESCRIPTORS ... most are static, but strings and (full)
15994 + * configuration descriptors are built on demand.
15995 + */
15996 +
15997 +/*
15998 +#define STRING_MANUFACTURER 25
15999 +#define STRING_PRODUCT 42
16000 +#define STRING_SERIAL 101
16001 +*/
16002 +#define STRING_MANUFACTURER 1
16003 +#define STRING_PRODUCT 2
16004 +#define STRING_SERIAL 3
16005 +
16006 +#define STRING_SOURCE_SINK 250
16007 +#define STRING_LOOPBACK 251
16008 +
16009 +/*
16010 + * This device advertises two configurations; these numbers work
16011 + * on a pxa250 as well as more flexible hardware.
16012 + */
16013 +#define CONFIG_SOURCE_SINK 3
16014 +#define CONFIG_LOOPBACK 2
16015 +
16016 +/*
16017 +static struct usb_device_descriptor
16018 +device_desc = {
16019 + .bLength = sizeof device_desc,
16020 + .bDescriptorType = USB_DT_DEVICE,
16021 +
16022 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
16023 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
16024 +
16025 + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
16026 + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
16027 + .iManufacturer = STRING_MANUFACTURER,
16028 + .iProduct = STRING_PRODUCT,
16029 + .iSerialNumber = STRING_SERIAL,
16030 + .bNumConfigurations = 2,
16031 +};
16032 +*/
16033 +static struct usb_device_descriptor
16034 +device_desc = {
16035 + .bLength = sizeof device_desc,
16036 + .bDescriptorType = USB_DT_DEVICE,
16037 + .bcdUSB = __constant_cpu_to_le16 (0x0100),
16038 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
16039 + .bDeviceSubClass = 0,
16040 + .bDeviceProtocol = 0,
16041 + .bMaxPacketSize0 = 64,
16042 + .bcdDevice = __constant_cpu_to_le16 (0x0100),
16043 + .idVendor = __constant_cpu_to_le16 (0x0499),
16044 + .idProduct = __constant_cpu_to_le16 (0x3002),
16045 + .iManufacturer = STRING_MANUFACTURER,
16046 + .iProduct = STRING_PRODUCT,
16047 + .iSerialNumber = STRING_SERIAL,
16048 + .bNumConfigurations = 1,
16049 +};
16050 +
16051 +static struct usb_config_descriptor
16052 +z_config = {
16053 + .bLength = sizeof z_config,
16054 + .bDescriptorType = USB_DT_CONFIG,
16055 +
16056 + /* compute wTotalLength on the fly */
16057 + .bNumInterfaces = 2,
16058 + .bConfigurationValue = 1,
16059 + .iConfiguration = 0,
16060 + .bmAttributes = 0x40,
16061 + .bMaxPower = 0, /* self-powered */
16062 +};
16063 +
16064 +
16065 +static struct usb_otg_descriptor
16066 +otg_descriptor = {
16067 + .bLength = sizeof otg_descriptor,
16068 + .bDescriptorType = USB_DT_OTG,
16069 +
16070 + .bmAttributes = USB_OTG_SRP,
16071 +};
16072 +
16073 +/* one interface in each configuration */
16074 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16075 +
16076 +/*
16077 + * usb 2.0 devices need to expose both high speed and full speed
16078 + * descriptors, unless they only run at full speed.
16079 + *
16080 + * that means alternate endpoint descriptors (bigger packets)
16081 + * and a "device qualifier" ... plus more construction options
16082 + * for the config descriptor.
16083 + */
16084 +
16085 +static struct usb_qualifier_descriptor
16086 +dev_qualifier = {
16087 + .bLength = sizeof dev_qualifier,
16088 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
16089 +
16090 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
16091 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
16092 +
16093 + .bNumConfigurations = 2,
16094 +};
16095 +
16096 +
16097 +struct usb_cs_as_general_descriptor {
16098 + __u8 bLength;
16099 + __u8 bDescriptorType;
16100 +
16101 + __u8 bDescriptorSubType;
16102 + __u8 bTerminalLink;
16103 + __u8 bDelay;
16104 + __u16 wFormatTag;
16105 +} __attribute__ ((packed));
16106 +
16107 +struct usb_cs_as_format_descriptor {
16108 + __u8 bLength;
16109 + __u8 bDescriptorType;
16110 +
16111 + __u8 bDescriptorSubType;
16112 + __u8 bFormatType;
16113 + __u8 bNrChannels;
16114 + __u8 bSubframeSize;
16115 + __u8 bBitResolution;
16116 + __u8 bSamfreqType;
16117 + __u8 tLowerSamFreq[3];
16118 + __u8 tUpperSamFreq[3];
16119 +} __attribute__ ((packed));
16120 +
16121 +static const struct usb_interface_descriptor
16122 +z_audio_control_if_desc = {
16123 + .bLength = sizeof z_audio_control_if_desc,
16124 + .bDescriptorType = USB_DT_INTERFACE,
16125 + .bInterfaceNumber = 0,
16126 + .bAlternateSetting = 0,
16127 + .bNumEndpoints = 0,
16128 + .bInterfaceClass = USB_CLASS_AUDIO,
16129 + .bInterfaceSubClass = 0x1,
16130 + .bInterfaceProtocol = 0,
16131 + .iInterface = 0,
16132 +};
16133 +
16134 +static const struct usb_interface_descriptor
16135 +z_audio_if_desc = {
16136 + .bLength = sizeof z_audio_if_desc,
16137 + .bDescriptorType = USB_DT_INTERFACE,
16138 + .bInterfaceNumber = 1,
16139 + .bAlternateSetting = 0,
16140 + .bNumEndpoints = 0,
16141 + .bInterfaceClass = USB_CLASS_AUDIO,
16142 + .bInterfaceSubClass = 0x2,
16143 + .bInterfaceProtocol = 0,
16144 + .iInterface = 0,
16145 +};
16146 +
16147 +static const struct usb_interface_descriptor
16148 +z_audio_if_desc2 = {
16149 + .bLength = sizeof z_audio_if_desc,
16150 + .bDescriptorType = USB_DT_INTERFACE,
16151 + .bInterfaceNumber = 1,
16152 + .bAlternateSetting = 1,
16153 + .bNumEndpoints = 1,
16154 + .bInterfaceClass = USB_CLASS_AUDIO,
16155 + .bInterfaceSubClass = 0x2,
16156 + .bInterfaceProtocol = 0,
16157 + .iInterface = 0,
16158 +};
16159 +
16160 +static const struct usb_cs_as_general_descriptor
16161 +z_audio_cs_as_if_desc = {
16162 + .bLength = 7,
16163 + .bDescriptorType = 0x24,
16164 +
16165 + .bDescriptorSubType = 0x01,
16166 + .bTerminalLink = 0x01,
16167 + .bDelay = 0x0,
16168 + .wFormatTag = __constant_cpu_to_le16 (0x0001)
16169 +};
16170 +
16171 +
16172 +static const struct usb_cs_as_format_descriptor
16173 +z_audio_cs_as_format_desc = {
16174 + .bLength = 0xe,
16175 + .bDescriptorType = 0x24,
16176 +
16177 + .bDescriptorSubType = 2,
16178 + .bFormatType = 1,
16179 + .bNrChannels = 1,
16180 + .bSubframeSize = 1,
16181 + .bBitResolution = 8,
16182 + .bSamfreqType = 0,
16183 + .tLowerSamFreq = {0x7e, 0x13, 0x00},
16184 + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
16185 +};
16186 +
16187 +static const struct usb_endpoint_descriptor
16188 +z_iso_ep = {
16189 + .bLength = 0x09,
16190 + .bDescriptorType = 0x05,
16191 + .bEndpointAddress = 0x04,
16192 + .bmAttributes = 0x09,
16193 + .wMaxPacketSize = 0x0038,
16194 + .bInterval = 0x01,
16195 + .bRefresh = 0x00,
16196 + .bSynchAddress = 0x00,
16197 +};
16198 +
16199 +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16200 +
16201 +// 9 bytes
16202 +static char z_ac_interface_header_desc[] =
16203 +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
16204 +
16205 +// 12 bytes
16206 +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
16207 + 0x03, 0x00, 0x00, 0x00};
16208 +// 13 bytes
16209 +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
16210 + 0x02, 0x00, 0x02, 0x00, 0x00};
16211 +// 9 bytes
16212 +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
16213 + 0x00};
16214 +
16215 +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
16216 + 0x00};
16217 +
16218 +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16219 +
16220 +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
16221 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16222 +
16223 +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
16224 + 0x00};
16225 +
16226 +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16227 +
16228 +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
16229 + 0x00};
16230 +
16231 +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16232 +
16233 +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
16234 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16235 +
16236 +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
16237 + 0x00};
16238 +
16239 +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16240 +
16241 +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
16242 + 0x00};
16243 +
16244 +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16245 +
16246 +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
16247 + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16248 +
16249 +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
16250 + 0x00};
16251 +
16252 +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16253 +
16254 +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
16255 + 0x00};
16256 +
16257 +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16258 +
16259 +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
16260 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16261 +
16262 +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
16263 + 0x00};
16264 +
16265 +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16266 +
16267 +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
16268 + 0x00};
16269 +
16270 +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16271 +
16272 +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
16273 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16274 +
16275 +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
16276 + 0x00};
16277 +
16278 +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16279 +
16280 +
16281 +
16282 +static const struct usb_descriptor_header *z_function [] = {
16283 + (struct usb_descriptor_header *) &z_audio_control_if_desc,
16284 + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
16285 + (struct usb_descriptor_header *) &z_0,
16286 + (struct usb_descriptor_header *) &z_1,
16287 + (struct usb_descriptor_header *) &z_2,
16288 + (struct usb_descriptor_header *) &z_audio_if_desc,
16289 + (struct usb_descriptor_header *) &z_audio_if_desc2,
16290 + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
16291 + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
16292 + (struct usb_descriptor_header *) &z_iso_ep,
16293 + (struct usb_descriptor_header *) &z_iso_ep2,
16294 + (struct usb_descriptor_header *) &za_0,
16295 + (struct usb_descriptor_header *) &za_1,
16296 + (struct usb_descriptor_header *) &za_2,
16297 + (struct usb_descriptor_header *) &za_3,
16298 + (struct usb_descriptor_header *) &za_4,
16299 + (struct usb_descriptor_header *) &za_5,
16300 + (struct usb_descriptor_header *) &za_6,
16301 + (struct usb_descriptor_header *) &za_7,
16302 + (struct usb_descriptor_header *) &za_8,
16303 + (struct usb_descriptor_header *) &za_9,
16304 + (struct usb_descriptor_header *) &za_10,
16305 + (struct usb_descriptor_header *) &za_11,
16306 + (struct usb_descriptor_header *) &za_12,
16307 + (struct usb_descriptor_header *) &za_13,
16308 + (struct usb_descriptor_header *) &za_14,
16309 + (struct usb_descriptor_header *) &za_15,
16310 + (struct usb_descriptor_header *) &za_16,
16311 + (struct usb_descriptor_header *) &za_17,
16312 + (struct usb_descriptor_header *) &za_18,
16313 + (struct usb_descriptor_header *) &za_19,
16314 + (struct usb_descriptor_header *) &za_20,
16315 + (struct usb_descriptor_header *) &za_21,
16316 + (struct usb_descriptor_header *) &za_22,
16317 + (struct usb_descriptor_header *) &za_23,
16318 + (struct usb_descriptor_header *) &za_24,
16319 + NULL,
16320 +};
16321 +
16322 +/* maxpacket and other transfer characteristics vary by speed. */
16323 +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
16324 +
16325 +#else
16326 +
16327 +/* if there's no high speed support, maxpacket doesn't change. */
16328 +#define ep_desc(g,hs,fs) fs
16329 +
16330 +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
16331 +
16332 +static char manufacturer [40];
16333 +//static char serial [40];
16334 +static char serial [] = "Ser 00 em";
16335 +
16336 +/* static strings, in UTF-8 */
16337 +static struct usb_string strings [] = {
16338 + { STRING_MANUFACTURER, manufacturer, },
16339 + { STRING_PRODUCT, longname, },
16340 + { STRING_SERIAL, serial, },
16341 + { STRING_LOOPBACK, loopback, },
16342 + { STRING_SOURCE_SINK, source_sink, },
16343 + { } /* end of list */
16344 +};
16345 +
16346 +static struct usb_gadget_strings stringtab = {
16347 + .language = 0x0409, /* en-us */
16348 + .strings = strings,
16349 +};
16350 +
16351 +/*
16352 + * config descriptors are also handcrafted. these must agree with code
16353 + * that sets configurations, and with code managing interfaces and their
16354 + * altsettings. other complexity may come from:
16355 + *
16356 + * - high speed support, including "other speed config" rules
16357 + * - multiple configurations
16358 + * - interfaces with alternate settings
16359 + * - embedded class or vendor-specific descriptors
16360 + *
16361 + * this handles high speed, and has a second config that could as easily
16362 + * have been an alternate interface setting (on most hardware).
16363 + *
16364 + * NOTE: to demonstrate (and test) more USB capabilities, this driver
16365 + * should include an altsetting to test interrupt transfers, including
16366 + * high bandwidth modes at high speed. (Maybe work like Intel's test
16367 + * device?)
16368 + */
16369 +static int
16370 +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
16371 +{
16372 + int len;
16373 + const struct usb_descriptor_header **function;
16374 +
16375 + function = z_function;
16376 + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
16377 + if (len < 0)
16378 + return len;
16379 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
16380 + return len;
16381 +}
16382 +
16383 +/*-------------------------------------------------------------------------*/
16384 +
16385 +static struct usb_request *
16386 +alloc_ep_req (struct usb_ep *ep, unsigned length)
16387 +{
16388 + struct usb_request *req;
16389 +
16390 + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
16391 + if (req) {
16392 + req->length = length;
16393 + req->buf = usb_ep_alloc_buffer (ep, length,
16394 + &req->dma, GFP_ATOMIC);
16395 + if (!req->buf) {
16396 + usb_ep_free_request (ep, req);
16397 + req = NULL;
16398 + }
16399 + }
16400 + return req;
16401 +}
16402 +
16403 +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
16404 +{
16405 + if (req->buf)
16406 + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
16407 + usb_ep_free_request (ep, req);
16408 +}
16409 +
16410 +/*-------------------------------------------------------------------------*/
16411 +
16412 +/* optionally require specific source/sink data patterns */
16413 +
16414 +static int
16415 +check_read_data (
16416 + struct zero_dev *dev,
16417 + struct usb_ep *ep,
16418 + struct usb_request *req
16419 +)
16420 +{
16421 + unsigned i;
16422 + u8 *buf = req->buf;
16423 +
16424 + for (i = 0; i < req->actual; i++, buf++) {
16425 + switch (pattern) {
16426 + /* all-zeroes has no synchronization issues */
16427 + case 0:
16428 + if (*buf == 0)
16429 + continue;
16430 + break;
16431 + /* mod63 stays in sync with short-terminated transfers,
16432 + * or otherwise when host and gadget agree on how large
16433 + * each usb transfer request should be. resync is done
16434 + * with set_interface or set_config.
16435 + */
16436 + case 1:
16437 + if (*buf == (u8)(i % 63))
16438 + continue;
16439 + break;
16440 + }
16441 + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
16442 + usb_ep_set_halt (ep);
16443 + return -EINVAL;
16444 + }
16445 + return 0;
16446 +}
16447 +
16448 +/*-------------------------------------------------------------------------*/
16449 +
16450 +static void zero_reset_config (struct zero_dev *dev)
16451 +{
16452 + if (dev->config == 0)
16453 + return;
16454 +
16455 + DBG (dev, "reset config\n");
16456 +
16457 + /* just disable endpoints, forcing completion of pending i/o.
16458 + * all our completion handlers free their requests in this case.
16459 + */
16460 + if (dev->in_ep) {
16461 + usb_ep_disable (dev->in_ep);
16462 + dev->in_ep = NULL;
16463 + }
16464 + if (dev->out_ep) {
16465 + usb_ep_disable (dev->out_ep);
16466 + dev->out_ep = NULL;
16467 + }
16468 + dev->config = 0;
16469 + del_timer (&dev->resume);
16470 +}
16471 +
16472 +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
16473 +
16474 +static void
16475 +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
16476 +{
16477 + struct zero_dev *dev = ep->driver_data;
16478 + int status = req->status;
16479 + int i, j;
16480 +
16481 + switch (status) {
16482 +
16483 + case 0: /* normal completion? */
16484 + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
16485 + for (i=0, j=rbuf_start; i<req->actual; i++) {
16486 + //printk ("%02x ", ((__u8*)req->buf)[i]);
16487 + rbuf[j] = ((__u8*)req->buf)[i];
16488 + j++;
16489 + if (j >= RBUF_LEN) j=0;
16490 + }
16491 + rbuf_start = j;
16492 + //printk ("\n\n");
16493 +
16494 + if (rbuf_len < RBUF_LEN) {
16495 + rbuf_len += req->actual;
16496 + if (rbuf_len > RBUF_LEN) {
16497 + rbuf_len = RBUF_LEN;
16498 + }
16499 + }
16500 +
16501 + break;
16502 +
16503 + /* this endpoint is normally active while we're configured */
16504 + case -ECONNABORTED: /* hardware forced ep reset */
16505 + case -ECONNRESET: /* request dequeued */
16506 + case -ESHUTDOWN: /* disconnect from host */
16507 + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
16508 + req->actual, req->length);
16509 + if (ep == dev->out_ep)
16510 + check_read_data (dev, ep, req);
16511 + free_ep_req (ep, req);
16512 + return;
16513 +
16514 + case -EOVERFLOW: /* buffer overrun on read means that
16515 + * we didn't provide a big enough
16516 + * buffer.
16517 + */
16518 + default:
16519 +#if 1
16520 + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
16521 + status, req->actual, req->length);
16522 +#endif
16523 + case -EREMOTEIO: /* short read */
16524 + break;
16525 + }
16526 +
16527 + status = usb_ep_queue (ep, req, GFP_ATOMIC);
16528 + if (status) {
16529 + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
16530 + ep->name, req->length, status);
16531 + usb_ep_set_halt (ep);
16532 + /* FIXME recover later ... somehow */
16533 + }
16534 +}
16535 +
16536 +static struct usb_request *
16537 +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
16538 +{
16539 + struct usb_request *req;
16540 + int status;
16541 +
16542 + req = alloc_ep_req (ep, 512);
16543 + if (!req)
16544 + return NULL;
16545 +
16546 + req->complete = zero_isoc_complete;
16547 +
16548 + status = usb_ep_queue (ep, req, gfp_flags);
16549 + if (status) {
16550 + struct zero_dev *dev = ep->driver_data;
16551 +
16552 + ERROR (dev, "start %s --> %d\n", ep->name, status);
16553 + free_ep_req (ep, req);
16554 + req = NULL;
16555 + }
16556 +
16557 + return req;
16558 +}
16559 +
16560 +/* change our operational config. this code must agree with the code
16561 + * that returns config descriptors, and altsetting code.
16562 + *
16563 + * it's also responsible for power management interactions. some
16564 + * configurations might not work with our current power sources.
16565 + *
16566 + * note that some device controller hardware will constrain what this
16567 + * code can do, perhaps by disallowing more than one configuration or
16568 + * by limiting configuration choices (like the pxa2xx).
16569 + */
16570 +static int
16571 +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
16572 +{
16573 + int result = 0;
16574 + struct usb_gadget *gadget = dev->gadget;
16575 + const struct usb_endpoint_descriptor *d;
16576 + struct usb_ep *ep;
16577 +
16578 + if (number == dev->config)
16579 + return 0;
16580 +
16581 + zero_reset_config (dev);
16582 +
16583 + gadget_for_each_ep (ep, gadget) {
16584 +
16585 + if (strcmp (ep->name, "ep4") == 0) {
16586 +
16587 + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
16588 + result = usb_ep_enable (ep, d);
16589 +
16590 + if (result == 0) {
16591 + ep->driver_data = dev;
16592 + dev->in_ep = ep;
16593 +
16594 + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
16595 +
16596 + dev->in_ep = ep;
16597 + continue;
16598 + }
16599 +
16600 + usb_ep_disable (ep);
16601 + result = -EIO;
16602 + }
16603 + }
16604 +
16605 + }
16606 +
16607 + dev->config = number;
16608 + return result;
16609 +}
16610 +
16611 +/*-------------------------------------------------------------------------*/
16612 +
16613 +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
16614 +{
16615 + if (req->status || req->actual != req->length)
16616 + DBG ((struct zero_dev *) ep->driver_data,
16617 + "setup complete --> %d, %d/%d\n",
16618 + req->status, req->actual, req->length);
16619 +}
16620 +
16621 +/*
16622 + * The setup() callback implements all the ep0 functionality that's
16623 + * not handled lower down, in hardware or the hardware driver (like
16624 + * device and endpoint feature flags, and their status). It's all
16625 + * housekeeping for the gadget function we're implementing. Most of
16626 + * the work is in config-specific setup.
16627 + */
16628 +static int
16629 +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
16630 +{
16631 + struct zero_dev *dev = get_gadget_data (gadget);
16632 + struct usb_request *req = dev->req;
16633 + int value = -EOPNOTSUPP;
16634 +
16635 + /* usually this stores reply data in the pre-allocated ep0 buffer,
16636 + * but config change events will reconfigure hardware.
16637 + */
16638 + req->zero = 0;
16639 + switch (ctrl->bRequest) {
16640 +
16641 + case USB_REQ_GET_DESCRIPTOR:
16642 +
16643 + switch (ctrl->wValue >> 8) {
16644 +
16645 + case USB_DT_DEVICE:
16646 + value = min (ctrl->wLength, (u16) sizeof device_desc);
16647 + memcpy (req->buf, &device_desc, value);
16648 + break;
16649 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16650 + case USB_DT_DEVICE_QUALIFIER:
16651 + if (!gadget->is_dualspeed)
16652 + break;
16653 + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
16654 + memcpy (req->buf, &dev_qualifier, value);
16655 + break;
16656 +
16657 + case USB_DT_OTHER_SPEED_CONFIG:
16658 + if (!gadget->is_dualspeed)
16659 + break;
16660 + // FALLTHROUGH
16661 +#endif /* CONFIG_USB_GADGET_DUALSPEED */
16662 + case USB_DT_CONFIG:
16663 + value = config_buf (gadget, req->buf,
16664 + ctrl->wValue >> 8,
16665 + ctrl->wValue & 0xff);
16666 + if (value >= 0)
16667 + value = min (ctrl->wLength, (u16) value);
16668 + break;
16669 +
16670 + case USB_DT_STRING:
16671 + /* wIndex == language code.
16672 + * this driver only handles one language, you can
16673 + * add string tables for other languages, using
16674 + * any UTF-8 characters
16675 + */
16676 + value = usb_gadget_get_string (&stringtab,
16677 + ctrl->wValue & 0xff, req->buf);
16678 + if (value >= 0) {
16679 + value = min (ctrl->wLength, (u16) value);
16680 + }
16681 + break;
16682 + }
16683 + break;
16684 +
16685 + /* currently two configs, two speeds */
16686 + case USB_REQ_SET_CONFIGURATION:
16687 + if (ctrl->bRequestType != 0)
16688 + goto unknown;
16689 +
16690 + spin_lock (&dev->lock);
16691 + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
16692 + spin_unlock (&dev->lock);
16693 + break;
16694 + case USB_REQ_GET_CONFIGURATION:
16695 + if (ctrl->bRequestType != USB_DIR_IN)
16696 + goto unknown;
16697 + *(u8 *)req->buf = dev->config;
16698 + value = min (ctrl->wLength, (u16) 1);
16699 + break;
16700 +
16701 + /* until we add altsetting support, or other interfaces,
16702 + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
16703 + * and already killed pending endpoint I/O.
16704 + */
16705 + case USB_REQ_SET_INTERFACE:
16706 +
16707 + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
16708 + goto unknown;
16709 + spin_lock (&dev->lock);
16710 + if (dev->config) {
16711 + u8 config = dev->config;
16712 +
16713 + /* resets interface configuration, forgets about
16714 + * previous transaction state (queued bufs, etc)
16715 + * and re-inits endpoint state (toggle etc)
16716 + * no response queued, just zero status == success.
16717 + * if we had more than one interface we couldn't
16718 + * use this "reset the config" shortcut.
16719 + */
16720 + zero_reset_config (dev);
16721 + zero_set_config (dev, config, GFP_ATOMIC);
16722 + value = 0;
16723 + }
16724 + spin_unlock (&dev->lock);
16725 + break;
16726 + case USB_REQ_GET_INTERFACE:
16727 + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
16728 + value = ctrl->wLength;
16729 + break;
16730 + }
16731 + else {
16732 + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
16733 + goto unknown;
16734 + if (!dev->config)
16735 + break;
16736 + if (ctrl->wIndex != 0) {
16737 + value = -EDOM;
16738 + break;
16739 + }
16740 + *(u8 *)req->buf = 0;
16741 + value = min (ctrl->wLength, (u16) 1);
16742 + }
16743 + break;
16744 +
16745 + /*
16746 + * These are the same vendor-specific requests supported by
16747 + * Intel's USB 2.0 compliance test devices. We exceed that
16748 + * device spec by allowing multiple-packet requests.
16749 + */
16750 + case 0x5b: /* control WRITE test -- fill the buffer */
16751 + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
16752 + goto unknown;
16753 + if (ctrl->wValue || ctrl->wIndex)
16754 + break;
16755 + /* just read that many bytes into the buffer */
16756 + if (ctrl->wLength > USB_BUFSIZ)
16757 + break;
16758 + value = ctrl->wLength;
16759 + break;
16760 + case 0x5c: /* control READ test -- return the buffer */
16761 + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
16762 + goto unknown;
16763 + if (ctrl->wValue || ctrl->wIndex)
16764 + break;
16765 + /* expect those bytes are still in the buffer; send back */
16766 + if (ctrl->wLength > USB_BUFSIZ
16767 + || ctrl->wLength != req->length)
16768 + break;
16769 + value = ctrl->wLength;
16770 + break;
16771 +
16772 + case 0x01: // SET_CUR
16773 + case 0x02:
16774 + case 0x03:
16775 + case 0x04:
16776 + case 0x05:
16777 + value = ctrl->wLength;
16778 + break;
16779 + case 0x81:
16780 + switch (ctrl->wValue) {
16781 + case 0x0201:
16782 + case 0x0202:
16783 + ((u8*)req->buf)[0] = 0x00;
16784 + ((u8*)req->buf)[1] = 0xe3;
16785 + break;
16786 + case 0x0300:
16787 + case 0x0500:
16788 + ((u8*)req->buf)[0] = 0x00;
16789 + break;
16790 + }
16791 + //((u8*)req->buf)[0] = 0x81;
16792 + //((u8*)req->buf)[1] = 0x81;
16793 + value = ctrl->wLength;
16794 + break;
16795 + case 0x82:
16796 + switch (ctrl->wValue) {
16797 + case 0x0201:
16798 + case 0x0202:
16799 + ((u8*)req->buf)[0] = 0x00;
16800 + ((u8*)req->buf)[1] = 0xc3;
16801 + break;
16802 + case 0x0300:
16803 + case 0x0500:
16804 + ((u8*)req->buf)[0] = 0x00;
16805 + break;
16806 + }
16807 + //((u8*)req->buf)[0] = 0x82;
16808 + //((u8*)req->buf)[1] = 0x82;
16809 + value = ctrl->wLength;
16810 + break;
16811 + case 0x83:
16812 + switch (ctrl->wValue) {
16813 + case 0x0201:
16814 + case 0x0202:
16815 + ((u8*)req->buf)[0] = 0x00;
16816 + ((u8*)req->buf)[1] = 0x00;
16817 + break;
16818 + case 0x0300:
16819 + ((u8*)req->buf)[0] = 0x60;
16820 + break;
16821 + case 0x0500:
16822 + ((u8*)req->buf)[0] = 0x18;
16823 + break;
16824 + }
16825 + //((u8*)req->buf)[0] = 0x83;
16826 + //((u8*)req->buf)[1] = 0x83;
16827 + value = ctrl->wLength;
16828 + break;
16829 + case 0x84:
16830 + switch (ctrl->wValue) {
16831 + case 0x0201:
16832 + case 0x0202:
16833 + ((u8*)req->buf)[0] = 0x00;
16834 + ((u8*)req->buf)[1] = 0x01;
16835 + break;
16836 + case 0x0300:
16837 + case 0x0500:
16838 + ((u8*)req->buf)[0] = 0x08;
16839 + break;
16840 + }
16841 + //((u8*)req->buf)[0] = 0x84;
16842 + //((u8*)req->buf)[1] = 0x84;
16843 + value = ctrl->wLength;
16844 + break;
16845 + case 0x85:
16846 + ((u8*)req->buf)[0] = 0x85;
16847 + ((u8*)req->buf)[1] = 0x85;
16848 + value = ctrl->wLength;
16849 + break;
16850 +
16851 +
16852 + default:
16853 +unknown:
16854 + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
16855 + ctrl->bRequestType, ctrl->bRequest,
16856 + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
16857 + }
16858 +
16859 + /* respond with data transfer before status phase? */
16860 + if (value >= 0) {
16861 + req->length = value;
16862 + req->zero = value < ctrl->wLength
16863 + && (value % gadget->ep0->maxpacket) == 0;
16864 + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
16865 + if (value < 0) {
16866 + DBG (dev, "ep_queue < 0 --> %d\n", value);
16867 + req->status = 0;
16868 + zero_setup_complete (gadget->ep0, req);
16869 + }
16870 + }
16871 +
16872 + /* device either stalls (value < 0) or reports success */
16873 + return value;
16874 +}
16875 +
16876 +static void
16877 +zero_disconnect (struct usb_gadget *gadget)
16878 +{
16879 + struct zero_dev *dev = get_gadget_data (gadget);
16880 + unsigned long flags;
16881 +
16882 + spin_lock_irqsave (&dev->lock, flags);
16883 + zero_reset_config (dev);
16884 +
16885 + /* a more significant application might have some non-usb
16886 + * activities to quiesce here, saving resources like power
16887 + * or pushing the notification up a network stack.
16888 + */
16889 + spin_unlock_irqrestore (&dev->lock, flags);
16890 +
16891 + /* next we may get setup() calls to enumerate new connections;
16892 + * or an unbind() during shutdown (including removing module).
16893 + */
16894 +}
16895 +
16896 +static void
16897 +zero_autoresume (unsigned long _dev)
16898 +{
16899 + struct zero_dev *dev = (struct zero_dev *) _dev;
16900 + int status;
16901 +
16902 + /* normally the host would be woken up for something
16903 + * more significant than just a timer firing...
16904 + */
16905 + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
16906 + status = usb_gadget_wakeup (dev->gadget);
16907 + DBG (dev, "wakeup --> %d\n", status);
16908 + }
16909 +}
16910 +
16911 +/*-------------------------------------------------------------------------*/
16912 +
16913 +static void
16914 +zero_unbind (struct usb_gadget *gadget)
16915 +{
16916 + struct zero_dev *dev = get_gadget_data (gadget);
16917 +
16918 + DBG (dev, "unbind\n");
16919 +
16920 + /* we've already been disconnected ... no i/o is active */
16921 + if (dev->req)
16922 + free_ep_req (gadget->ep0, dev->req);
16923 + del_timer_sync (&dev->resume);
16924 + kfree (dev);
16925 + set_gadget_data (gadget, NULL);
16926 +}
16927 +
16928 +static int
16929 +zero_bind (struct usb_gadget *gadget)
16930 +{
16931 + struct zero_dev *dev;
16932 + //struct usb_ep *ep;
16933 +
16934 + printk("binding\n");
16935 + /*
16936 + * DRIVER POLICY CHOICE: you may want to do this differently.
16937 + * One thing to avoid is reusing a bcdDevice revision code
16938 + * with different host-visible configurations or behavior
16939 + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
16940 + */
16941 + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
16942 +
16943 +
16944 + /* ok, we made sense of the hardware ... */
16945 + dev = kzalloc (sizeof *dev, SLAB_KERNEL);
16946 + if (!dev)
16947 + return -ENOMEM;
16948 + spin_lock_init (&dev->lock);
16949 + dev->gadget = gadget;
16950 + set_gadget_data (gadget, dev);
16951 +
16952 + /* preallocate control response and buffer */
16953 + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
16954 + if (!dev->req)
16955 + goto enomem;
16956 + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
16957 + &dev->req->dma, GFP_KERNEL);
16958 + if (!dev->req->buf)
16959 + goto enomem;
16960 +
16961 + dev->req->complete = zero_setup_complete;
16962 +
16963 + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
16964 +
16965 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16966 + /* assume ep0 uses the same value for both speeds ... */
16967 + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
16968 +
16969 + /* and that all endpoints are dual-speed */
16970 + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
16971 + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
16972 +#endif
16973 +
16974 + usb_gadget_set_selfpowered (gadget);
16975 +
16976 + init_timer (&dev->resume);
16977 + dev->resume.function = zero_autoresume;
16978 + dev->resume.data = (unsigned long) dev;
16979 +
16980 + gadget->ep0->driver_data = dev;
16981 +
16982 + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
16983 + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
16984 + EP_OUT_NAME, EP_IN_NAME);
16985 +
16986 + snprintf (manufacturer, sizeof manufacturer,
16987 + UTS_SYSNAME " " UTS_RELEASE " with %s",
16988 + gadget->name);
16989 +
16990 + return 0;
16991 +
16992 +enomem:
16993 + zero_unbind (gadget);
16994 + return -ENOMEM;
16995 +}
16996 +
16997 +/*-------------------------------------------------------------------------*/
16998 +
16999 +static void
17000 +zero_suspend (struct usb_gadget *gadget)
17001 +{
17002 + struct zero_dev *dev = get_gadget_data (gadget);
17003 +
17004 + if (gadget->speed == USB_SPEED_UNKNOWN)
17005 + return;
17006 +
17007 + if (autoresume) {
17008 + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
17009 + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
17010 + } else
17011 + DBG (dev, "suspend\n");
17012 +}
17013 +
17014 +static void
17015 +zero_resume (struct usb_gadget *gadget)
17016 +{
17017 + struct zero_dev *dev = get_gadget_data (gadget);
17018 +
17019 + DBG (dev, "resume\n");
17020 + del_timer (&dev->resume);
17021 +}
17022 +
17023 +
17024 +/*-------------------------------------------------------------------------*/
17025 +
17026 +static struct usb_gadget_driver zero_driver = {
17027 +#ifdef CONFIG_USB_GADGET_DUALSPEED
17028 + .speed = USB_SPEED_HIGH,
17029 +#else
17030 + .speed = USB_SPEED_FULL,
17031 +#endif
17032 + .function = (char *) longname,
17033 + .bind = zero_bind,
17034 + .unbind = zero_unbind,
17035 +
17036 + .setup = zero_setup,
17037 + .disconnect = zero_disconnect,
17038 +
17039 + .suspend = zero_suspend,
17040 + .resume = zero_resume,
17041 +
17042 + .driver = {
17043 + .name = (char *) shortname,
17044 + // .shutdown = ...
17045 + // .suspend = ...
17046 + // .resume = ...
17047 + },
17048 +};
17049 +
17050 +MODULE_AUTHOR ("David Brownell");
17051 +MODULE_LICENSE ("Dual BSD/GPL");
17052 +
17053 +static struct proc_dir_entry *pdir, *pfile;
17054 +
17055 +static int isoc_read_data (char *page, char **start,
17056 + off_t off, int count,
17057 + int *eof, void *data)
17058 +{
17059 + int i;
17060 + static int c = 0;
17061 + static int done = 0;
17062 + static int s = 0;
17063 +
17064 +/*
17065 + printk ("\ncount: %d\n", count);
17066 + printk ("rbuf_start: %d\n", rbuf_start);
17067 + printk ("rbuf_len: %d\n", rbuf_len);
17068 + printk ("off: %d\n", off);
17069 + printk ("start: %p\n\n", *start);
17070 +*/
17071 + if (done) {
17072 + c = 0;
17073 + done = 0;
17074 + *eof = 1;
17075 + return 0;
17076 + }
17077 +
17078 + if (c == 0) {
17079 + if (rbuf_len == RBUF_LEN)
17080 + s = rbuf_start;
17081 + else s = 0;
17082 + }
17083 +
17084 + for (i=0; i<count && c<rbuf_len; i++, c++) {
17085 + page[i] = rbuf[(c+s) % RBUF_LEN];
17086 + }
17087 + *start = page;
17088 +
17089 + if (c >= rbuf_len) {
17090 + *eof = 1;
17091 + done = 1;
17092 + }
17093 +
17094 +
17095 + return i;
17096 +}
17097 +
17098 +static int __init init (void)
17099 +{
17100 +
17101 + int retval = 0;
17102 +
17103 + pdir = proc_mkdir("isoc_test", NULL);
17104 + if(pdir == NULL) {
17105 + retval = -ENOMEM;
17106 + printk("Error creating dir\n");
17107 + goto done;
17108 + }
17109 + pdir->owner = THIS_MODULE;
17110 +
17111 + pfile = create_proc_read_entry("isoc_data",
17112 + 0444, pdir,
17113 + isoc_read_data,
17114 + NULL);
17115 + if (pfile == NULL) {
17116 + retval = -ENOMEM;
17117 + printk("Error creating file\n");
17118 + goto no_file;
17119 + }
17120 + pfile->owner = THIS_MODULE;
17121 +
17122 + return usb_gadget_register_driver (&zero_driver);
17123 +
17124 + no_file:
17125 + remove_proc_entry("isoc_data", NULL);
17126 + done:
17127 + return retval;
17128 +}
17129 +module_init (init);
17130 +
17131 +static void __exit cleanup (void)
17132 +{
17133 +
17134 + usb_gadget_unregister_driver (&zero_driver);
17135 +
17136 + remove_proc_entry("isoc_data", pdir);
17137 + remove_proc_entry("isoc_test", NULL);
17138 +}
17139 +module_exit (cleanup);
17140 --- /dev/null
17141 +++ b/drivers/usb/host/dwc_otg/dwc_cfi_common.h
17142 @@ -0,0 +1,142 @@
17143 +/* ==========================================================================
17144 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
17145 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
17146 + * otherwise expressly agreed to in writing between Synopsys and you.
17147 + *
17148 + * The Software IS NOT an item of Licensed Software or Licensed Product under
17149 + * any End User Software License Agreement or Agreement for Licensed Product
17150 + * with Synopsys or any supplement thereto. You are permitted to use and
17151 + * redistribute this Software in source and binary forms, with or without
17152 + * modification, provided that redistributions of source code must retain this
17153 + * notice. You may not view, use, disclose, copy or distribute this file or
17154 + * any information contained herein except pursuant to this license grant from
17155 + * Synopsys. If you do not agree with this notice, including the disclaimer
17156 + * below, then you are not authorized to use the Software.
17157 + *
17158 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
17159 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17160 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17161 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
17162 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17163 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17164 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17165 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17166 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17167 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
17168 + * DAMAGE.
17169 + * ========================================================================== */
17170 +
17171 +#if !defined(__DWC_CFI_COMMON_H__)
17172 +#define __DWC_CFI_COMMON_H__
17173 +
17174 +//#include <linux/types.h>
17175 +
17176 +/**
17177 + * @file
17178 + *
17179 + * This file contains the CFI specific common constants, interfaces
17180 + * (functions and macros) and structures for Linux. No PCD specific
17181 + * data structure or definition is to be included in this file.
17182 + *
17183 + */
17184 +
17185 +/** This is a request for all Core Features */
17186 +#define VEN_CORE_GET_FEATURES 0xB1
17187 +
17188 +/** This is a request to get the value of a specific Core Feature */
17189 +#define VEN_CORE_GET_FEATURE 0xB2
17190 +
17191 +/** This command allows the host to set the value of a specific Core Feature */
17192 +#define VEN_CORE_SET_FEATURE 0xB3
17193 +
17194 +/** This command allows the host to set the default values of
17195 + * either all or any specific Core Feature
17196 + */
17197 +#define VEN_CORE_RESET_FEATURES 0xB4
17198 +
17199 +/** This command forces the PCD to write the deferred values of a Core Features */
17200 +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
17201 +
17202 +/** This request reads a DWORD value from a register at the specified offset */
17203 +#define VEN_CORE_READ_REGISTER 0xB6
17204 +
17205 +/** This request writes a DWORD value into a register at the specified offset */
17206 +#define VEN_CORE_WRITE_REGISTER 0xB7
17207 +
17208 +/** This structure is the header of the Core Features dataset returned to
17209 + * the Host
17210 + */
17211 +struct cfi_all_features_header {
17212 +/** The features header structure length is */
17213 +#define CFI_ALL_FEATURES_HDR_LEN 8
17214 + /**
17215 + * The total length of the features dataset returned to the Host
17216 + */
17217 + uint16_t wTotalLen;
17218 +
17219 + /**
17220 + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
17221 + * This field identifies the version of the CFI Specification with which
17222 + * the device is compliant.
17223 + */
17224 + uint16_t wVersion;
17225 +
17226 + /** The ID of the Core */
17227 + uint16_t wCoreID;
17228 +#define CFI_CORE_ID_UDC 1
17229 +#define CFI_CORE_ID_OTG 2
17230 +#define CFI_CORE_ID_WUDEV 3
17231 +
17232 + /** Number of features returned by VEN_CORE_GET_FEATURES request */
17233 + uint16_t wNumFeatures;
17234 +} UPACKED;
17235 +
17236 +typedef struct cfi_all_features_header cfi_all_features_header_t;
17237 +
17238 +/** This structure is a header of the Core Feature descriptor dataset returned to
17239 + * the Host after the VEN_CORE_GET_FEATURES request
17240 + */
17241 +struct cfi_feature_desc_header {
17242 +#define CFI_FEATURE_DESC_HDR_LEN 8
17243 +
17244 + /** The feature ID */
17245 + uint16_t wFeatureID;
17246 +
17247 + /** Length of this feature descriptor in bytes - including the
17248 + * length of the feature name string
17249 + */
17250 + uint16_t wLength;
17251 +
17252 + /** The data length of this feature in bytes */
17253 + uint16_t wDataLength;
17254 +
17255 + /**
17256 + * Attributes of this features
17257 + * D0: Access rights
17258 + * 0 - Read/Write
17259 + * 1 - Read only
17260 + */
17261 + uint8_t bmAttributes;
17262 +#define CFI_FEATURE_ATTR_RO 1
17263 +#define CFI_FEATURE_ATTR_RW 0
17264 +
17265 + /** Length of the feature name in bytes */
17266 + uint8_t bNameLen;
17267 +
17268 + /** The feature name buffer */
17269 + //uint8_t *name;
17270 +} UPACKED;
17271 +
17272 +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
17273 +
17274 +/**
17275 + * This structure describes a NULL terminated string referenced by its id field.
17276 + * It is very similar to usb_string structure but has the id field type set to 16-bit.
17277 + */
17278 +struct cfi_string {
17279 + uint16_t id;
17280 + const uint8_t *s;
17281 +};
17282 +typedef struct cfi_string cfi_string_t;
17283 +
17284 +#endif
17285 --- /dev/null
17286 +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.c
17287 @@ -0,0 +1,854 @@
17288 +/* ==========================================================================
17289 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
17290 + * $Revision: #12 $
17291 + * $Date: 2011/10/26 $
17292 + * $Change: 1873028 $
17293 + *
17294 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
17295 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
17296 + * otherwise expressly agreed to in writing between Synopsys and you.
17297 + *
17298 + * The Software IS NOT an item of Licensed Software or Licensed Product under
17299 + * any End User Software License Agreement or Agreement for Licensed Product
17300 + * with Synopsys or any supplement thereto. You are permitted to use and
17301 + * redistribute this Software in source and binary forms, with or without
17302 + * modification, provided that redistributions of source code must retain this
17303 + * notice. You may not view, use, disclose, copy or distribute this file or
17304 + * any information contained herein except pursuant to this license grant from
17305 + * Synopsys. If you do not agree with this notice, including the disclaimer
17306 + * below, then you are not authorized to use the Software.
17307 + *
17308 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
17309 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17310 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17311 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
17312 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17313 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17314 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17315 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17316 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17317 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
17318 + * DAMAGE.
17319 + * ========================================================================== */
17320 +
17321 +#include "dwc_os.h"
17322 +#include "dwc_otg_regs.h"
17323 +#include "dwc_otg_cil.h"
17324 +#include "dwc_otg_adp.h"
17325 +
17326 +/** @file
17327 + *
17328 + * This file contains the most of the Attach Detect Protocol implementation for
17329 + * the driver to support OTG Rev2.0.
17330 + *
17331 + */
17332 +
17333 +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
17334 +{
17335 + adpctl_data_t adpctl;
17336 +
17337 + adpctl.d32 = value;
17338 + adpctl.b.ar = 0x2;
17339 +
17340 + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
17341 +
17342 + while (adpctl.b.ar) {
17343 + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
17344 + }
17345 +
17346 +}
17347 +
17348 +/**
17349 + * Function is called to read ADP registers
17350 + */
17351 +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
17352 +{
17353 + adpctl_data_t adpctl;
17354 +
17355 + adpctl.d32 = 0;
17356 + adpctl.b.ar = 0x1;
17357 +
17358 + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
17359 +
17360 + while (adpctl.b.ar) {
17361 + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
17362 + }
17363 +
17364 + return adpctl.d32;
17365 +}
17366 +
17367 +/**
17368 + * Function is called to read ADPCTL register and filter Write-clear bits
17369 + */
17370 +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
17371 +{
17372 + adpctl_data_t adpctl;
17373 +
17374 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17375 + adpctl.b.adp_tmout_int = 0;
17376 + adpctl.b.adp_prb_int = 0;
17377 + adpctl.b.adp_tmout_int = 0;
17378 +
17379 + return adpctl.d32;
17380 +}
17381 +
17382 +/**
17383 + * Function is called to write ADP registers
17384 + */
17385 +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
17386 + uint32_t set)
17387 +{
17388 + dwc_otg_adp_write_reg(core_if,
17389 + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
17390 +}
17391 +
17392 +static void adp_sense_timeout(void *ptr)
17393 +{
17394 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
17395 + core_if->adp.sense_timer_started = 0;
17396 + DWC_PRINTF("ADP SENSE TIMEOUT\n");
17397 + if (core_if->adp_enable) {
17398 + dwc_otg_adp_sense_stop(core_if);
17399 + dwc_otg_adp_probe_start(core_if);
17400 + }
17401 +}
17402 +
17403 +/**
17404 + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
17405 + */
17406 +static void adp_vbuson_timeout(void *ptr)
17407 +{
17408 + gpwrdn_data_t gpwrdn;
17409 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
17410 + hprt0_data_t hprt0 = {.d32 = 0 };
17411 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
17412 + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
17413 + if (core_if) {
17414 + core_if->adp.vbuson_timer_started = 0;
17415 + /* Turn off vbus */
17416 + hprt0.b.prtpwr = 1;
17417 + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
17418 + gpwrdn.d32 = 0;
17419 +
17420 + /* Power off the core */
17421 + if (core_if->power_down == 2) {
17422 + /* Enable Wakeup Logic */
17423 +// gpwrdn.b.wkupactiv = 1;
17424 + gpwrdn.b.pmuactv = 0;
17425 + gpwrdn.b.pwrdnrstn = 1;
17426 + gpwrdn.b.pwrdnclmp = 1;
17427 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17428 + gpwrdn.d32);
17429 +
17430 + /* Suspend the Phy Clock */
17431 + pcgcctl.b.stoppclk = 1;
17432 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
17433 +
17434 + /* Switch on VDD */
17435 +// gpwrdn.b.wkupactiv = 1;
17436 + gpwrdn.b.pmuactv = 1;
17437 + gpwrdn.b.pwrdnrstn = 1;
17438 + gpwrdn.b.pwrdnclmp = 1;
17439 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17440 + gpwrdn.d32);
17441 + } else {
17442 + /* Enable Power Down Logic */
17443 + gpwrdn.b.pmuintsel = 1;
17444 + gpwrdn.b.pmuactv = 1;
17445 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17446 + }
17447 +
17448 + /* Power off the core */
17449 + if (core_if->power_down == 2) {
17450 + gpwrdn.d32 = 0;
17451 + gpwrdn.b.pwrdnswtch = 1;
17452 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
17453 + gpwrdn.d32, 0);
17454 + }
17455 +
17456 + /* Unmask SRP detected interrupt from Power Down Logic */
17457 + gpwrdn.d32 = 0;
17458 + gpwrdn.b.srp_det_msk = 1;
17459 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17460 +
17461 + dwc_otg_adp_probe_start(core_if);
17462 + dwc_otg_dump_global_registers(core_if);
17463 + dwc_otg_dump_host_registers(core_if);
17464 + }
17465 +
17466 +}
17467 +
17468 +/**
17469 + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
17470 + * not asserted within 1.1 seconds.
17471 + *
17472 + * @param core_if the pointer to core_if strucure.
17473 + */
17474 +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
17475 +{
17476 + core_if->adp.vbuson_timer_started = 1;
17477 + if (core_if->adp.vbuson_timer)
17478 + {
17479 + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
17480 + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
17481 + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
17482 + } else {
17483 + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
17484 + }
17485 +}
17486 +
17487 +#if 0
17488 +/**
17489 + * Masks all DWC OTG core interrupts
17490 + *
17491 + */
17492 +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
17493 +{
17494 + int i;
17495 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
17496 +
17497 + /* Mask Host Interrupts */
17498 +
17499 + /* Clear and disable HCINTs */
17500 + for (i = 0; i < core_if->core_params->host_channels; i++) {
17501 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
17502 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
17503 +
17504 + }
17505 +
17506 + /* Clear and disable HAINT */
17507 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
17508 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
17509 +
17510 + /* Mask Device Interrupts */
17511 + if (!core_if->multiproc_int_enable) {
17512 + /* Clear and disable IN Endpoint interrupts */
17513 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
17514 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
17515 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
17516 + diepint, 0xFFFFFFFF);
17517 + }
17518 +
17519 + /* Clear and disable OUT Endpoint interrupts */
17520 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
17521 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
17522 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
17523 + doepint, 0xFFFFFFFF);
17524 + }
17525 +
17526 + /* Clear and disable DAINT */
17527 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
17528 + 0xFFFFFFFF);
17529 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
17530 + } else {
17531 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
17532 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
17533 + diepeachintmsk[i], 0);
17534 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
17535 + diepint, 0xFFFFFFFF);
17536 + }
17537 +
17538 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
17539 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
17540 + doepeachintmsk[i], 0);
17541 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
17542 + doepint, 0xFFFFFFFF);
17543 + }
17544 +
17545 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
17546 + 0);
17547 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
17548 + 0xFFFFFFFF);
17549 +
17550 + }
17551 +
17552 + /* Disable interrupts */
17553 + ahbcfg.b.glblintrmsk = 1;
17554 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
17555 +
17556 + /* Disable all interrupts. */
17557 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
17558 +
17559 + /* Clear any pending interrupts */
17560 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17561 +
17562 + /* Clear any pending OTG Interrupts */
17563 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
17564 +}
17565 +
17566 +/**
17567 + * Unmask Port Connection Detected interrupt
17568 + *
17569 + */
17570 +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
17571 +{
17572 + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
17573 +
17574 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
17575 +}
17576 +#endif
17577 +
17578 +/**
17579 + * Starts the ADP Probing
17580 + *
17581 + * @param core_if the pointer to core_if structure.
17582 + */
17583 +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
17584 +{
17585 +
17586 + adpctl_data_t adpctl = {.d32 = 0};
17587 + gpwrdn_data_t gpwrdn;
17588 +#if 0
17589 + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
17590 + .b.adp_sns_int = 1, b.adp_tmout_int};
17591 +#endif
17592 + dwc_otg_disable_global_interrupts(core_if);
17593 + DWC_PRINTF("ADP Probe Start\n");
17594 + core_if->adp.probe_enabled = 1;
17595 +
17596 + adpctl.b.adpres = 1;
17597 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17598 +
17599 + while (adpctl.b.adpres) {
17600 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17601 + }
17602 +
17603 + adpctl.d32 = 0;
17604 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17605 +
17606 + /* In Host mode unmask SRP detected interrupt */
17607 + gpwrdn.d32 = 0;
17608 + gpwrdn.b.sts_chngint_msk = 1;
17609 + if (!gpwrdn.b.idsts) {
17610 + gpwrdn.b.srp_det_msk = 1;
17611 + }
17612 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17613 +
17614 + adpctl.b.adp_tmout_int_msk = 1;
17615 + adpctl.b.adp_prb_int_msk = 1;
17616 + adpctl.b.prb_dschg = 1;
17617 + adpctl.b.prb_delta = 1;
17618 + adpctl.b.prb_per = 1;
17619 + adpctl.b.adpen = 1;
17620 + adpctl.b.enaprb = 1;
17621 +
17622 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17623 + DWC_PRINTF("ADP Probe Finish\n");
17624 + return 0;
17625 +}
17626 +
17627 +/**
17628 + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
17629 + * within 3 seconds.
17630 + *
17631 + * @param core_if the pointer to core_if strucure.
17632 + */
17633 +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
17634 +{
17635 + core_if->adp.sense_timer_started = 1;
17636 + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
17637 +}
17638 +
17639 +/**
17640 + * Starts the ADP Sense
17641 + *
17642 + * @param core_if the pointer to core_if strucure.
17643 + */
17644 +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
17645 +{
17646 + adpctl_data_t adpctl;
17647 +
17648 + DWC_PRINTF("ADP Sense Start\n");
17649 +
17650 + /* Unmask ADP sense interrupt and mask all other from the core */
17651 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17652 + adpctl.b.adp_sns_int_msk = 1;
17653 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17654 + dwc_otg_disable_global_interrupts(core_if); // vahrama
17655 +
17656 + /* Set ADP reset bit*/
17657 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17658 + adpctl.b.adpres = 1;
17659 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17660 +
17661 + while (adpctl.b.adpres) {
17662 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17663 + }
17664 +
17665 + adpctl.b.adpres = 0;
17666 + adpctl.b.adpen = 1;
17667 + adpctl.b.enasns = 1;
17668 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17669 +
17670 + dwc_otg_adp_sense_timer_start(core_if);
17671 +
17672 + return 0;
17673 +}
17674 +
17675 +/**
17676 + * Stops the ADP Probing
17677 + *
17678 + * @param core_if the pointer to core_if strucure.
17679 + */
17680 +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
17681 +{
17682 +
17683 + adpctl_data_t adpctl;
17684 + DWC_PRINTF("Stop ADP probe\n");
17685 + core_if->adp.probe_enabled = 0;
17686 + core_if->adp.probe_counter = 0;
17687 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17688 +
17689 + adpctl.b.adpen = 0;
17690 + adpctl.b.adp_prb_int = 1;
17691 + adpctl.b.adp_tmout_int = 1;
17692 + adpctl.b.adp_sns_int = 1;
17693 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17694 +
17695 + return 0;
17696 +}
17697 +
17698 +/**
17699 + * Stops the ADP Sensing
17700 + *
17701 + * @param core_if the pointer to core_if strucure.
17702 + */
17703 +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
17704 +{
17705 + adpctl_data_t adpctl;
17706 +
17707 + core_if->adp.sense_enabled = 0;
17708 +
17709 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17710 + adpctl.b.enasns = 0;
17711 + adpctl.b.adp_sns_int = 1;
17712 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17713 +
17714 + return 0;
17715 +}
17716 +
17717 +/**
17718 + * Called to turn on the VBUS after initial ADP probe in host mode.
17719 + * If port power was already enabled in cil_hcd_start function then
17720 + * only schedule a timer.
17721 + *
17722 + * @param core_if the pointer to core_if structure.
17723 + */
17724 +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
17725 +{
17726 + hprt0_data_t hprt0 = {.d32 = 0 };
17727 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
17728 + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
17729 +
17730 + if (hprt0.b.prtpwr == 0) {
17731 + hprt0.b.prtpwr = 1;
17732 + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
17733 + }
17734 +
17735 + dwc_otg_adp_vbuson_timer_start(core_if);
17736 +}
17737 +
17738 +/**
17739 + * Called right after driver is loaded
17740 + * to perform initial actions for ADP
17741 + *
17742 + * @param core_if the pointer to core_if structure.
17743 + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
17744 + */
17745 +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
17746 +{
17747 + gpwrdn_data_t gpwrdn;
17748 +
17749 + DWC_PRINTF("ADP Initial Start\n");
17750 + core_if->adp.adp_started = 1;
17751 +
17752 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17753 + dwc_otg_disable_global_interrupts(core_if);
17754 + if (is_host) {
17755 + DWC_PRINTF("HOST MODE\n");
17756 + /* Enable Power Down Logic Interrupt*/
17757 + gpwrdn.d32 = 0;
17758 + gpwrdn.b.pmuintsel = 1;
17759 + gpwrdn.b.pmuactv = 1;
17760 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17761 + /* Initialize first ADP probe to obtain Ramp Time value */
17762 + core_if->adp.initial_probe = 1;
17763 + dwc_otg_adp_probe_start(core_if);
17764 + } else {
17765 + gotgctl_data_t gotgctl;
17766 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
17767 + DWC_PRINTF("DEVICE MODE\n");
17768 + if (gotgctl.b.bsesvld == 0) {
17769 + /* Enable Power Down Logic Interrupt*/
17770 + gpwrdn.d32 = 0;
17771 + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
17772 + gpwrdn.b.pmuintsel = 1;
17773 + gpwrdn.b.pmuactv = 1;
17774 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17775 + core_if->adp.initial_probe = 1;
17776 + dwc_otg_adp_probe_start(core_if);
17777 + } else {
17778 + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
17779 + core_if->op_state = B_PERIPHERAL;
17780 + dwc_otg_core_init(core_if);
17781 + dwc_otg_enable_global_interrupts(core_if);
17782 + cil_pcd_start(core_if);
17783 + dwc_otg_dump_global_registers(core_if);
17784 + dwc_otg_dump_dev_registers(core_if);
17785 + }
17786 + }
17787 +}
17788 +
17789 +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
17790 +{
17791 + core_if->adp.adp_started = 0;
17792 + core_if->adp.initial_probe = 0;
17793 + core_if->adp.probe_timer_values[0] = -1;
17794 + core_if->adp.probe_timer_values[1] = -1;
17795 + core_if->adp.probe_enabled = 0;
17796 + core_if->adp.sense_enabled = 0;
17797 + core_if->adp.sense_timer_started = 0;
17798 + core_if->adp.vbuson_timer_started = 0;
17799 + core_if->adp.probe_counter = 0;
17800 + core_if->adp.gpwrdn = 0;
17801 + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
17802 + /* Initialize timers */
17803 + core_if->adp.sense_timer =
17804 + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
17805 + core_if->adp.vbuson_timer =
17806 + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
17807 + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
17808 + {
17809 + DWC_ERROR("Could not allocate memory for ADP timers\n");
17810 + }
17811 +}
17812 +
17813 +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
17814 +{
17815 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
17816 + gpwrdn.b.pmuintsel = 1;
17817 + gpwrdn.b.pmuactv = 1;
17818 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17819 +
17820 + if (core_if->adp.probe_enabled)
17821 + dwc_otg_adp_probe_stop(core_if);
17822 + if (core_if->adp.sense_enabled)
17823 + dwc_otg_adp_sense_stop(core_if);
17824 + if (core_if->adp.sense_timer_started)
17825 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
17826 + if (core_if->adp.vbuson_timer_started)
17827 + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
17828 + DWC_TIMER_FREE(core_if->adp.sense_timer);
17829 + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
17830 +}
17831 +
17832 +/////////////////////////////////////////////////////////////////////
17833 +////////////// ADP Interrupt Handlers ///////////////////////////////
17834 +/////////////////////////////////////////////////////////////////////
17835 +/**
17836 + * This function sets Ramp Timer values
17837 + */
17838 +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
17839 +{
17840 + if (core_if->adp.probe_timer_values[0] == -1) {
17841 + core_if->adp.probe_timer_values[0] = val;
17842 + core_if->adp.probe_timer_values[1] = -1;
17843 + return 1;
17844 + } else {
17845 + core_if->adp.probe_timer_values[1] =
17846 + core_if->adp.probe_timer_values[0];
17847 + core_if->adp.probe_timer_values[0] = val;
17848 + return 0;
17849 + }
17850 +}
17851 +
17852 +/**
17853 + * This function compares Ramp Timer values
17854 + */
17855 +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
17856 +{
17857 + uint32_t diff;
17858 + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
17859 + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
17860 + else
17861 + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
17862 + if(diff < 2) {
17863 + return 0;
17864 + } else {
17865 + return 1;
17866 + }
17867 +}
17868 +
17869 +/**
17870 + * This function handles ADP Probe Interrupts
17871 + */
17872 +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
17873 + uint32_t val)
17874 +{
17875 + adpctl_data_t adpctl = {.d32 = 0 };
17876 + gpwrdn_data_t gpwrdn, temp;
17877 + adpctl.d32 = val;
17878 +
17879 + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17880 + core_if->adp.probe_counter++;
17881 + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17882 + if (adpctl.b.rtim == 0 && !temp.b.idsts){
17883 + DWC_PRINTF("RTIM value is 0\n");
17884 + goto exit;
17885 + }
17886 + if (set_timer_value(core_if, adpctl.b.rtim) &&
17887 + core_if->adp.initial_probe) {
17888 + core_if->adp.initial_probe = 0;
17889 + dwc_otg_adp_probe_stop(core_if);
17890 + gpwrdn.d32 = 0;
17891 + gpwrdn.b.pmuactv = 1;
17892 + gpwrdn.b.pmuintsel = 1;
17893 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17894 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17895 +
17896 + /* check which value is for device mode and which for Host mode */
17897 + if (!temp.b.idsts) { /* considered host mode value is 0 */
17898 + /*
17899 + * Turn on VBUS after initial ADP probe.
17900 + */
17901 + core_if->op_state = A_HOST;
17902 + dwc_otg_enable_global_interrupts(core_if);
17903 + DWC_SPINUNLOCK(core_if->lock);
17904 + cil_hcd_start(core_if);
17905 + dwc_otg_adp_turnon_vbus(core_if);
17906 + DWC_SPINLOCK(core_if->lock);
17907 + } else {
17908 + /*
17909 + * Initiate SRP after initial ADP probe.
17910 + */
17911 + dwc_otg_enable_global_interrupts(core_if);
17912 + dwc_otg_initiate_srp(core_if);
17913 + }
17914 + } else if (core_if->adp.probe_counter > 2){
17915 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17916 + if (compare_timer_values(core_if)) {
17917 + DWC_PRINTF("Difference in timer values !!! \n");
17918 +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
17919 + dwc_otg_adp_probe_stop(core_if);
17920 +
17921 + /* Power on the core */
17922 + if (core_if->power_down == 2) {
17923 + gpwrdn.b.pwrdnswtch = 1;
17924 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17925 + gpwrdn, 0, gpwrdn.d32);
17926 + }
17927 +
17928 + /* check which value is for device mode and which for Host mode */
17929 + if (!temp.b.idsts) { /* considered host mode value is 0 */
17930 + /* Disable Interrupt from Power Down Logic */
17931 + gpwrdn.d32 = 0;
17932 + gpwrdn.b.pmuintsel = 1;
17933 + gpwrdn.b.pmuactv = 1;
17934 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17935 + gpwrdn, gpwrdn.d32, 0);
17936 +
17937 + /*
17938 + * Initialize the Core for Host mode.
17939 + */
17940 + core_if->op_state = A_HOST;
17941 + dwc_otg_core_init(core_if);
17942 + dwc_otg_enable_global_interrupts(core_if);
17943 + cil_hcd_start(core_if);
17944 + } else {
17945 + gotgctl_data_t gotgctl;
17946 + /* Mask SRP detected interrupt from Power Down Logic */
17947 + gpwrdn.d32 = 0;
17948 + gpwrdn.b.srp_det_msk = 1;
17949 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17950 + gpwrdn, gpwrdn.d32, 0);
17951 +
17952 + /* Disable Power Down Logic */
17953 + gpwrdn.d32 = 0;
17954 + gpwrdn.b.pmuintsel = 1;
17955 + gpwrdn.b.pmuactv = 1;
17956 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17957 + gpwrdn, gpwrdn.d32, 0);
17958 +
17959 + /*
17960 + * Initialize the Core for Device mode.
17961 + */
17962 + core_if->op_state = B_PERIPHERAL;
17963 + dwc_otg_core_init(core_if);
17964 + dwc_otg_enable_global_interrupts(core_if);
17965 + cil_pcd_start(core_if);
17966 +
17967 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
17968 + if (!gotgctl.b.bsesvld) {
17969 + dwc_otg_initiate_srp(core_if);
17970 + }
17971 + }
17972 + }
17973 + if (core_if->power_down == 2) {
17974 + if (gpwrdn.b.bsessvld) {
17975 + /* Mask SRP detected interrupt from Power Down Logic */
17976 + gpwrdn.d32 = 0;
17977 + gpwrdn.b.srp_det_msk = 1;
17978 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17979 +
17980 + /* Disable Power Down Logic */
17981 + gpwrdn.d32 = 0;
17982 + gpwrdn.b.pmuactv = 1;
17983 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17984 +
17985 + /*
17986 + * Initialize the Core for Device mode.
17987 + */
17988 + core_if->op_state = B_PERIPHERAL;
17989 + dwc_otg_core_init(core_if);
17990 + dwc_otg_enable_global_interrupts(core_if);
17991 + cil_pcd_start(core_if);
17992 + }
17993 + }
17994 + }
17995 +exit:
17996 + /* Clear interrupt */
17997 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17998 + adpctl.b.adp_prb_int = 1;
17999 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
18000 +
18001 + return 0;
18002 +}
18003 +
18004 +/**
18005 + * This function hadles ADP Sense Interrupt
18006 + */
18007 +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
18008 +{
18009 + adpctl_data_t adpctl;
18010 + /* Stop ADP Sense timer */
18011 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
18012 +
18013 + /* Restart ADP Sense timer */
18014 + dwc_otg_adp_sense_timer_start(core_if);
18015 +
18016 + /* Clear interrupt */
18017 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
18018 + adpctl.b.adp_sns_int = 1;
18019 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
18020 +
18021 + return 0;
18022 +}
18023 +
18024 +/**
18025 + * This function handles ADP Probe Interrupts
18026 + */
18027 +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
18028 + uint32_t val)
18029 +{
18030 + adpctl_data_t adpctl = {.d32 = 0 };
18031 + adpctl.d32 = val;
18032 + set_timer_value(core_if, adpctl.b.rtim);
18033 +
18034 + /* Clear interrupt */
18035 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
18036 + adpctl.b.adp_tmout_int = 1;
18037 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
18038 +
18039 + return 0;
18040 +}
18041 +
18042 +/**
18043 + * ADP Interrupt handler.
18044 + *
18045 + */
18046 +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
18047 +{
18048 + int retval = 0;
18049 + adpctl_data_t adpctl = {.d32 = 0};
18050 +
18051 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
18052 + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
18053 +
18054 + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
18055 + DWC_PRINTF("ADP Sense interrupt\n");
18056 + retval |= dwc_otg_adp_handle_sns_intr(core_if);
18057 + }
18058 + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
18059 + DWC_PRINTF("ADP timeout interrupt\n");
18060 + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
18061 + }
18062 + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
18063 + DWC_PRINTF("ADP Probe interrupt\n");
18064 + adpctl.b.adp_prb_int = 1;
18065 + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
18066 + }
18067 +
18068 +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
18069 + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
18070 + DWC_PRINTF("RETURN FROM ADP ISR\n");
18071 +
18072 + return retval;
18073 +}
18074 +
18075 +/**
18076 + *
18077 + * @param core_if Programming view of DWC_otg controller.
18078 + */
18079 +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
18080 +{
18081 +
18082 +#ifndef DWC_HOST_ONLY
18083 + hprt0_data_t hprt0;
18084 + gpwrdn_data_t gpwrdn;
18085 + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
18086 +
18087 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
18088 + /* check which value is for device mode and which for Host mode */
18089 + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
18090 + DWC_PRINTF("SRP: Host mode\n");
18091 +
18092 + if (core_if->adp_enable) {
18093 + dwc_otg_adp_probe_stop(core_if);
18094 +
18095 + /* Power on the core */
18096 + if (core_if->power_down == 2) {
18097 + gpwrdn.b.pwrdnswtch = 1;
18098 + DWC_MODIFY_REG32(&core_if->core_global_regs->
18099 + gpwrdn, 0, gpwrdn.d32);
18100 + }
18101 +
18102 + core_if->op_state = A_HOST;
18103 + dwc_otg_core_init(core_if);
18104 + dwc_otg_enable_global_interrupts(core_if);
18105 + cil_hcd_start(core_if);
18106 + }
18107 +
18108 + /* Turn on the port power bit. */
18109 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
18110 + hprt0.b.prtpwr = 1;
18111 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
18112 +
18113 + /* Start the Connection timer. So a message can be displayed
18114 + * if connect does not occur within 10 seconds. */
18115 + cil_hcd_session_start(core_if);
18116 + } else {
18117 + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
18118 + if (core_if->adp_enable) {
18119 + dwc_otg_adp_probe_stop(core_if);
18120 +
18121 + /* Power on the core */
18122 + if (core_if->power_down == 2) {
18123 + gpwrdn.b.pwrdnswtch = 1;
18124 + DWC_MODIFY_REG32(&core_if->core_global_regs->
18125 + gpwrdn, 0, gpwrdn.d32);
18126 + }
18127 +
18128 + gpwrdn.d32 = 0;
18129 + gpwrdn.b.pmuactv = 0;
18130 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
18131 + gpwrdn.d32);
18132 +
18133 + core_if->op_state = B_PERIPHERAL;
18134 + dwc_otg_core_init(core_if);
18135 + dwc_otg_enable_global_interrupts(core_if);
18136 + cil_pcd_start(core_if);
18137 + }
18138 + }
18139 +#endif
18140 + return 1;
18141 +}
18142 --- /dev/null
18143 +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.h
18144 @@ -0,0 +1,80 @@
18145 +/* ==========================================================================
18146 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
18147 + * $Revision: #7 $
18148 + * $Date: 2011/10/24 $
18149 + * $Change: 1871159 $
18150 + *
18151 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
18152 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
18153 + * otherwise expressly agreed to in writing between Synopsys and you.
18154 + *
18155 + * The Software IS NOT an item of Licensed Software or Licensed Product under
18156 + * any End User Software License Agreement or Agreement for Licensed Product
18157 + * with Synopsys or any supplement thereto. You are permitted to use and
18158 + * redistribute this Software in source and binary forms, with or without
18159 + * modification, provided that redistributions of source code must retain this
18160 + * notice. You may not view, use, disclose, copy or distribute this file or
18161 + * any information contained herein except pursuant to this license grant from
18162 + * Synopsys. If you do not agree with this notice, including the disclaimer
18163 + * below, then you are not authorized to use the Software.
18164 + *
18165 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
18166 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18167 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18168 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
18169 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18170 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18171 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
18172 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18173 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18174 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
18175 + * DAMAGE.
18176 + * ========================================================================== */
18177 +
18178 +#ifndef __DWC_OTG_ADP_H__
18179 +#define __DWC_OTG_ADP_H__
18180 +
18181 +/**
18182 + * @file
18183 + *
18184 + * This file contains the Attach Detect Protocol interfaces and defines
18185 + * (functions) and structures for Linux.
18186 + *
18187 + */
18188 +
18189 +#define DWC_OTG_ADP_UNATTACHED 0
18190 +#define DWC_OTG_ADP_ATTACHED 1
18191 +#define DWC_OTG_ADP_UNKOWN 2
18192 +
18193 +typedef struct dwc_otg_adp {
18194 + uint32_t adp_started;
18195 + uint32_t initial_probe;
18196 + int32_t probe_timer_values[2];
18197 + uint32_t probe_enabled;
18198 + uint32_t sense_enabled;
18199 + dwc_timer_t *sense_timer;
18200 + uint32_t sense_timer_started;
18201 + dwc_timer_t *vbuson_timer;
18202 + uint32_t vbuson_timer_started;
18203 + uint32_t attached;
18204 + uint32_t probe_counter;
18205 + uint32_t gpwrdn;
18206 +} dwc_otg_adp_t;
18207 +
18208 +/**
18209 + * Attach Detect Protocol functions
18210 + */
18211 +
18212 +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
18213 +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
18214 +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
18215 +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
18216 +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
18217 +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
18218 +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
18219 +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
18220 +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
18221 +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
18222 +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
18223 +
18224 +#endif //__DWC_OTG_ADP_H__
18225 --- /dev/null
18226 +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.c
18227 @@ -0,0 +1,1212 @@
18228 +/* ==========================================================================
18229 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
18230 + * $Revision: #44 $
18231 + * $Date: 2010/11/29 $
18232 + * $Change: 1636033 $
18233 + *
18234 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
18235 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
18236 + * otherwise expressly agreed to in writing between Synopsys and you.
18237 + *
18238 + * The Software IS NOT an item of Licensed Software or Licensed Product under
18239 + * any End User Software License Agreement or Agreement for Licensed Product
18240 + * with Synopsys or any supplement thereto. You are permitted to use and
18241 + * redistribute this Software in source and binary forms, with or without
18242 + * modification, provided that redistributions of source code must retain this
18243 + * notice. You may not view, use, disclose, copy or distribute this file or
18244 + * any information contained herein except pursuant to this license grant from
18245 + * Synopsys. If you do not agree with this notice, including the disclaimer
18246 + * below, then you are not authorized to use the Software.
18247 + *
18248 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
18249 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18250 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18251 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
18252 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18253 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18254 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
18255 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18256 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18257 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
18258 + * DAMAGE.
18259 + * ========================================================================== */
18260 +
18261 +/** @file
18262 + *
18263 + * The diagnostic interface will provide access to the controller for
18264 + * bringing up the hardware and testing. The Linux driver attributes
18265 + * feature will be used to provide the Linux Diagnostic
18266 + * Interface. These attributes are accessed through sysfs.
18267 + */
18268 +
18269 +/** @page "Linux Module Attributes"
18270 + *
18271 + * The Linux module attributes feature is used to provide the Linux
18272 + * Diagnostic Interface. These attributes are accessed through sysfs.
18273 + * The diagnostic interface will provide access to the controller for
18274 + * bringing up the hardware and testing.
18275 +
18276 + The following table shows the attributes.
18277 + <table>
18278 + <tr>
18279 + <td><b> Name</b></td>
18280 + <td><b> Description</b></td>
18281 + <td><b> Access</b></td>
18282 + </tr>
18283 +
18284 + <tr>
18285 + <td> mode </td>
18286 + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
18287 + <td> Read</td>
18288 + </tr>
18289 +
18290 + <tr>
18291 + <td> hnpcapable </td>
18292 + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
18293 + Read returns the current value.</td>
18294 + <td> Read/Write</td>
18295 + </tr>
18296 +
18297 + <tr>
18298 + <td> srpcapable </td>
18299 + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
18300 + Read returns the current value.</td>
18301 + <td> Read/Write</td>
18302 + </tr>
18303 +
18304 + <tr>
18305 + <td> hsic_connect </td>
18306 + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
18307 + Read returns the current value.</td>
18308 + <td> Read/Write</td>
18309 + </tr>
18310 +
18311 + <tr>
18312 + <td> inv_sel_hsic </td>
18313 + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
18314 + Read returns the current value.</td>
18315 + <td> Read/Write</td>
18316 + </tr>
18317 +
18318 + <tr>
18319 + <td> hnp </td>
18320 + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
18321 + <td> Read/Write</td>
18322 + </tr>
18323 +
18324 + <tr>
18325 + <td> srp </td>
18326 + <td> Initiates the Session Request Protocol. Read returns the status.</td>
18327 + <td> Read/Write</td>
18328 + </tr>
18329 +
18330 + <tr>
18331 + <td> buspower </td>
18332 + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
18333 + <td> Read/Write</td>
18334 + </tr>
18335 +
18336 + <tr>
18337 + <td> bussuspend </td>
18338 + <td> Suspends the USB bus.</td>
18339 + <td> Read/Write</td>
18340 + </tr>
18341 +
18342 + <tr>
18343 + <td> busconnected </td>
18344 + <td> Gets the connection status of the bus</td>
18345 + <td> Read</td>
18346 + </tr>
18347 +
18348 + <tr>
18349 + <td> gotgctl </td>
18350 + <td> Gets or sets the Core Control Status Register.</td>
18351 + <td> Read/Write</td>
18352 + </tr>
18353 +
18354 + <tr>
18355 + <td> gusbcfg </td>
18356 + <td> Gets or sets the Core USB Configuration Register</td>
18357 + <td> Read/Write</td>
18358 + </tr>
18359 +
18360 + <tr>
18361 + <td> grxfsiz </td>
18362 + <td> Gets or sets the Receive FIFO Size Register</td>
18363 + <td> Read/Write</td>
18364 + </tr>
18365 +
18366 + <tr>
18367 + <td> gnptxfsiz </td>
18368 + <td> Gets or sets the non-periodic Transmit Size Register</td>
18369 + <td> Read/Write</td>
18370 + </tr>
18371 +
18372 + <tr>
18373 + <td> gpvndctl </td>
18374 + <td> Gets or sets the PHY Vendor Control Register</td>
18375 + <td> Read/Write</td>
18376 + </tr>
18377 +
18378 + <tr>
18379 + <td> ggpio </td>
18380 + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
18381 + or sets the upper 16 bits.</td>
18382 + <td> Read/Write</td>
18383 + </tr>
18384 +
18385 + <tr>
18386 + <td> guid </td>
18387 + <td> Gets or sets the value of the User ID Register</td>
18388 + <td> Read/Write</td>
18389 + </tr>
18390 +
18391 + <tr>
18392 + <td> gsnpsid </td>
18393 + <td> Gets the value of the Synopsys ID Regester</td>
18394 + <td> Read</td>
18395 + </tr>
18396 +
18397 + <tr>
18398 + <td> devspeed </td>
18399 + <td> Gets or sets the device speed setting in the DCFG register</td>
18400 + <td> Read/Write</td>
18401 + </tr>
18402 +
18403 + <tr>
18404 + <td> enumspeed </td>
18405 + <td> Gets the device enumeration Speed.</td>
18406 + <td> Read</td>
18407 + </tr>
18408 +
18409 + <tr>
18410 + <td> hptxfsiz </td>
18411 + <td> Gets the value of the Host Periodic Transmit FIFO</td>
18412 + <td> Read</td>
18413 + </tr>
18414 +
18415 + <tr>
18416 + <td> hprt0 </td>
18417 + <td> Gets or sets the value in the Host Port Control and Status Register</td>
18418 + <td> Read/Write</td>
18419 + </tr>
18420 +
18421 + <tr>
18422 + <td> regoffset </td>
18423 + <td> Sets the register offset for the next Register Access</td>
18424 + <td> Read/Write</td>
18425 + </tr>
18426 +
18427 + <tr>
18428 + <td> regvalue </td>
18429 + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
18430 + <td> Read/Write</td>
18431 + </tr>
18432 +
18433 + <tr>
18434 + <td> remote_wakeup </td>
18435 + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
18436 + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
18437 + Wakeup signalling bit in the Device Control Register is set for 1
18438 + milli-second.</td>
18439 + <td> Read/Write</td>
18440 + </tr>
18441 +
18442 + <tr>
18443 + <td> rem_wakeup_pwrdn </td>
18444 + <td> On read, shows the status core - hibernated or not. On write, initiates
18445 + a remote wakeup of the device from Hibernation. </td>
18446 + <td> Read/Write</td>
18447 + </tr>
18448 +
18449 + <tr>
18450 + <td> mode_ch_tim_en </td>
18451 + <td> This bit is used to enable or disable the host core to wait for 200 PHY
18452 + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
18453 + after Suspend or LPM. </td>
18454 + <td> Read/Write</td>
18455 + </tr>
18456 +
18457 + <tr>
18458 + <td> fr_interval </td>
18459 + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
18460 + reload HFIR register during runtime. The application can write a value to this
18461 + register only after the Port Enable bit of the Host Port Control and Status
18462 + register (HPRT.PrtEnaPort) has been set </td>
18463 + <td> Read/Write</td>
18464 + </tr>
18465 +
18466 + <tr>
18467 + <td> disconnect_us </td>
18468 + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
18469 + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
18470 + <td> Read/Write</td>
18471 + </tr>
18472 +
18473 + <tr>
18474 + <td> regdump </td>
18475 + <td> Dumps the contents of core registers.</td>
18476 + <td> Read</td>
18477 + </tr>
18478 +
18479 + <tr>
18480 + <td> spramdump </td>
18481 + <td> Dumps the contents of core registers.</td>
18482 + <td> Read</td>
18483 + </tr>
18484 +
18485 + <tr>
18486 + <td> hcddump </td>
18487 + <td> Dumps the current HCD state.</td>
18488 + <td> Read</td>
18489 + </tr>
18490 +
18491 + <tr>
18492 + <td> hcd_frrem </td>
18493 + <td> Shows the average value of the Frame Remaining
18494 + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
18495 + occurs. This can be used to determine the average interrupt latency. Also
18496 + shows the average Frame Remaining value for start_transfer and the "a" and
18497 + "b" sample points. The "a" and "b" sample points may be used during debugging
18498 + bto determine how long it takes to execute a section of the HCD code.</td>
18499 + <td> Read</td>
18500 + </tr>
18501 +
18502 + <tr>
18503 + <td> rd_reg_test </td>
18504 + <td> Displays the time required to read the GNPTXFSIZ register many times
18505 + (the output shows the number of times the register is read).
18506 + <td> Read</td>
18507 + </tr>
18508 +
18509 + <tr>
18510 + <td> wr_reg_test </td>
18511 + <td> Displays the time required to write the GNPTXFSIZ register many times
18512 + (the output shows the number of times the register is written).
18513 + <td> Read</td>
18514 + </tr>
18515 +
18516 + <tr>
18517 + <td> lpm_response </td>
18518 + <td> Gets or sets lpm_response mode. Applicable only in device mode.
18519 + <td> Write</td>
18520 + </tr>
18521 +
18522 + <tr>
18523 + <td> sleep_status </td>
18524 + <td> Shows sleep status of device.
18525 + <td> Read</td>
18526 + </tr>
18527 +
18528 + </table>
18529 +
18530 + Example usage:
18531 + To get the current mode:
18532 + cat /sys/devices/lm0/mode
18533 +
18534 + To power down the USB:
18535 + echo 0 > /sys/devices/lm0/buspower
18536 + */
18537 +
18538 +#include "dwc_otg_os_dep.h"
18539 +#include "dwc_os.h"
18540 +#include "dwc_otg_driver.h"
18541 +#include "dwc_otg_attr.h"
18542 +#include "dwc_otg_core_if.h"
18543 +#include "dwc_otg_pcd_if.h"
18544 +#include "dwc_otg_hcd_if.h"
18545 +
18546 +/*
18547 + * MACROs for defining sysfs attribute
18548 + */
18549 +#ifdef LM_INTERFACE
18550 +
18551 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18552 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18553 +{ \
18554 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18555 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18556 + uint32_t val; \
18557 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18558 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18559 +}
18560 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18561 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18562 + const char *buf, size_t count) \
18563 +{ \
18564 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18565 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18566 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18567 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18568 + return count; \
18569 +}
18570 +
18571 +#elif defined(PCI_INTERFACE)
18572 +
18573 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18574 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18575 +{ \
18576 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18577 + uint32_t val; \
18578 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18579 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18580 +}
18581 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18582 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18583 + const char *buf, size_t count) \
18584 +{ \
18585 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18586 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18587 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18588 + return count; \
18589 +}
18590 +
18591 +#elif defined(PLATFORM_INTERFACE)
18592 +
18593 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18594 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18595 +{ \
18596 + struct platform_device *platform_dev = \
18597 + container_of(_dev, struct platform_device, dev); \
18598 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18599 + uint32_t val; \
18600 + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
18601 + __func__, _dev, platform_dev, otg_dev); \
18602 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18603 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18604 +}
18605 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18606 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18607 + const char *buf, size_t count) \
18608 +{ \
18609 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18610 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18611 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18612 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18613 + return count; \
18614 +}
18615 +#endif
18616 +
18617 +/*
18618 + * MACROs for defining sysfs attribute for 32-bit registers
18619 + */
18620 +#ifdef LM_INTERFACE
18621 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18622 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18623 +{ \
18624 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18625 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18626 + uint32_t val; \
18627 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18628 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18629 +}
18630 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18631 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18632 + const char *buf, size_t count) \
18633 +{ \
18634 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18635 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18636 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18637 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18638 + return count; \
18639 +}
18640 +#elif defined(PCI_INTERFACE)
18641 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18642 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18643 +{ \
18644 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18645 + uint32_t val; \
18646 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18647 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18648 +}
18649 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18650 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18651 + const char *buf, size_t count) \
18652 +{ \
18653 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18654 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18655 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18656 + return count; \
18657 +}
18658 +
18659 +#elif defined(PLATFORM_INTERFACE)
18660 +#include "dwc_otg_dbg.h"
18661 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18662 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18663 +{ \
18664 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18665 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18666 + uint32_t val; \
18667 + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
18668 + __func__, _dev, platform_dev, otg_dev); \
18669 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18670 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18671 +}
18672 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18673 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18674 + const char *buf, size_t count) \
18675 +{ \
18676 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18677 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18678 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18679 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18680 + return count; \
18681 +}
18682 +
18683 +#endif
18684 +
18685 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
18686 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18687 +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18688 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
18689 +
18690 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
18691 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18692 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
18693 +
18694 +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
18695 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18696 +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18697 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
18698 +
18699 +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
18700 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18701 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
18702 +
18703 +/** @name Functions for Show/Store of Attributes */
18704 +/**@{*/
18705 +
18706 +/**
18707 + * Helper function returning the otg_device structure of the given device
18708 + */
18709 +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
18710 +{
18711 + dwc_otg_device_t *otg_dev;
18712 + DWC_OTG_GETDRVDEV(otg_dev, _dev);
18713 + return otg_dev;
18714 +}
18715 +
18716 +/**
18717 + * Show the register offset of the Register Access.
18718 + */
18719 +static ssize_t regoffset_show(struct device *_dev,
18720 + struct device_attribute *attr, char *buf)
18721 +{
18722 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18723 + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
18724 + otg_dev->os_dep.reg_offset);
18725 +}
18726 +
18727 +/**
18728 + * Set the register offset for the next Register Access Read/Write
18729 + */
18730 +static ssize_t regoffset_store(struct device *_dev,
18731 + struct device_attribute *attr,
18732 + const char *buf, size_t count)
18733 +{
18734 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18735 + uint32_t offset = simple_strtoul(buf, NULL, 16);
18736 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
18737 + if (offset < SZ_256K) {
18738 +#elif defined(PCI_INTERFACE)
18739 + if (offset < 0x00040000) {
18740 +#endif
18741 + otg_dev->os_dep.reg_offset = offset;
18742 + } else {
18743 + dev_err(_dev, "invalid offset\n");
18744 + }
18745 +
18746 + return count;
18747 +}
18748 +
18749 +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
18750 +
18751 +/**
18752 + * Show the value of the register at the offset in the reg_offset
18753 + * attribute.
18754 + */
18755 +static ssize_t regvalue_show(struct device *_dev,
18756 + struct device_attribute *attr, char *buf)
18757 +{
18758 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18759 + uint32_t val;
18760 + volatile uint32_t *addr;
18761 +
18762 + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
18763 + /* Calculate the address */
18764 + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
18765 + (uint8_t *) otg_dev->os_dep.base);
18766 + val = DWC_READ_REG32(addr);
18767 + return snprintf(buf,
18768 + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
18769 + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
18770 + val);
18771 + } else {
18772 + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
18773 + return sprintf(buf, "invalid offset\n");
18774 + }
18775 +}
18776 +
18777 +/**
18778 + * Store the value in the register at the offset in the reg_offset
18779 + * attribute.
18780 + *
18781 + */
18782 +static ssize_t regvalue_store(struct device *_dev,
18783 + struct device_attribute *attr,
18784 + const char *buf, size_t count)
18785 +{
18786 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18787 + volatile uint32_t *addr;
18788 + uint32_t val = simple_strtoul(buf, NULL, 16);
18789 + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
18790 + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
18791 + /* Calculate the address */
18792 + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
18793 + (uint8_t *) otg_dev->os_dep.base);
18794 + DWC_WRITE_REG32(addr, val);
18795 + } else {
18796 + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
18797 + otg_dev->os_dep.reg_offset);
18798 + }
18799 + return count;
18800 +}
18801 +
18802 +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
18803 +
18804 +/*
18805 + * Attributes
18806 + */
18807 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
18808 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
18809 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
18810 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
18811 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
18812 +
18813 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
18814 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
18815 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
18816 +
18817 +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
18818 +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
18819 + &(otg_dev->core_if->core_global_regs->gusbcfg),
18820 + "GUSBCFG");
18821 +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
18822 + &(otg_dev->core_if->core_global_regs->grxfsiz),
18823 + "GRXFSIZ");
18824 +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
18825 + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
18826 + "GNPTXFSIZ");
18827 +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
18828 + &(otg_dev->core_if->core_global_regs->gpvndctl),
18829 + "GPVNDCTL");
18830 +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
18831 + &(otg_dev->core_if->core_global_regs->ggpio),
18832 + "GGPIO");
18833 +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
18834 + "GUID");
18835 +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
18836 + &(otg_dev->core_if->core_global_regs->gsnpsid),
18837 + "GSNPSID");
18838 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
18839 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
18840 +
18841 +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
18842 + &(otg_dev->core_if->core_global_regs->hptxfsiz),
18843 + "HPTXFSIZ");
18844 +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
18845 +
18846 +/**
18847 + * @todo Add code to initiate the HNP.
18848 + */
18849 +/**
18850 + * Show the HNP status bit
18851 + */
18852 +static ssize_t hnp_show(struct device *_dev,
18853 + struct device_attribute *attr, char *buf)
18854 +{
18855 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18856 + return sprintf(buf, "HstNegScs = 0x%x\n",
18857 + dwc_otg_get_hnpstatus(otg_dev->core_if));
18858 +}
18859 +
18860 +/**
18861 + * Set the HNP Request bit
18862 + */
18863 +static ssize_t hnp_store(struct device *_dev,
18864 + struct device_attribute *attr,
18865 + const char *buf, size_t count)
18866 +{
18867 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18868 + uint32_t in = simple_strtoul(buf, NULL, 16);
18869 + dwc_otg_set_hnpreq(otg_dev->core_if, in);
18870 + return count;
18871 +}
18872 +
18873 +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
18874 +
18875 +/**
18876 + * @todo Add code to initiate the SRP.
18877 + */
18878 +/**
18879 + * Show the SRP status bit
18880 + */
18881 +static ssize_t srp_show(struct device *_dev,
18882 + struct device_attribute *attr, char *buf)
18883 +{
18884 +#ifndef DWC_HOST_ONLY
18885 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18886 + return sprintf(buf, "SesReqScs = 0x%x\n",
18887 + dwc_otg_get_srpstatus(otg_dev->core_if));
18888 +#else
18889 + return sprintf(buf, "Host Only Mode!\n");
18890 +#endif
18891 +}
18892 +
18893 +/**
18894 + * Set the SRP Request bit
18895 + */
18896 +static ssize_t srp_store(struct device *_dev,
18897 + struct device_attribute *attr,
18898 + const char *buf, size_t count)
18899 +{
18900 +#ifndef DWC_HOST_ONLY
18901 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18902 + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
18903 +#endif
18904 + return count;
18905 +}
18906 +
18907 +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
18908 +
18909 +/**
18910 + * @todo Need to do more for power on/off?
18911 + */
18912 +/**
18913 + * Show the Bus Power status
18914 + */
18915 +static ssize_t buspower_show(struct device *_dev,
18916 + struct device_attribute *attr, char *buf)
18917 +{
18918 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18919 + return sprintf(buf, "Bus Power = 0x%x\n",
18920 + dwc_otg_get_prtpower(otg_dev->core_if));
18921 +}
18922 +
18923 +/**
18924 + * Set the Bus Power status
18925 + */
18926 +static ssize_t buspower_store(struct device *_dev,
18927 + struct device_attribute *attr,
18928 + const char *buf, size_t count)
18929 +{
18930 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18931 + uint32_t on = simple_strtoul(buf, NULL, 16);
18932 + dwc_otg_set_prtpower(otg_dev->core_if, on);
18933 + return count;
18934 +}
18935 +
18936 +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
18937 +
18938 +/**
18939 + * @todo Need to do more for suspend?
18940 + */
18941 +/**
18942 + * Show the Bus Suspend status
18943 + */
18944 +static ssize_t bussuspend_show(struct device *_dev,
18945 + struct device_attribute *attr, char *buf)
18946 +{
18947 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18948 + return sprintf(buf, "Bus Suspend = 0x%x\n",
18949 + dwc_otg_get_prtsuspend(otg_dev->core_if));
18950 +}
18951 +
18952 +/**
18953 + * Set the Bus Suspend status
18954 + */
18955 +static ssize_t bussuspend_store(struct device *_dev,
18956 + struct device_attribute *attr,
18957 + const char *buf, size_t count)
18958 +{
18959 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18960 + uint32_t in = simple_strtoul(buf, NULL, 16);
18961 + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
18962 + return count;
18963 +}
18964 +
18965 +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
18966 +
18967 +/**
18968 + * Show the Mode Change Ready Timer status
18969 + */
18970 +static ssize_t mode_ch_tim_en_show(struct device *_dev,
18971 + struct device_attribute *attr, char *buf)
18972 +{
18973 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18974 + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
18975 + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
18976 +}
18977 +
18978 +/**
18979 + * Set the Mode Change Ready Timer status
18980 + */
18981 +static ssize_t mode_ch_tim_en_store(struct device *_dev,
18982 + struct device_attribute *attr,
18983 + const char *buf, size_t count)
18984 +{
18985 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18986 + uint32_t in = simple_strtoul(buf, NULL, 16);
18987 + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
18988 + return count;
18989 +}
18990 +
18991 +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
18992 +
18993 +/**
18994 + * Show the value of HFIR Frame Interval bitfield
18995 + */
18996 +static ssize_t fr_interval_show(struct device *_dev,
18997 + struct device_attribute *attr, char *buf)
18998 +{
18999 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19000 + return sprintf(buf, "Frame Interval = 0x%x\n",
19001 + dwc_otg_get_fr_interval(otg_dev->core_if));
19002 +}
19003 +
19004 +/**
19005 + * Set the HFIR Frame Interval value
19006 + */
19007 +static ssize_t fr_interval_store(struct device *_dev,
19008 + struct device_attribute *attr,
19009 + const char *buf, size_t count)
19010 +{
19011 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19012 + uint32_t in = simple_strtoul(buf, NULL, 10);
19013 + dwc_otg_set_fr_interval(otg_dev->core_if, in);
19014 + return count;
19015 +}
19016 +
19017 +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
19018 +
19019 +/**
19020 + * Show the status of Remote Wakeup.
19021 + */
19022 +static ssize_t remote_wakeup_show(struct device *_dev,
19023 + struct device_attribute *attr, char *buf)
19024 +{
19025 +#ifndef DWC_HOST_ONLY
19026 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19027 +
19028 + return sprintf(buf,
19029 + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
19030 + dwc_otg_get_remotewakesig(otg_dev->core_if),
19031 + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
19032 + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
19033 +#else
19034 + return sprintf(buf, "Host Only Mode!\n");
19035 +#endif /* DWC_HOST_ONLY */
19036 +}
19037 +
19038 +/**
19039 + * Initiate a remote wakeup of the host. The Device control register
19040 + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
19041 + * flag is set.
19042 + *
19043 + */
19044 +static ssize_t remote_wakeup_store(struct device *_dev,
19045 + struct device_attribute *attr,
19046 + const char *buf, size_t count)
19047 +{
19048 +#ifndef DWC_HOST_ONLY
19049 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19050 + uint32_t val = simple_strtoul(buf, NULL, 16);
19051 +
19052 + if (val & 1) {
19053 + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
19054 + } else {
19055 + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
19056 + }
19057 +#endif /* DWC_HOST_ONLY */
19058 + return count;
19059 +}
19060 +
19061 +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
19062 + remote_wakeup_store);
19063 +
19064 +/**
19065 + * Show the whether core is hibernated or not.
19066 + */
19067 +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
19068 + struct device_attribute *attr, char *buf)
19069 +{
19070 +#ifndef DWC_HOST_ONLY
19071 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19072 +
19073 + if (dwc_otg_get_core_state(otg_dev->core_if)) {
19074 + DWC_PRINTF("Core is in hibernation\n");
19075 + } else {
19076 + DWC_PRINTF("Core is not in hibernation\n");
19077 + }
19078 +#endif /* DWC_HOST_ONLY */
19079 + return 0;
19080 +}
19081 +
19082 +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
19083 + int rem_wakeup, int reset);
19084 +
19085 +/**
19086 + * Initiate a remote wakeup of the device to exit from hibernation.
19087 + */
19088 +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
19089 + struct device_attribute *attr,
19090 + const char *buf, size_t count)
19091 +{
19092 +#ifndef DWC_HOST_ONLY
19093 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19094 + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
19095 +#endif
19096 + return count;
19097 +}
19098 +
19099 +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
19100 + rem_wakeup_pwrdn_store);
19101 +
19102 +static ssize_t disconnect_us(struct device *_dev,
19103 + struct device_attribute *attr,
19104 + const char *buf, size_t count)
19105 +{
19106 +
19107 +#ifndef DWC_HOST_ONLY
19108 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19109 + uint32_t val = simple_strtoul(buf, NULL, 16);
19110 + DWC_PRINTF("The Passed value is %04x\n", val);
19111 +
19112 + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
19113 +
19114 +#endif /* DWC_HOST_ONLY */
19115 + return count;
19116 +}
19117 +
19118 +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
19119 +
19120 +/**
19121 + * Dump global registers and either host or device registers (depending on the
19122 + * current mode of the core).
19123 + */
19124 +static ssize_t regdump_show(struct device *_dev,
19125 + struct device_attribute *attr, char *buf)
19126 +{
19127 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19128 +
19129 + dwc_otg_dump_global_registers(otg_dev->core_if);
19130 + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
19131 + dwc_otg_dump_host_registers(otg_dev->core_if);
19132 + } else {
19133 + dwc_otg_dump_dev_registers(otg_dev->core_if);
19134 +
19135 + }
19136 + return sprintf(buf, "Register Dump\n");
19137 +}
19138 +
19139 +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
19140 +
19141 +/**
19142 + * Dump global registers and either host or device registers (depending on the
19143 + * current mode of the core).
19144 + */
19145 +static ssize_t spramdump_show(struct device *_dev,
19146 + struct device_attribute *attr, char *buf)
19147 +{
19148 +#if 0
19149 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19150 +
19151 + dwc_otg_dump_spram(otg_dev->core_if);
19152 +#endif
19153 +
19154 + return sprintf(buf, "SPRAM Dump\n");
19155 +}
19156 +
19157 +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
19158 +
19159 +/**
19160 + * Dump the current hcd state.
19161 + */
19162 +static ssize_t hcddump_show(struct device *_dev,
19163 + struct device_attribute *attr, char *buf)
19164 +{
19165 +#ifndef DWC_DEVICE_ONLY
19166 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19167 + dwc_otg_hcd_dump_state(otg_dev->hcd);
19168 +#endif /* DWC_DEVICE_ONLY */
19169 + return sprintf(buf, "HCD Dump\n");
19170 +}
19171 +
19172 +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
19173 +
19174 +/**
19175 + * Dump the average frame remaining at SOF. This can be used to
19176 + * determine average interrupt latency. Frame remaining is also shown for
19177 + * start transfer and two additional sample points.
19178 + */
19179 +static ssize_t hcd_frrem_show(struct device *_dev,
19180 + struct device_attribute *attr, char *buf)
19181 +{
19182 +#ifndef DWC_DEVICE_ONLY
19183 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19184 +
19185 + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
19186 +#endif /* DWC_DEVICE_ONLY */
19187 + return sprintf(buf, "HCD Dump Frame Remaining\n");
19188 +}
19189 +
19190 +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
19191 +
19192 +/**
19193 + * Displays the time required to read the GNPTXFSIZ register many times (the
19194 + * output shows the number of times the register is read).
19195 + */
19196 +#define RW_REG_COUNT 10000000
19197 +#define MSEC_PER_JIFFIE 1000/HZ
19198 +static ssize_t rd_reg_test_show(struct device *_dev,
19199 + struct device_attribute *attr, char *buf)
19200 +{
19201 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19202 + int i;
19203 + int time;
19204 + int start_jiffies;
19205 +
19206 + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
19207 + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
19208 + start_jiffies = jiffies;
19209 + for (i = 0; i < RW_REG_COUNT; i++) {
19210 + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
19211 + }
19212 + time = jiffies - start_jiffies;
19213 + return sprintf(buf,
19214 + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
19215 + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
19216 +}
19217 +
19218 +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
19219 +
19220 +/**
19221 + * Displays the time required to write the GNPTXFSIZ register many times (the
19222 + * output shows the number of times the register is written).
19223 + */
19224 +static ssize_t wr_reg_test_show(struct device *_dev,
19225 + struct device_attribute *attr, char *buf)
19226 +{
19227 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19228 + uint32_t reg_val;
19229 + int i;
19230 + int time;
19231 + int start_jiffies;
19232 +
19233 + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
19234 + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
19235 + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
19236 + start_jiffies = jiffies;
19237 + for (i = 0; i < RW_REG_COUNT; i++) {
19238 + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
19239 + }
19240 + time = jiffies - start_jiffies;
19241 + return sprintf(buf,
19242 + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
19243 + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
19244 +}
19245 +
19246 +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
19247 +
19248 +#ifdef CONFIG_USB_DWC_OTG_LPM
19249 +
19250 +/**
19251 +* Show the lpm_response attribute.
19252 +*/
19253 +static ssize_t lpmresp_show(struct device *_dev,
19254 + struct device_attribute *attr, char *buf)
19255 +{
19256 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19257 +
19258 + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
19259 + return sprintf(buf, "** LPM is DISABLED **\n");
19260 +
19261 + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
19262 + return sprintf(buf, "** Current mode is not device mode\n");
19263 + }
19264 + return sprintf(buf, "lpm_response = %d\n",
19265 + dwc_otg_get_lpmresponse(otg_dev->core_if));
19266 +}
19267 +
19268 +/**
19269 +* Store the lpm_response attribute.
19270 +*/
19271 +static ssize_t lpmresp_store(struct device *_dev,
19272 + struct device_attribute *attr,
19273 + const char *buf, size_t count)
19274 +{
19275 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19276 + uint32_t val = simple_strtoul(buf, NULL, 16);
19277 +
19278 + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
19279 + return 0;
19280 + }
19281 +
19282 + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
19283 + return 0;
19284 + }
19285 +
19286 + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
19287 + return count;
19288 +}
19289 +
19290 +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
19291 +
19292 +/**
19293 +* Show the sleep_status attribute.
19294 +*/
19295 +static ssize_t sleepstatus_show(struct device *_dev,
19296 + struct device_attribute *attr, char *buf)
19297 +{
19298 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19299 + return sprintf(buf, "Sleep Status = %d\n",
19300 + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
19301 +}
19302 +
19303 +/**
19304 + * Store the sleep_status attribure.
19305 + */
19306 +static ssize_t sleepstatus_store(struct device *_dev,
19307 + struct device_attribute *attr,
19308 + const char *buf, size_t count)
19309 +{
19310 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19311 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
19312 +
19313 + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
19314 + if (dwc_otg_is_host_mode(core_if)) {
19315 +
19316 + DWC_PRINTF("Host initiated resume\n");
19317 + dwc_otg_set_prtresume(otg_dev->core_if, 1);
19318 + }
19319 + }
19320 +
19321 + return count;
19322 +}
19323 +
19324 +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
19325 + sleepstatus_store);
19326 +
19327 +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
19328 +
19329 +/**@}*/
19330 +
19331 +/**
19332 + * Create the device files
19333 + */
19334 +void dwc_otg_attr_create(
19335 +#ifdef LM_INTERFACE
19336 + struct lm_device *dev
19337 +#elif defined(PCI_INTERFACE)
19338 + struct pci_dev *dev
19339 +#elif defined(PLATFORM_INTERFACE)
19340 + struct platform_device *dev
19341 +#endif
19342 + )
19343 +{
19344 + int error;
19345 +
19346 + error = device_create_file(&dev->dev, &dev_attr_regoffset);
19347 + error = device_create_file(&dev->dev, &dev_attr_regvalue);
19348 + error = device_create_file(&dev->dev, &dev_attr_mode);
19349 + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
19350 + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
19351 + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
19352 + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
19353 + error = device_create_file(&dev->dev, &dev_attr_hnp);
19354 + error = device_create_file(&dev->dev, &dev_attr_srp);
19355 + error = device_create_file(&dev->dev, &dev_attr_buspower);
19356 + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
19357 + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
19358 + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
19359 + error = device_create_file(&dev->dev, &dev_attr_busconnected);
19360 + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
19361 + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
19362 + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
19363 + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
19364 + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
19365 + error = device_create_file(&dev->dev, &dev_attr_ggpio);
19366 + error = device_create_file(&dev->dev, &dev_attr_guid);
19367 + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
19368 + error = device_create_file(&dev->dev, &dev_attr_devspeed);
19369 + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
19370 + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
19371 + error = device_create_file(&dev->dev, &dev_attr_hprt0);
19372 + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
19373 + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
19374 + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
19375 + error = device_create_file(&dev->dev, &dev_attr_regdump);
19376 + error = device_create_file(&dev->dev, &dev_attr_spramdump);
19377 + error = device_create_file(&dev->dev, &dev_attr_hcddump);
19378 + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
19379 + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
19380 + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
19381 +#ifdef CONFIG_USB_DWC_OTG_LPM
19382 + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
19383 + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
19384 +#endif
19385 +}
19386 +
19387 +/**
19388 + * Remove the device files
19389 + */
19390 +void dwc_otg_attr_remove(
19391 +#ifdef LM_INTERFACE
19392 + struct lm_device *dev
19393 +#elif defined(PCI_INTERFACE)
19394 + struct pci_dev *dev
19395 +#elif defined(PLATFORM_INTERFACE)
19396 + struct platform_device *dev
19397 +#endif
19398 + )
19399 +{
19400 + device_remove_file(&dev->dev, &dev_attr_regoffset);
19401 + device_remove_file(&dev->dev, &dev_attr_regvalue);
19402 + device_remove_file(&dev->dev, &dev_attr_mode);
19403 + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
19404 + device_remove_file(&dev->dev, &dev_attr_srpcapable);
19405 + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
19406 + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
19407 + device_remove_file(&dev->dev, &dev_attr_hnp);
19408 + device_remove_file(&dev->dev, &dev_attr_srp);
19409 + device_remove_file(&dev->dev, &dev_attr_buspower);
19410 + device_remove_file(&dev->dev, &dev_attr_bussuspend);
19411 + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
19412 + device_remove_file(&dev->dev, &dev_attr_fr_interval);
19413 + device_remove_file(&dev->dev, &dev_attr_busconnected);
19414 + device_remove_file(&dev->dev, &dev_attr_gotgctl);
19415 + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
19416 + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
19417 + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
19418 + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
19419 + device_remove_file(&dev->dev, &dev_attr_ggpio);
19420 + device_remove_file(&dev->dev, &dev_attr_guid);
19421 + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
19422 + device_remove_file(&dev->dev, &dev_attr_devspeed);
19423 + device_remove_file(&dev->dev, &dev_attr_enumspeed);
19424 + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
19425 + device_remove_file(&dev->dev, &dev_attr_hprt0);
19426 + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
19427 + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
19428 + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
19429 + device_remove_file(&dev->dev, &dev_attr_regdump);
19430 + device_remove_file(&dev->dev, &dev_attr_spramdump);
19431 + device_remove_file(&dev->dev, &dev_attr_hcddump);
19432 + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
19433 + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
19434 + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
19435 +#ifdef CONFIG_USB_DWC_OTG_LPM
19436 + device_remove_file(&dev->dev, &dev_attr_lpm_response);
19437 + device_remove_file(&dev->dev, &dev_attr_sleep_status);
19438 +#endif
19439 +}
19440 --- /dev/null
19441 +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.h
19442 @@ -0,0 +1,89 @@
19443 +/* ==========================================================================
19444 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
19445 + * $Revision: #13 $
19446 + * $Date: 2010/06/21 $
19447 + * $Change: 1532021 $
19448 + *
19449 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
19450 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
19451 + * otherwise expressly agreed to in writing between Synopsys and you.
19452 + *
19453 + * The Software IS NOT an item of Licensed Software or Licensed Product under
19454 + * any End User Software License Agreement or Agreement for Licensed Product
19455 + * with Synopsys or any supplement thereto. You are permitted to use and
19456 + * redistribute this Software in source and binary forms, with or without
19457 + * modification, provided that redistributions of source code must retain this
19458 + * notice. You may not view, use, disclose, copy or distribute this file or
19459 + * any information contained herein except pursuant to this license grant from
19460 + * Synopsys. If you do not agree with this notice, including the disclaimer
19461 + * below, then you are not authorized to use the Software.
19462 + *
19463 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
19464 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19465 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19466 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
19467 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19468 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19469 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19470 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19471 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19472 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
19473 + * DAMAGE.
19474 + * ========================================================================== */
19475 +
19476 +#if !defined(__DWC_OTG_ATTR_H__)
19477 +#define __DWC_OTG_ATTR_H__
19478 +
19479 +/** @file
19480 + * This file contains the interface to the Linux device attributes.
19481 + */
19482 +extern struct device_attribute dev_attr_regoffset;
19483 +extern struct device_attribute dev_attr_regvalue;
19484 +
19485 +extern struct device_attribute dev_attr_mode;
19486 +extern struct device_attribute dev_attr_hnpcapable;
19487 +extern struct device_attribute dev_attr_srpcapable;
19488 +extern struct device_attribute dev_attr_hnp;
19489 +extern struct device_attribute dev_attr_srp;
19490 +extern struct device_attribute dev_attr_buspower;
19491 +extern struct device_attribute dev_attr_bussuspend;
19492 +extern struct device_attribute dev_attr_mode_ch_tim_en;
19493 +extern struct device_attribute dev_attr_fr_interval;
19494 +extern struct device_attribute dev_attr_busconnected;
19495 +extern struct device_attribute dev_attr_gotgctl;
19496 +extern struct device_attribute dev_attr_gusbcfg;
19497 +extern struct device_attribute dev_attr_grxfsiz;
19498 +extern struct device_attribute dev_attr_gnptxfsiz;
19499 +extern struct device_attribute dev_attr_gpvndctl;
19500 +extern struct device_attribute dev_attr_ggpio;
19501 +extern struct device_attribute dev_attr_guid;
19502 +extern struct device_attribute dev_attr_gsnpsid;
19503 +extern struct device_attribute dev_attr_devspeed;
19504 +extern struct device_attribute dev_attr_enumspeed;
19505 +extern struct device_attribute dev_attr_hptxfsiz;
19506 +extern struct device_attribute dev_attr_hprt0;
19507 +#ifdef CONFIG_USB_DWC_OTG_LPM
19508 +extern struct device_attribute dev_attr_lpm_response;
19509 +extern struct device_attribute devi_attr_sleep_status;
19510 +#endif
19511 +
19512 +void dwc_otg_attr_create(
19513 +#ifdef LM_INTERFACE
19514 + struct lm_device *dev
19515 +#elif defined(PCI_INTERFACE)
19516 + struct pci_dev *dev
19517 +#elif defined(PLATFORM_INTERFACE)
19518 + struct platform_device *dev
19519 +#endif
19520 + );
19521 +
19522 +void dwc_otg_attr_remove(
19523 +#ifdef LM_INTERFACE
19524 + struct lm_device *dev
19525 +#elif defined(PCI_INTERFACE)
19526 + struct pci_dev *dev
19527 +#elif defined(PLATFORM_INTERFACE)
19528 + struct platform_device *dev
19529 +#endif
19530 + );
19531 +#endif
19532 --- /dev/null
19533 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
19534 @@ -0,0 +1,1876 @@
19535 +/* ==========================================================================
19536 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
19537 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
19538 + * otherwise expressly agreed to in writing between Synopsys and you.
19539 + *
19540 + * The Software IS NOT an item of Licensed Software or Licensed Product under
19541 + * any End User Software License Agreement or Agreement for Licensed Product
19542 + * with Synopsys or any supplement thereto. You are permitted to use and
19543 + * redistribute this Software in source and binary forms, with or without
19544 + * modification, provided that redistributions of source code must retain this
19545 + * notice. You may not view, use, disclose, copy or distribute this file or
19546 + * any information contained herein except pursuant to this license grant from
19547 + * Synopsys. If you do not agree with this notice, including the disclaimer
19548 + * below, then you are not authorized to use the Software.
19549 + *
19550 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
19551 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19552 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19553 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
19554 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19555 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19556 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19557 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19558 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19559 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
19560 + * DAMAGE.
19561 + * ========================================================================== */
19562 +
19563 +/** @file
19564 + *
19565 + * This file contains the most of the CFI(Core Feature Interface)
19566 + * implementation for the OTG.
19567 + */
19568 +
19569 +#ifdef DWC_UTE_CFI
19570 +
19571 +#include "dwc_otg_pcd.h"
19572 +#include "dwc_otg_cfi.h"
19573 +
19574 +/** This definition should actually migrate to the Portability Library */
19575 +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
19576 +
19577 +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
19578 +
19579 +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
19580 +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
19581 + struct dwc_otg_pcd *pcd,
19582 + struct cfi_usb_ctrlrequest *ctrl_req);
19583 +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
19584 +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19585 + struct cfi_usb_ctrlrequest *req);
19586 +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19587 + struct cfi_usb_ctrlrequest *req);
19588 +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19589 + struct cfi_usb_ctrlrequest *req);
19590 +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
19591 + struct cfi_usb_ctrlrequest *req);
19592 +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
19593 +
19594 +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
19595 +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
19596 +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
19597 +
19598 +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
19599 +
19600 +/** This is the header of the all features descriptor */
19601 +static cfi_all_features_header_t all_props_desc_header = {
19602 + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
19603 + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
19604 + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
19605 +};
19606 +
19607 +/** This is an array of statically allocated feature descriptors */
19608 +static cfi_feature_desc_header_t prop_descs[] = {
19609 +
19610 + /* FT_ID_DMA_MODE */
19611 + {
19612 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
19613 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19614 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
19615 + },
19616 +
19617 + /* FT_ID_DMA_BUFFER_SETUP */
19618 + {
19619 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
19620 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19621 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19622 + },
19623 +
19624 + /* FT_ID_DMA_BUFF_ALIGN */
19625 + {
19626 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
19627 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19628 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19629 + },
19630 +
19631 + /* FT_ID_DMA_CONCAT_SETUP */
19632 + {
19633 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
19634 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19635 + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19636 + },
19637 +
19638 + /* FT_ID_DMA_CIRCULAR */
19639 + {
19640 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
19641 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19642 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19643 + },
19644 +
19645 + /* FT_ID_THRESHOLD_SETUP */
19646 + {
19647 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
19648 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19649 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19650 + },
19651 +
19652 + /* FT_ID_DFIFO_DEPTH */
19653 + {
19654 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
19655 + .bmAttributes = CFI_FEATURE_ATTR_RO,
19656 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19657 + },
19658 +
19659 + /* FT_ID_TX_FIFO_DEPTH */
19660 + {
19661 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
19662 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19663 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19664 + },
19665 +
19666 + /* FT_ID_RX_FIFO_DEPTH */
19667 + {
19668 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
19669 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19670 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19671 + }
19672 +};
19673 +
19674 +/** The table of feature names */
19675 +cfi_string_t prop_name_table[] = {
19676 + {FT_ID_DMA_MODE, "dma_mode"},
19677 + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
19678 + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
19679 + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
19680 + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
19681 + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
19682 + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
19683 + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
19684 + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
19685 + {}
19686 +};
19687 +
19688 +/************************************************************************/
19689 +
19690 +/**
19691 + * Returns the name of the feature by its ID
19692 + * or NULL if no featute ID matches.
19693 + *
19694 + */
19695 +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
19696 +{
19697 + cfi_string_t *pstr;
19698 + *len = 0;
19699 +
19700 + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
19701 + if (pstr->id == prop_id) {
19702 + *len = DWC_STRLEN(pstr->s);
19703 + return pstr->s;
19704 + }
19705 + }
19706 + return NULL;
19707 +}
19708 +
19709 +/**
19710 + * This function handles all CFI specific control requests.
19711 + *
19712 + * Return a negative value to stall the DCE.
19713 + */
19714 +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
19715 +{
19716 + int retval = 0;
19717 + dwc_otg_pcd_ep_t *ep = NULL;
19718 + cfiobject_t *cfi = pcd->cfi;
19719 + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
19720 + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
19721 + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
19722 + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
19723 + uint32_t regaddr = 0;
19724 + uint32_t regval = 0;
19725 +
19726 + /* Save this Control Request in the CFI object.
19727 + * The data field will be assigned in the data stage completion CB function.
19728 + */
19729 + cfi->ctrl_req = *ctrl;
19730 + cfi->ctrl_req.data = NULL;
19731 +
19732 + cfi->need_gadget_att = 0;
19733 + cfi->need_status_in_complete = 0;
19734 +
19735 + switch (ctrl->bRequest) {
19736 + case VEN_CORE_GET_FEATURES:
19737 + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
19738 + if (retval >= 0) {
19739 + //dump_msg(cfi->buf_in.buf, retval);
19740 + ep = &pcd->ep0;
19741 +
19742 + retval = min((uint16_t) retval, wLen);
19743 + /* Transfer this buffer to the host through the EP0-IN EP */
19744 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19745 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19746 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19747 + ep->dwc_ep.xfer_len = retval;
19748 + ep->dwc_ep.xfer_count = 0;
19749 + ep->dwc_ep.sent_zlp = 0;
19750 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19751 +
19752 + pcd->ep0_pending = 1;
19753 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19754 + }
19755 + retval = 0;
19756 + break;
19757 +
19758 + case VEN_CORE_GET_FEATURE:
19759 + CFI_INFO("VEN_CORE_GET_FEATURE\n");
19760 + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
19761 + pcd, ctrl);
19762 + if (retval >= 0) {
19763 + ep = &pcd->ep0;
19764 +
19765 + retval = min((uint16_t) retval, wLen);
19766 + /* Transfer this buffer to the host through the EP0-IN EP */
19767 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19768 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19769 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19770 + ep->dwc_ep.xfer_len = retval;
19771 + ep->dwc_ep.xfer_count = 0;
19772 + ep->dwc_ep.sent_zlp = 0;
19773 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19774 +
19775 + pcd->ep0_pending = 1;
19776 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19777 + }
19778 + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
19779 + dump_msg(cfi->buf_in.buf, retval);
19780 + break;
19781 +
19782 + case VEN_CORE_SET_FEATURE:
19783 + CFI_INFO("VEN_CORE_SET_FEATURE\n");
19784 + /* Set up an XFER to get the data stage of the control request,
19785 + * which is the new value of the feature to be modified.
19786 + */
19787 + ep = &pcd->ep0;
19788 + ep->dwc_ep.is_in = 0;
19789 + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
19790 + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
19791 + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
19792 + ep->dwc_ep.xfer_len = wLen;
19793 + ep->dwc_ep.xfer_count = 0;
19794 + ep->dwc_ep.sent_zlp = 0;
19795 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19796 +
19797 + pcd->ep0_pending = 1;
19798 + /* Read the control write's data stage */
19799 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19800 + retval = 0;
19801 + break;
19802 +
19803 + case VEN_CORE_RESET_FEATURES:
19804 + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
19805 + cfi->need_gadget_att = 1;
19806 + cfi->need_status_in_complete = 1;
19807 + retval = cfi_preproc_reset(pcd, ctrl);
19808 + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
19809 + break;
19810 +
19811 + case VEN_CORE_ACTIVATE_FEATURES:
19812 + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
19813 + break;
19814 +
19815 + case VEN_CORE_READ_REGISTER:
19816 + CFI_INFO("VEN_CORE_READ_REGISTER\n");
19817 + /* wValue optionally contains the HI WORD of the register offset and
19818 + * wIndex contains the LOW WORD of the register offset
19819 + */
19820 + if (wValue == 0) {
19821 + /* @TODO - MAS - fix the access to the base field */
19822 + regaddr = 0;
19823 + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
19824 + //GET_CORE_IF(pcd)->co
19825 + regaddr |= wIndex;
19826 + } else {
19827 + regaddr = (wValue << 16) | wIndex;
19828 + }
19829 +
19830 + /* Read a 32-bit value of the memory at the regaddr */
19831 + regval = DWC_READ_REG32((uint32_t *) regaddr);
19832 +
19833 + ep = &pcd->ep0;
19834 + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
19835 + ep->dwc_ep.is_in = 1;
19836 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19837 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19838 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19839 + ep->dwc_ep.xfer_len = wLen;
19840 + ep->dwc_ep.xfer_count = 0;
19841 + ep->dwc_ep.sent_zlp = 0;
19842 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19843 +
19844 + pcd->ep0_pending = 1;
19845 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19846 + cfi->need_gadget_att = 0;
19847 + retval = 0;
19848 + break;
19849 +
19850 + case VEN_CORE_WRITE_REGISTER:
19851 + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
19852 + /* Set up an XFER to get the data stage of the control request,
19853 + * which is the new value of the register to be modified.
19854 + */
19855 + ep = &pcd->ep0;
19856 + ep->dwc_ep.is_in = 0;
19857 + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
19858 + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
19859 + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
19860 + ep->dwc_ep.xfer_len = wLen;
19861 + ep->dwc_ep.xfer_count = 0;
19862 + ep->dwc_ep.sent_zlp = 0;
19863 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19864 +
19865 + pcd->ep0_pending = 1;
19866 + /* Read the control write's data stage */
19867 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19868 + retval = 0;
19869 + break;
19870 +
19871 + default:
19872 + retval = -DWC_E_NOT_SUPPORTED;
19873 + break;
19874 + }
19875 +
19876 + return retval;
19877 +}
19878 +
19879 +/**
19880 + * This function prepares the core features descriptors and copies its
19881 + * raw representation into the buffer <buf>.
19882 + *
19883 + * The buffer structure is as follows:
19884 + * all_features_header (8 bytes)
19885 + * features_#1 (8 bytes + feature name string length)
19886 + * features_#2 (8 bytes + feature name string length)
19887 + * .....
19888 + * features_#n - where n=the total count of feature descriptors
19889 + */
19890 +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
19891 +{
19892 + cfi_feature_desc_header_t *prop_hdr = prop_descs;
19893 + cfi_feature_desc_header_t *prop;
19894 + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
19895 + cfi_all_features_header_t *tmp;
19896 + uint8_t *tmpbuf = buf;
19897 + const uint8_t *pname = NULL;
19898 + int i, j, namelen = 0, totlen;
19899 +
19900 + /* Prepare and copy the core features into the buffer */
19901 + CFI_INFO("%s:\n", __func__);
19902 +
19903 + tmp = (cfi_all_features_header_t *) tmpbuf;
19904 + *tmp = *all_props_hdr;
19905 + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
19906 +
19907 + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
19908 + for (i = 0; i < j; i++, prop_hdr++) {
19909 + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
19910 + prop = (cfi_feature_desc_header_t *) tmpbuf;
19911 + *prop = *prop_hdr;
19912 +
19913 + prop->bNameLen = namelen;
19914 + prop->wLength =
19915 + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
19916 + namelen);
19917 +
19918 + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
19919 + dwc_memcpy(tmpbuf, pname, namelen);
19920 + tmpbuf += namelen;
19921 + }
19922 +
19923 + totlen = tmpbuf - buf;
19924 +
19925 + if (totlen > 0) {
19926 + tmp = (cfi_all_features_header_t *) buf;
19927 + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
19928 + }
19929 +
19930 + return totlen;
19931 +}
19932 +
19933 +/**
19934 + * This function releases all the dynamic memory in the CFI object.
19935 + */
19936 +static void cfi_release(cfiobject_t * cfiobj)
19937 +{
19938 + cfi_ep_t *cfiep;
19939 + dwc_list_link_t *tmp;
19940 +
19941 + CFI_INFO("%s\n", __func__);
19942 +
19943 + if (cfiobj->buf_in.buf) {
19944 + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
19945 + cfiobj->buf_in.addr);
19946 + cfiobj->buf_in.buf = NULL;
19947 + }
19948 +
19949 + if (cfiobj->buf_out.buf) {
19950 + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
19951 + cfiobj->buf_out.addr);
19952 + cfiobj->buf_out.buf = NULL;
19953 + }
19954 +
19955 + /* Free the Buffer Setup values for each EP */
19956 + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
19957 + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
19958 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
19959 + cfi_free_ep_bs_dyn_data(cfiep);
19960 + }
19961 +}
19962 +
19963 +/**
19964 + * This function frees the dynamically allocated EP buffer setup data.
19965 + */
19966 +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
19967 +{
19968 + if (cfiep->bm_sg) {
19969 + DWC_FREE(cfiep->bm_sg);
19970 + cfiep->bm_sg = NULL;
19971 + }
19972 +
19973 + if (cfiep->bm_align) {
19974 + DWC_FREE(cfiep->bm_align);
19975 + cfiep->bm_align = NULL;
19976 + }
19977 +
19978 + if (cfiep->bm_concat) {
19979 + if (NULL != cfiep->bm_concat->wTxBytes) {
19980 + DWC_FREE(cfiep->bm_concat->wTxBytes);
19981 + cfiep->bm_concat->wTxBytes = NULL;
19982 + }
19983 + DWC_FREE(cfiep->bm_concat);
19984 + cfiep->bm_concat = NULL;
19985 + }
19986 +}
19987 +
19988 +/**
19989 + * This function initializes the default values of the features
19990 + * for a specific endpoint and should be called only once when
19991 + * the EP is enabled first time.
19992 + */
19993 +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
19994 +{
19995 + int retval = 0;
19996 +
19997 + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
19998 + if (NULL == cfiep->bm_sg) {
19999 + CFI_INFO("Failed to allocate memory for SG feature value\n");
20000 + return -DWC_E_NO_MEMORY;
20001 + }
20002 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20003 +
20004 + /* For the Concatenation feature's default value we do not allocate
20005 + * memory for the wTxBytes field - it will be done in the set_feature_value
20006 + * request handler.
20007 + */
20008 + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
20009 + if (NULL == cfiep->bm_concat) {
20010 + CFI_INFO
20011 + ("Failed to allocate memory for CONCATENATION feature value\n");
20012 + DWC_FREE(cfiep->bm_sg);
20013 + return -DWC_E_NO_MEMORY;
20014 + }
20015 + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
20016 +
20017 + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
20018 + if (NULL == cfiep->bm_align) {
20019 + CFI_INFO
20020 + ("Failed to allocate memory for Alignment feature value\n");
20021 + DWC_FREE(cfiep->bm_sg);
20022 + DWC_FREE(cfiep->bm_concat);
20023 + return -DWC_E_NO_MEMORY;
20024 + }
20025 + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
20026 +
20027 + return retval;
20028 +}
20029 +
20030 +/**
20031 + * The callback function that notifies the CFI on the activation of
20032 + * an endpoint in the PCD. The following steps are done in this function:
20033 + *
20034 + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
20035 + * active endpoint)
20036 + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
20037 + * Set the Buffer Mode to standard
20038 + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
20039 + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
20040 + */
20041 +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
20042 + struct dwc_otg_pcd_ep *ep)
20043 +{
20044 + cfi_ep_t *cfiep;
20045 + int retval = -DWC_E_NOT_SUPPORTED;
20046 +
20047 + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
20048 + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
20049 + /* MAS - Check whether this endpoint already is in the list */
20050 + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
20051 +
20052 + if (NULL == cfiep) {
20053 + /* Allocate a cfi_ep_t object */
20054 + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
20055 + if (NULL == cfiep) {
20056 + CFI_INFO
20057 + ("Unable to allocate memory for <cfiep> in function %s\n",
20058 + __func__);
20059 + return -DWC_E_NO_MEMORY;
20060 + }
20061 + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
20062 +
20063 + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
20064 + cfiep->ep = ep;
20065 +
20066 + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
20067 + ep->dwc_ep.descs =
20068 + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
20069 + sizeof(dwc_otg_dma_desc_t),
20070 + &ep->dwc_ep.descs_dma_addr);
20071 +
20072 + if (NULL == ep->dwc_ep.descs) {
20073 + DWC_FREE(cfiep);
20074 + return -DWC_E_NO_MEMORY;
20075 + }
20076 +
20077 + DWC_LIST_INIT(&cfiep->lh);
20078 +
20079 + /* Set the buffer mode to BM_STANDARD. It will be modified
20080 + * when building descriptors for a specific buffer mode */
20081 + ep->dwc_ep.buff_mode = BM_STANDARD;
20082 +
20083 + /* Create and initialize the default values for this EP's Buffer modes */
20084 + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
20085 + return retval;
20086 +
20087 + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
20088 + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
20089 + retval = 0;
20090 + } else { /* The sought EP already is in the list */
20091 + CFI_INFO("%s: The sought EP already is in the list\n",
20092 + __func__);
20093 + }
20094 +
20095 + return retval;
20096 +}
20097 +
20098 +/**
20099 + * This function is called when the data stage of a 3-stage Control Write request
20100 + * is complete.
20101 + *
20102 + */
20103 +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
20104 + struct dwc_otg_pcd *pcd)
20105 +{
20106 + uint32_t addr, reg_value;
20107 + uint16_t wIndex, wValue;
20108 + uint8_t bRequest;
20109 + uint8_t *buf = cfi->buf_out.buf;
20110 + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
20111 + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
20112 + int retval = -DWC_E_NOT_SUPPORTED;
20113 +
20114 + CFI_INFO("%s\n", __func__);
20115 +
20116 + bRequest = ctrl_req->bRequest;
20117 + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
20118 + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
20119 +
20120 + /*
20121 + * Save the pointer to the data stage in the ctrl_req's <data> field.
20122 + * The request should be already saved in the command stage by now.
20123 + */
20124 + ctrl_req->data = cfi->buf_out.buf;
20125 + cfi->need_status_in_complete = 0;
20126 + cfi->need_gadget_att = 0;
20127 +
20128 + switch (bRequest) {
20129 + case VEN_CORE_WRITE_REGISTER:
20130 + /* The buffer contains raw data of the new value for the register */
20131 + reg_value = *((uint32_t *) buf);
20132 + if (wValue == 0) {
20133 + addr = 0;
20134 + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
20135 + addr += wIndex;
20136 + } else {
20137 + addr = (wValue << 16) | wIndex;
20138 + }
20139 +
20140 + //writel(reg_value, addr);
20141 +
20142 + retval = 0;
20143 + cfi->need_status_in_complete = 1;
20144 + break;
20145 +
20146 + case VEN_CORE_SET_FEATURE:
20147 + /* The buffer contains raw data of the new value of the feature */
20148 + retval = cfi_set_feature_value(pcd);
20149 + if (retval < 0)
20150 + return retval;
20151 +
20152 + cfi->need_status_in_complete = 1;
20153 + break;
20154 +
20155 + default:
20156 + break;
20157 + }
20158 +
20159 + return retval;
20160 +}
20161 +
20162 +/**
20163 + * This function builds the DMA descriptors for the SG buffer mode.
20164 + */
20165 +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20166 + dwc_otg_pcd_request_t * req)
20167 +{
20168 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
20169 + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
20170 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
20171 + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
20172 + dma_addr_t buff_addr = req->dma;
20173 + int i;
20174 + uint32_t txsize, off;
20175 +
20176 + txsize = sgval->wSize;
20177 + off = sgval->bOffset;
20178 +
20179 +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
20180 +// __func__, cfiep->ep->ep.name, txsize, off);
20181 +
20182 + for (i = 0; i < sgval->bCount; i++) {
20183 + desc->status.b.bs = BS_HOST_BUSY;
20184 + desc->buf = buff_addr;
20185 + desc->status.b.l = 0;
20186 + desc->status.b.ioc = 0;
20187 + desc->status.b.sp = 0;
20188 + desc->status.b.bytes = txsize;
20189 + desc->status.b.bs = BS_HOST_READY;
20190 +
20191 + /* Set the next address of the buffer */
20192 + buff_addr += txsize + off;
20193 + desc_last = desc;
20194 + desc++;
20195 + }
20196 +
20197 + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
20198 + desc_last->status.b.l = 1;
20199 + desc_last->status.b.ioc = 1;
20200 + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
20201 + /* Save the last DMA descriptor pointer */
20202 + cfiep->dma_desc_last = desc_last;
20203 + cfiep->desc_count = sgval->bCount;
20204 +}
20205 +
20206 +/**
20207 + * This function builds the DMA descriptors for the Concatenation buffer mode.
20208 + */
20209 +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20210 + dwc_otg_pcd_request_t * req)
20211 +{
20212 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
20213 + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
20214 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
20215 + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
20216 + dma_addr_t buff_addr = req->dma;
20217 + int i;
20218 + uint16_t *txsize;
20219 +
20220 + txsize = concatval->wTxBytes;
20221 +
20222 + for (i = 0; i < concatval->hdr.bDescCount; i++) {
20223 + desc->buf = buff_addr;
20224 + desc->status.b.bs = BS_HOST_BUSY;
20225 + desc->status.b.l = 0;
20226 + desc->status.b.ioc = 0;
20227 + desc->status.b.sp = 0;
20228 + desc->status.b.bytes = *txsize;
20229 + desc->status.b.bs = BS_HOST_READY;
20230 +
20231 + txsize++;
20232 + /* Set the next address of the buffer */
20233 + buff_addr += UGETW(ep->desc->wMaxPacketSize);
20234 + desc_last = desc;
20235 + desc++;
20236 + }
20237 +
20238 + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
20239 + desc_last->status.b.l = 1;
20240 + desc_last->status.b.ioc = 1;
20241 + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
20242 + cfiep->dma_desc_last = desc_last;
20243 + cfiep->desc_count = concatval->hdr.bDescCount;
20244 +}
20245 +
20246 +/**
20247 + * This function builds the DMA descriptors for the Circular buffer mode
20248 + */
20249 +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20250 + dwc_otg_pcd_request_t * req)
20251 +{
20252 + /* @todo: MAS - add implementation when this feature needs to be tested */
20253 +}
20254 +
20255 +/**
20256 + * This function builds the DMA descriptors for the Alignment buffer mode
20257 + */
20258 +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20259 + dwc_otg_pcd_request_t * req)
20260 +{
20261 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
20262 + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
20263 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
20264 + dma_addr_t buff_addr = req->dma;
20265 +
20266 + desc->status.b.bs = BS_HOST_BUSY;
20267 + desc->status.b.l = 1;
20268 + desc->status.b.ioc = 1;
20269 + desc->status.b.sp = ep->dwc_ep.sent_zlp;
20270 + desc->status.b.bytes = req->length;
20271 + /* Adjust the buffer alignment */
20272 + desc->buf = (buff_addr + alignval->bAlign);
20273 + desc->status.b.bs = BS_HOST_READY;
20274 + cfiep->dma_desc_last = desc;
20275 + cfiep->desc_count = 1;
20276 +}
20277 +
20278 +/**
20279 + * This function builds the DMA descriptors chain for different modes of the
20280 + * buffer setup of an endpoint.
20281 + */
20282 +static void cfi_build_descriptors(struct cfiobject *cfi,
20283 + struct dwc_otg_pcd *pcd,
20284 + struct dwc_otg_pcd_ep *ep,
20285 + dwc_otg_pcd_request_t * req)
20286 +{
20287 + cfi_ep_t *cfiep;
20288 +
20289 + /* Get the cfiep by the dwc_otg_pcd_ep */
20290 + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
20291 + if (NULL == cfiep) {
20292 + CFI_INFO("%s: Unable to find a matching active endpoint\n",
20293 + __func__);
20294 + return;
20295 + }
20296 +
20297 + cfiep->xfer_len = req->length;
20298 +
20299 + /* Iterate through all the DMA descriptors */
20300 + switch (cfiep->ep->dwc_ep.buff_mode) {
20301 + case BM_SG:
20302 + cfi_build_sg_descs(cfi, cfiep, req);
20303 + break;
20304 +
20305 + case BM_CONCAT:
20306 + cfi_build_concat_descs(cfi, cfiep, req);
20307 + break;
20308 +
20309 + case BM_CIRCULAR:
20310 + cfi_build_circ_descs(cfi, cfiep, req);
20311 + break;
20312 +
20313 + case BM_ALIGN:
20314 + cfi_build_align_descs(cfi, cfiep, req);
20315 + break;
20316 +
20317 + default:
20318 + break;
20319 + }
20320 +}
20321 +
20322 +/**
20323 + * Allocate DMA buffer for different Buffer modes.
20324 + */
20325 +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
20326 + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
20327 + unsigned size, gfp_t flags)
20328 +{
20329 + return DWC_DMA_ALLOC(size, dma);
20330 +}
20331 +
20332 +/**
20333 + * This function initializes the CFI object.
20334 + */
20335 +int init_cfi(cfiobject_t * cfiobj)
20336 +{
20337 + CFI_INFO("%s\n", __func__);
20338 +
20339 + /* Allocate a buffer for IN XFERs */
20340 + cfiobj->buf_in.buf =
20341 + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
20342 + if (NULL == cfiobj->buf_in.buf) {
20343 + CFI_INFO("Unable to allocate buffer for INs\n");
20344 + return -DWC_E_NO_MEMORY;
20345 + }
20346 +
20347 + /* Allocate a buffer for OUT XFERs */
20348 + cfiobj->buf_out.buf =
20349 + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
20350 + if (NULL == cfiobj->buf_out.buf) {
20351 + CFI_INFO("Unable to allocate buffer for OUT\n");
20352 + return -DWC_E_NO_MEMORY;
20353 + }
20354 +
20355 + /* Initialize the callback function pointers */
20356 + cfiobj->ops.release = cfi_release;
20357 + cfiobj->ops.ep_enable = cfi_ep_enable;
20358 + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
20359 + cfiobj->ops.build_descriptors = cfi_build_descriptors;
20360 + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
20361 +
20362 + /* Initialize the list of active endpoints in the CFI object */
20363 + DWC_LIST_INIT(&cfiobj->active_eps);
20364 +
20365 + return 0;
20366 +}
20367 +
20368 +/**
20369 + * This function reads the required feature's current value into the buffer
20370 + *
20371 + * @retval: Returns negative as error, or the data length of the feature
20372 + */
20373 +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
20374 + struct dwc_otg_pcd *pcd,
20375 + struct cfi_usb_ctrlrequest *ctrl_req)
20376 +{
20377 + int retval = -DWC_E_NOT_SUPPORTED;
20378 + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
20379 + uint16_t dfifo, rxfifo, txfifo;
20380 +
20381 + switch (ctrl_req->wIndex) {
20382 + /* Whether the DDMA is enabled or not */
20383 + case FT_ID_DMA_MODE:
20384 + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
20385 + retval = 1;
20386 + break;
20387 +
20388 + case FT_ID_DMA_BUFFER_SETUP:
20389 + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
20390 + break;
20391 +
20392 + case FT_ID_DMA_BUFF_ALIGN:
20393 + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
20394 + break;
20395 +
20396 + case FT_ID_DMA_CONCAT_SETUP:
20397 + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
20398 + break;
20399 +
20400 + case FT_ID_DMA_CIRCULAR:
20401 + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
20402 + break;
20403 +
20404 + case FT_ID_THRESHOLD_SETUP:
20405 + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
20406 + break;
20407 +
20408 + case FT_ID_DFIFO_DEPTH:
20409 + dfifo = get_dfifo_size(coreif);
20410 + *((uint16_t *) buf) = dfifo;
20411 + retval = sizeof(uint16_t);
20412 + break;
20413 +
20414 + case FT_ID_TX_FIFO_DEPTH:
20415 + retval = get_txfifo_size(pcd, ctrl_req->wValue);
20416 + if (retval >= 0) {
20417 + txfifo = retval;
20418 + *((uint16_t *) buf) = txfifo;
20419 + retval = sizeof(uint16_t);
20420 + }
20421 + break;
20422 +
20423 + case FT_ID_RX_FIFO_DEPTH:
20424 + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
20425 + if (retval >= 0) {
20426 + rxfifo = retval;
20427 + *((uint16_t *) buf) = rxfifo;
20428 + retval = sizeof(uint16_t);
20429 + }
20430 + break;
20431 + }
20432 +
20433 + return retval;
20434 +}
20435 +
20436 +/**
20437 + * This function resets the SG for the specified EP to its default value
20438 + */
20439 +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
20440 +{
20441 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20442 + return 0;
20443 +}
20444 +
20445 +/**
20446 + * This function resets the Alignment for the specified EP to its default value
20447 + */
20448 +static int cfi_reset_align_val(cfi_ep_t * cfiep)
20449 +{
20450 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20451 + return 0;
20452 +}
20453 +
20454 +/**
20455 + * This function resets the Concatenation for the specified EP to its default value
20456 + * This function will also set the value of the wTxBytes field to NULL after
20457 + * freeing the memory previously allocated for this field.
20458 + */
20459 +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
20460 +{
20461 + /* First we need to free the wTxBytes field */
20462 + if (cfiep->bm_concat->wTxBytes) {
20463 + DWC_FREE(cfiep->bm_concat->wTxBytes);
20464 + cfiep->bm_concat->wTxBytes = NULL;
20465 + }
20466 +
20467 + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
20468 + return 0;
20469 +}
20470 +
20471 +/**
20472 + * This function resets all the buffer setups of the specified endpoint
20473 + */
20474 +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
20475 +{
20476 + cfi_reset_sg_val(cfiep);
20477 + cfi_reset_align_val(cfiep);
20478 + cfi_reset_concat_val(cfiep);
20479 + return 0;
20480 +}
20481 +
20482 +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
20483 + uint8_t rx_rst, uint8_t tx_rst)
20484 +{
20485 + int retval = -DWC_E_INVALID;
20486 + uint16_t tx_siz[15];
20487 + uint16_t rx_siz = 0;
20488 + dwc_otg_pcd_ep_t *ep = NULL;
20489 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
20490 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
20491 +
20492 + if (rx_rst) {
20493 + rx_siz = params->dev_rx_fifo_size;
20494 + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
20495 + }
20496 +
20497 + if (tx_rst) {
20498 + if (ep_addr == 0) {
20499 + int i;
20500 +
20501 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20502 + tx_siz[i] =
20503 + core_if->core_params->dev_tx_fifo_size[i];
20504 + core_if->core_params->dev_tx_fifo_size[i] =
20505 + core_if->init_txfsiz[i];
20506 + }
20507 + } else {
20508 +
20509 + ep = get_ep_by_addr(pcd, ep_addr);
20510 +
20511 + if (NULL == ep) {
20512 + CFI_INFO
20513 + ("%s: Unable to get the endpoint addr=0x%02x\n",
20514 + __func__, ep_addr);
20515 + return -DWC_E_INVALID;
20516 + }
20517 +
20518 + tx_siz[0] =
20519 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
20520 + 1];
20521 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
20522 + GET_CORE_IF(pcd)->init_txfsiz[ep->
20523 + dwc_ep.tx_fifo_num -
20524 + 1];
20525 + }
20526 + }
20527 +
20528 + if (resize_fifos(GET_CORE_IF(pcd))) {
20529 + retval = 0;
20530 + } else {
20531 + CFI_INFO
20532 + ("%s: Error resetting the feature Reset All(FIFO size)\n",
20533 + __func__);
20534 + if (rx_rst) {
20535 + params->dev_rx_fifo_size = rx_siz;
20536 + }
20537 +
20538 + if (tx_rst) {
20539 + if (ep_addr == 0) {
20540 + int i;
20541 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
20542 + i++) {
20543 + core_if->
20544 + core_params->dev_tx_fifo_size[i] =
20545 + tx_siz[i];
20546 + }
20547 + } else {
20548 + params->dev_tx_fifo_size[ep->
20549 + dwc_ep.tx_fifo_num -
20550 + 1] = tx_siz[0];
20551 + }
20552 + }
20553 + retval = -DWC_E_INVALID;
20554 + }
20555 + return retval;
20556 +}
20557 +
20558 +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
20559 +{
20560 + int retval = 0;
20561 + cfi_ep_t *cfiep;
20562 + cfiobject_t *cfi = pcd->cfi;
20563 + dwc_list_link_t *tmp;
20564 +
20565 + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
20566 + if (retval < 0) {
20567 + return retval;
20568 + }
20569 +
20570 + /* If the EP address is known then reset the features for only that EP */
20571 + if (addr) {
20572 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20573 + if (NULL == cfiep) {
20574 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20575 + __func__, addr);
20576 + return -DWC_E_INVALID;
20577 + }
20578 + retval = cfi_ep_reset_all_setup_vals(cfiep);
20579 + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
20580 + }
20581 + /* Otherwise (wValue == 0), reset all features of all EP's */
20582 + else {
20583 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20584 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20585 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20586 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20587 + retval = cfi_ep_reset_all_setup_vals(cfiep);
20588 + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
20589 + if (retval < 0) {
20590 + CFI_INFO
20591 + ("%s: Error resetting the feature Reset All\n",
20592 + __func__);
20593 + return retval;
20594 + }
20595 + }
20596 + }
20597 + return retval;
20598 +}
20599 +
20600 +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
20601 + uint8_t addr)
20602 +{
20603 + int retval = 0;
20604 + cfi_ep_t *cfiep;
20605 + cfiobject_t *cfi = pcd->cfi;
20606 + dwc_list_link_t *tmp;
20607 +
20608 + /* If the EP address is known then reset the features for only that EP */
20609 + if (addr) {
20610 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20611 + if (NULL == cfiep) {
20612 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20613 + __func__, addr);
20614 + return -DWC_E_INVALID;
20615 + }
20616 + retval = cfi_reset_sg_val(cfiep);
20617 + }
20618 + /* Otherwise (wValue == 0), reset all features of all EP's */
20619 + else {
20620 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20621 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20622 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20623 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20624 + retval = cfi_reset_sg_val(cfiep);
20625 + if (retval < 0) {
20626 + CFI_INFO
20627 + ("%s: Error resetting the feature Buffer Setup\n",
20628 + __func__);
20629 + return retval;
20630 + }
20631 + }
20632 + }
20633 + return retval;
20634 +}
20635 +
20636 +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
20637 +{
20638 + int retval = 0;
20639 + cfi_ep_t *cfiep;
20640 + cfiobject_t *cfi = pcd->cfi;
20641 + dwc_list_link_t *tmp;
20642 +
20643 + /* If the EP address is known then reset the features for only that EP */
20644 + if (addr) {
20645 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20646 + if (NULL == cfiep) {
20647 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20648 + __func__, addr);
20649 + return -DWC_E_INVALID;
20650 + }
20651 + retval = cfi_reset_concat_val(cfiep);
20652 + }
20653 + /* Otherwise (wValue == 0), reset all features of all EP's */
20654 + else {
20655 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20656 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20657 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20658 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20659 + retval = cfi_reset_concat_val(cfiep);
20660 + if (retval < 0) {
20661 + CFI_INFO
20662 + ("%s: Error resetting the feature Concatenation Value\n",
20663 + __func__);
20664 + return retval;
20665 + }
20666 + }
20667 + }
20668 + return retval;
20669 +}
20670 +
20671 +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
20672 +{
20673 + int retval = 0;
20674 + cfi_ep_t *cfiep;
20675 + cfiobject_t *cfi = pcd->cfi;
20676 + dwc_list_link_t *tmp;
20677 +
20678 + /* If the EP address is known then reset the features for only that EP */
20679 + if (addr) {
20680 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20681 + if (NULL == cfiep) {
20682 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20683 + __func__, addr);
20684 + return -DWC_E_INVALID;
20685 + }
20686 + retval = cfi_reset_align_val(cfiep);
20687 + }
20688 + /* Otherwise (wValue == 0), reset all features of all EP's */
20689 + else {
20690 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20691 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20692 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20693 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20694 + retval = cfi_reset_align_val(cfiep);
20695 + if (retval < 0) {
20696 + CFI_INFO
20697 + ("%s: Error resetting the feature Aliignment Value\n",
20698 + __func__);
20699 + return retval;
20700 + }
20701 + }
20702 + }
20703 + return retval;
20704 +
20705 +}
20706 +
20707 +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
20708 + struct cfi_usb_ctrlrequest *req)
20709 +{
20710 + int retval = 0;
20711 +
20712 + switch (req->wIndex) {
20713 + case 0:
20714 + /* Reset all features */
20715 + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
20716 + break;
20717 +
20718 + case FT_ID_DMA_BUFFER_SETUP:
20719 + /* Reset the SG buffer setup */
20720 + retval =
20721 + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
20722 + break;
20723 +
20724 + case FT_ID_DMA_CONCAT_SETUP:
20725 + /* Reset the Concatenation buffer setup */
20726 + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
20727 + break;
20728 +
20729 + case FT_ID_DMA_BUFF_ALIGN:
20730 + /* Reset the Alignment buffer setup */
20731 + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
20732 + break;
20733 +
20734 + case FT_ID_TX_FIFO_DEPTH:
20735 + retval =
20736 + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
20737 + pcd->cfi->need_gadget_att = 0;
20738 + break;
20739 +
20740 + case FT_ID_RX_FIFO_DEPTH:
20741 + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
20742 + pcd->cfi->need_gadget_att = 0;
20743 + break;
20744 + default:
20745 + break;
20746 + }
20747 + return retval;
20748 +}
20749 +
20750 +/**
20751 + * This function sets a new value for the SG buffer setup.
20752 + */
20753 +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20754 +{
20755 + uint8_t inaddr, outaddr;
20756 + cfi_ep_t *epin, *epout;
20757 + ddma_sg_buffer_setup_t *psgval;
20758 + uint32_t desccount, size;
20759 +
20760 + CFI_INFO("%s\n", __func__);
20761 +
20762 + psgval = (ddma_sg_buffer_setup_t *) buf;
20763 + desccount = (uint32_t) psgval->bCount;
20764 + size = (uint32_t) psgval->wSize;
20765 +
20766 + /* Check the DMA descriptor count */
20767 + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
20768 + CFI_INFO
20769 + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
20770 + __func__, MAX_DMA_DESCS_PER_EP);
20771 + return -DWC_E_INVALID;
20772 + }
20773 +
20774 + /* Check the DMA descriptor count */
20775 +
20776 + if (size == 0) {
20777 +
20778 + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
20779 + __func__);
20780 +
20781 + return -DWC_E_INVALID;
20782 +
20783 + }
20784 +
20785 + inaddr = psgval->bInEndpointAddress;
20786 + outaddr = psgval->bOutEndpointAddress;
20787 +
20788 + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
20789 + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
20790 +
20791 + if (NULL == epin || NULL == epout) {
20792 + CFI_INFO
20793 + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
20794 + __func__, inaddr, outaddr);
20795 + return -DWC_E_INVALID;
20796 + }
20797 +
20798 + epin->ep->dwc_ep.buff_mode = BM_SG;
20799 + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
20800 +
20801 + epout->ep->dwc_ep.buff_mode = BM_SG;
20802 + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
20803 +
20804 + return 0;
20805 +}
20806 +
20807 +/**
20808 + * This function sets a new value for the buffer Alignment setup.
20809 + */
20810 +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20811 +{
20812 + cfi_ep_t *ep;
20813 + uint8_t addr;
20814 + ddma_align_buffer_setup_t *palignval;
20815 +
20816 + palignval = (ddma_align_buffer_setup_t *) buf;
20817 + addr = palignval->bEndpointAddress;
20818 +
20819 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20820 +
20821 + if (NULL == ep) {
20822 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20823 + __func__, addr);
20824 + return -DWC_E_INVALID;
20825 + }
20826 +
20827 + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
20828 + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
20829 +
20830 + return 0;
20831 +}
20832 +
20833 +/**
20834 + * This function sets a new value for the Concatenation buffer setup.
20835 + */
20836 +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20837 +{
20838 + uint8_t addr;
20839 + cfi_ep_t *ep;
20840 + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
20841 + uint16_t *pVals;
20842 + uint32_t desccount;
20843 + int i;
20844 + uint16_t mps;
20845 +
20846 + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
20847 + desccount = (uint32_t) pConcatValHdr->bDescCount;
20848 + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
20849 +
20850 + /* Check the DMA descriptor count */
20851 + if (desccount > MAX_DMA_DESCS_PER_EP) {
20852 + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
20853 + __func__, MAX_DMA_DESCS_PER_EP);
20854 + return -DWC_E_INVALID;
20855 + }
20856 +
20857 + addr = pConcatValHdr->bEndpointAddress;
20858 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20859 + if (NULL == ep) {
20860 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20861 + __func__, addr);
20862 + return -DWC_E_INVALID;
20863 + }
20864 +
20865 + mps = UGETW(ep->ep->desc->wMaxPacketSize);
20866 +
20867 +#if 0
20868 + for (i = 0; i < desccount; i++) {
20869 + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
20870 + }
20871 + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
20872 +#endif
20873 +
20874 + /* Check the wTxSizes to be less than or equal to the mps */
20875 + for (i = 0; i < desccount; i++) {
20876 + if (pVals[i] > mps) {
20877 + CFI_INFO
20878 + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
20879 + __func__, i, pVals[i]);
20880 + return -DWC_E_INVALID;
20881 + }
20882 + }
20883 +
20884 + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
20885 + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
20886 +
20887 + /* Free the previously allocated storage for the wTxBytes */
20888 + if (ep->bm_concat->wTxBytes) {
20889 + DWC_FREE(ep->bm_concat->wTxBytes);
20890 + }
20891 +
20892 + /* Allocate a new storage for the wTxBytes field */
20893 + ep->bm_concat->wTxBytes =
20894 + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
20895 + if (NULL == ep->bm_concat->wTxBytes) {
20896 + CFI_INFO("%s: Unable to allocate memory\n", __func__);
20897 + return -DWC_E_NO_MEMORY;
20898 + }
20899 +
20900 + /* Copy the new values into the wTxBytes filed */
20901 + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
20902 + sizeof(uint16_t) * pConcatValHdr->bDescCount);
20903 +
20904 + return 0;
20905 +}
20906 +
20907 +/**
20908 + * This function calculates the total of all FIFO sizes
20909 + *
20910 + * @param core_if Programming view of DWC_otg controller
20911 + *
20912 + * @return The total of data FIFO sizes.
20913 + *
20914 + */
20915 +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
20916 +{
20917 + dwc_otg_core_params_t *params = core_if->core_params;
20918 + uint16_t dfifo_total = 0;
20919 + int i;
20920 +
20921 + /* The shared RxFIFO size */
20922 + dfifo_total =
20923 + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
20924 +
20925 + /* Add up each TxFIFO size to the total */
20926 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20927 + dfifo_total += params->dev_tx_fifo_size[i];
20928 + }
20929 +
20930 + return dfifo_total;
20931 +}
20932 +
20933 +/**
20934 + * This function returns Rx FIFO size
20935 + *
20936 + * @param core_if Programming view of DWC_otg controller
20937 + *
20938 + * @return The total of data FIFO sizes.
20939 + *
20940 + */
20941 +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
20942 +{
20943 + switch (wValue >> 8) {
20944 + case 0:
20945 + return (core_if->pwron_rxfsiz <
20946 + 32768) ? core_if->pwron_rxfsiz : 32768;
20947 + break;
20948 + case 1:
20949 + return core_if->core_params->dev_rx_fifo_size;
20950 + break;
20951 + default:
20952 + return -DWC_E_INVALID;
20953 + break;
20954 + }
20955 +}
20956 +
20957 +/**
20958 + * This function returns Tx FIFO size for IN EP
20959 + *
20960 + * @param core_if Programming view of DWC_otg controller
20961 + *
20962 + * @return The total of data FIFO sizes.
20963 + *
20964 + */
20965 +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
20966 +{
20967 + dwc_otg_pcd_ep_t *ep;
20968 +
20969 + ep = get_ep_by_addr(pcd, wValue & 0xff);
20970 +
20971 + if (NULL == ep) {
20972 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20973 + __func__, wValue & 0xff);
20974 + return -DWC_E_INVALID;
20975 + }
20976 +
20977 + if (!ep->dwc_ep.is_in) {
20978 + CFI_INFO
20979 + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
20980 + __func__, wValue & 0xff);
20981 + return -DWC_E_INVALID;
20982 + }
20983 +
20984 + switch (wValue >> 8) {
20985 + case 0:
20986 + return (GET_CORE_IF(pcd)->pwron_txfsiz
20987 + [ep->dwc_ep.tx_fifo_num - 1] <
20988 + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
20989 + dwc_ep.tx_fifo_num
20990 + - 1] : 32768;
20991 + break;
20992 + case 1:
20993 + return GET_CORE_IF(pcd)->core_params->
20994 + dev_tx_fifo_size[ep->dwc_ep.num - 1];
20995 + break;
20996 + default:
20997 + return -DWC_E_INVALID;
20998 + break;
20999 + }
21000 +}
21001 +
21002 +/**
21003 + * This function checks if the submitted combination of
21004 + * device mode FIFO sizes is possible or not.
21005 + *
21006 + * @param core_if Programming view of DWC_otg controller
21007 + *
21008 + * @return 1 if possible, 0 otherwise.
21009 + *
21010 + */
21011 +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
21012 +{
21013 + uint16_t dfifo_actual = 0;
21014 + dwc_otg_core_params_t *params = core_if->core_params;
21015 + uint16_t start_addr = 0;
21016 + int i;
21017 +
21018 + dfifo_actual =
21019 + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
21020 +
21021 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21022 + dfifo_actual += params->dev_tx_fifo_size[i];
21023 + }
21024 +
21025 + if (dfifo_actual > core_if->total_fifo_size) {
21026 + return 0;
21027 + }
21028 +
21029 + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
21030 + return 0;
21031 +
21032 + if (params->dev_nperio_tx_fifo_size > 32768
21033 + || params->dev_nperio_tx_fifo_size < 16)
21034 + return 0;
21035 +
21036 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21037 +
21038 + if (params->dev_tx_fifo_size[i] > 768
21039 + || params->dev_tx_fifo_size[i] < 4)
21040 + return 0;
21041 + }
21042 +
21043 + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
21044 + return 0;
21045 + start_addr = params->dev_rx_fifo_size;
21046 +
21047 + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
21048 + return 0;
21049 + start_addr += params->dev_nperio_tx_fifo_size;
21050 +
21051 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21052 +
21053 + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
21054 + return 0;
21055 + start_addr += params->dev_tx_fifo_size[i];
21056 + }
21057 +
21058 + return 1;
21059 +}
21060 +
21061 +/**
21062 + * This function resizes Device mode FIFOs
21063 + *
21064 + * @param core_if Programming view of DWC_otg controller
21065 + *
21066 + * @return 1 if successful, 0 otherwise
21067 + *
21068 + */
21069 +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
21070 +{
21071 + int i = 0;
21072 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
21073 + dwc_otg_core_params_t *params = core_if->core_params;
21074 + uint32_t rx_fifo_size;
21075 + fifosize_data_t nptxfifosize;
21076 + fifosize_data_t txfifosize[15];
21077 +
21078 + uint32_t rx_fsz_bak;
21079 + uint32_t nptxfsz_bak;
21080 + uint32_t txfsz_bak[15];
21081 +
21082 + uint16_t start_address;
21083 + uint8_t retval = 1;
21084 +
21085 + if (!check_fifo_sizes(core_if)) {
21086 + return 0;
21087 + }
21088 +
21089 + /* Configure data FIFO sizes */
21090 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
21091 + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
21092 + rx_fifo_size = params->dev_rx_fifo_size;
21093 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
21094 +
21095 + /*
21096 + * Tx FIFOs These FIFOs are numbered from 1 to 15.
21097 + * Indexes of the FIFO size module parameters in the
21098 + * dev_tx_fifo_size array and the FIFO size registers in
21099 + * the dtxfsiz array run from 0 to 14.
21100 + */
21101 +
21102 + /* Non-periodic Tx FIFO */
21103 + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
21104 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
21105 + start_address = params->dev_rx_fifo_size;
21106 + nptxfifosize.b.startaddr = start_address;
21107 +
21108 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
21109 +
21110 + start_address += nptxfifosize.b.depth;
21111 +
21112 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21113 + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
21114 +
21115 + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
21116 + txfifosize[i].b.startaddr = start_address;
21117 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
21118 + txfifosize[i].d32);
21119 +
21120 + start_address += txfifosize[i].b.depth;
21121 + }
21122 +
21123 + /** Check if register values are set correctly */
21124 + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
21125 + retval = 0;
21126 + }
21127 +
21128 + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
21129 + retval = 0;
21130 + }
21131 +
21132 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21133 + if (txfifosize[i].d32 !=
21134 + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
21135 + retval = 0;
21136 + }
21137 + }
21138 +
21139 + /** If register values are not set correctly, reset old values */
21140 + if (retval == 0) {
21141 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
21142 +
21143 + /* Non-periodic Tx FIFO */
21144 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
21145 +
21146 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21147 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
21148 + txfsz_bak[i]);
21149 + }
21150 + }
21151 + } else {
21152 + return 0;
21153 + }
21154 +
21155 + /* Flush the FIFOs */
21156 + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
21157 + dwc_otg_flush_rx_fifo(core_if);
21158 +
21159 + return retval;
21160 +}
21161 +
21162 +/**
21163 + * This function sets a new value for the buffer Alignment setup.
21164 + */
21165 +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
21166 +{
21167 + int retval;
21168 + uint32_t fsiz;
21169 + uint16_t size;
21170 + uint16_t ep_addr;
21171 + dwc_otg_pcd_ep_t *ep;
21172 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
21173 + tx_fifo_size_setup_t *ptxfifoval;
21174 +
21175 + ptxfifoval = (tx_fifo_size_setup_t *) buf;
21176 + ep_addr = ptxfifoval->bEndpointAddress;
21177 + size = ptxfifoval->wDepth;
21178 +
21179 + ep = get_ep_by_addr(pcd, ep_addr);
21180 +
21181 + CFI_INFO
21182 + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
21183 + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
21184 +
21185 + if (NULL == ep) {
21186 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
21187 + __func__, ep_addr);
21188 + return -DWC_E_INVALID;
21189 + }
21190 +
21191 + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
21192 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
21193 +
21194 + if (resize_fifos(GET_CORE_IF(pcd))) {
21195 + retval = 0;
21196 + } else {
21197 + CFI_INFO
21198 + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
21199 + __func__, ep_addr);
21200 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
21201 + retval = -DWC_E_INVALID;
21202 + }
21203 +
21204 + return retval;
21205 +}
21206 +
21207 +/**
21208 + * This function sets a new value for the buffer Alignment setup.
21209 + */
21210 +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
21211 +{
21212 + int retval;
21213 + uint32_t fsiz;
21214 + uint16_t size;
21215 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
21216 + rx_fifo_size_setup_t *prxfifoval;
21217 +
21218 + prxfifoval = (rx_fifo_size_setup_t *) buf;
21219 + size = prxfifoval->wDepth;
21220 +
21221 + fsiz = params->dev_rx_fifo_size;
21222 + params->dev_rx_fifo_size = size;
21223 +
21224 + if (resize_fifos(GET_CORE_IF(pcd))) {
21225 + retval = 0;
21226 + } else {
21227 + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
21228 + __func__);
21229 + params->dev_rx_fifo_size = fsiz;
21230 + retval = -DWC_E_INVALID;
21231 + }
21232 +
21233 + return retval;
21234 +}
21235 +
21236 +/**
21237 + * This function reads the SG of an EP's buffer setup into the buffer buf
21238 + */
21239 +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21240 + struct cfi_usb_ctrlrequest *req)
21241 +{
21242 + int retval = -DWC_E_INVALID;
21243 + uint8_t addr;
21244 + cfi_ep_t *ep;
21245 +
21246 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21247 + addr = req->wValue & 0xFF;
21248 + if (addr == 0) /* The address should be non-zero */
21249 + return retval;
21250 +
21251 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21252 + if (NULL == ep) {
21253 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21254 + __func__, addr);
21255 + return retval;
21256 + }
21257 +
21258 + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
21259 + retval = BS_SG_VAL_DESC_LEN;
21260 + return retval;
21261 +}
21262 +
21263 +/**
21264 + * This function reads the Concatenation value of an EP's buffer mode into
21265 + * the buffer buf
21266 + */
21267 +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21268 + struct cfi_usb_ctrlrequest *req)
21269 +{
21270 + int retval = -DWC_E_INVALID;
21271 + uint8_t addr;
21272 + cfi_ep_t *ep;
21273 + uint8_t desc_count;
21274 +
21275 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21276 + addr = req->wValue & 0xFF;
21277 + if (addr == 0) /* The address should be non-zero */
21278 + return retval;
21279 +
21280 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21281 + if (NULL == ep) {
21282 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21283 + __func__, addr);
21284 + return retval;
21285 + }
21286 +
21287 + /* Copy the header to the buffer */
21288 + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
21289 + /* Advance the buffer pointer by the header size */
21290 + buf += BS_CONCAT_VAL_HDR_LEN;
21291 +
21292 + desc_count = ep->bm_concat->hdr.bDescCount;
21293 + /* Copy alll the wTxBytes to the buffer */
21294 + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
21295 +
21296 + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
21297 + return retval;
21298 +}
21299 +
21300 +/**
21301 + * This function reads the buffer Alignment value of an EP's buffer mode into
21302 + * the buffer buf
21303 + *
21304 + * @return The total number of bytes copied to the buffer or negative error code.
21305 + */
21306 +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21307 + struct cfi_usb_ctrlrequest *req)
21308 +{
21309 + int retval = -DWC_E_INVALID;
21310 + uint8_t addr;
21311 + cfi_ep_t *ep;
21312 +
21313 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21314 + addr = req->wValue & 0xFF;
21315 + if (addr == 0) /* The address should be non-zero */
21316 + return retval;
21317 +
21318 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21319 + if (NULL == ep) {
21320 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21321 + __func__, addr);
21322 + return retval;
21323 + }
21324 +
21325 + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
21326 + retval = BS_ALIGN_VAL_HDR_LEN;
21327 +
21328 + return retval;
21329 +}
21330 +
21331 +/**
21332 + * This function sets a new value for the specified feature
21333 + *
21334 + * @param pcd A pointer to the PCD object
21335 + *
21336 + * @return 0 if successful, negative error code otherwise to stall the DCE.
21337 + */
21338 +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
21339 +{
21340 + int retval = -DWC_E_NOT_SUPPORTED;
21341 + uint16_t wIndex, wValue;
21342 + uint8_t bRequest;
21343 + struct dwc_otg_core_if *coreif;
21344 + cfiobject_t *cfi = pcd->cfi;
21345 + struct cfi_usb_ctrlrequest *ctrl_req;
21346 + uint8_t *buf;
21347 + ctrl_req = &cfi->ctrl_req;
21348 +
21349 + buf = pcd->cfi->ctrl_req.data;
21350 +
21351 + coreif = GET_CORE_IF(pcd);
21352 + bRequest = ctrl_req->bRequest;
21353 + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
21354 + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
21355 +
21356 + /* See which feature is to be modified */
21357 + switch (wIndex) {
21358 + case FT_ID_DMA_BUFFER_SETUP:
21359 + /* Modify the feature */
21360 + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
21361 + return retval;
21362 +
21363 + /* And send this request to the gadget */
21364 + cfi->need_gadget_att = 1;
21365 + break;
21366 +
21367 + case FT_ID_DMA_BUFF_ALIGN:
21368 + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
21369 + return retval;
21370 + cfi->need_gadget_att = 1;
21371 + break;
21372 +
21373 + case FT_ID_DMA_CONCAT_SETUP:
21374 + /* Modify the feature */
21375 + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
21376 + return retval;
21377 + cfi->need_gadget_att = 1;
21378 + break;
21379 +
21380 + case FT_ID_DMA_CIRCULAR:
21381 + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
21382 + break;
21383 +
21384 + case FT_ID_THRESHOLD_SETUP:
21385 + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
21386 + break;
21387 +
21388 + case FT_ID_DFIFO_DEPTH:
21389 + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
21390 + break;
21391 +
21392 + case FT_ID_TX_FIFO_DEPTH:
21393 + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
21394 + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
21395 + return retval;
21396 + cfi->need_gadget_att = 0;
21397 + break;
21398 +
21399 + case FT_ID_RX_FIFO_DEPTH:
21400 + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
21401 + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
21402 + return retval;
21403 + cfi->need_gadget_att = 0;
21404 + break;
21405 + }
21406 +
21407 + return retval;
21408 +}
21409 +
21410 +#endif //DWC_UTE_CFI
21411 --- /dev/null
21412 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
21413 @@ -0,0 +1,320 @@
21414 +/* ==========================================================================
21415 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
21416 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
21417 + * otherwise expressly agreed to in writing between Synopsys and you.
21418 + *
21419 + * The Software IS NOT an item of Licensed Software or Licensed Product under
21420 + * any End User Software License Agreement or Agreement for Licensed Product
21421 + * with Synopsys or any supplement thereto. You are permitted to use and
21422 + * redistribute this Software in source and binary forms, with or without
21423 + * modification, provided that redistributions of source code must retain this
21424 + * notice. You may not view, use, disclose, copy or distribute this file or
21425 + * any information contained herein except pursuant to this license grant from
21426 + * Synopsys. If you do not agree with this notice, including the disclaimer
21427 + * below, then you are not authorized to use the Software.
21428 + *
21429 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
21430 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21431 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21432 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
21433 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21434 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21435 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21436 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21437 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21438 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
21439 + * DAMAGE.
21440 + * ========================================================================== */
21441 +
21442 +#if !defined(__DWC_OTG_CFI_H__)
21443 +#define __DWC_OTG_CFI_H__
21444 +
21445 +#include "dwc_otg_pcd.h"
21446 +#include "dwc_cfi_common.h"
21447 +
21448 +/**
21449 + * @file
21450 + * This file contains the CFI related OTG PCD specific common constants,
21451 + * interfaces(functions and macros) and data structures.The CFI Protocol is an
21452 + * optional interface for internal testing purposes that a DUT may implement to
21453 + * support testing of configurable features.
21454 + *
21455 + */
21456 +
21457 +struct dwc_otg_pcd;
21458 +struct dwc_otg_pcd_ep;
21459 +
21460 +/** OTG CFI Features (properties) ID constants */
21461 +/** This is a request for all Core Features */
21462 +#define FT_ID_DMA_MODE 0x0001
21463 +#define FT_ID_DMA_BUFFER_SETUP 0x0002
21464 +#define FT_ID_DMA_BUFF_ALIGN 0x0003
21465 +#define FT_ID_DMA_CONCAT_SETUP 0x0004
21466 +#define FT_ID_DMA_CIRCULAR 0x0005
21467 +#define FT_ID_THRESHOLD_SETUP 0x0006
21468 +#define FT_ID_DFIFO_DEPTH 0x0007
21469 +#define FT_ID_TX_FIFO_DEPTH 0x0008
21470 +#define FT_ID_RX_FIFO_DEPTH 0x0009
21471 +
21472 +/**********************************************************/
21473 +#define CFI_INFO_DEF
21474 +
21475 +#ifdef CFI_INFO_DEF
21476 +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
21477 +#else
21478 +#define CFI_INFO(fmt...)
21479 +#endif
21480 +
21481 +#define min(x,y) ({ \
21482 + x < y ? x : y; })
21483 +
21484 +#define max(x,y) ({ \
21485 + x > y ? x : y; })
21486 +
21487 +/**
21488 + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
21489 + * also used for setting up a buffer for Circular DDMA.
21490 + */
21491 +struct _ddma_sg_buffer_setup {
21492 +#define BS_SG_VAL_DESC_LEN 6
21493 + /* The OUT EP address */
21494 + uint8_t bOutEndpointAddress;
21495 + /* The IN EP address */
21496 + uint8_t bInEndpointAddress;
21497 + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
21498 + uint8_t bOffset;
21499 + /* The number of transfer segments (a DMA descriptors per each segment) */
21500 + uint8_t bCount;
21501 + /* Size (in byte) of each transfer segment */
21502 + uint16_t wSize;
21503 +} __attribute__ ((packed));
21504 +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
21505 +
21506 +/** Descriptor DMA Concatenation Buffer setup structure */
21507 +struct _ddma_concat_buffer_setup_hdr {
21508 +#define BS_CONCAT_VAL_HDR_LEN 4
21509 + /* The endpoint for which the buffer is to be set up */
21510 + uint8_t bEndpointAddress;
21511 + /* The count of descriptors to be used */
21512 + uint8_t bDescCount;
21513 + /* The total size of the transfer */
21514 + uint16_t wSize;
21515 +} __attribute__ ((packed));
21516 +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
21517 +
21518 +/** Descriptor DMA Concatenation Buffer setup structure */
21519 +struct _ddma_concat_buffer_setup {
21520 + /* The SG header */
21521 + ddma_concat_buffer_setup_hdr_t hdr;
21522 +
21523 + /* The XFER sizes pointer (allocated dynamically) */
21524 + uint16_t *wTxBytes;
21525 +} __attribute__ ((packed));
21526 +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
21527 +
21528 +/** Descriptor DMA Alignment Buffer setup structure */
21529 +struct _ddma_align_buffer_setup {
21530 +#define BS_ALIGN_VAL_HDR_LEN 2
21531 + uint8_t bEndpointAddress;
21532 + uint8_t bAlign;
21533 +} __attribute__ ((packed));
21534 +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
21535 +
21536 +/** Transmit FIFO Size setup structure */
21537 +struct _tx_fifo_size_setup {
21538 + uint8_t bEndpointAddress;
21539 + uint16_t wDepth;
21540 +} __attribute__ ((packed));
21541 +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
21542 +
21543 +/** Transmit FIFO Size setup structure */
21544 +struct _rx_fifo_size_setup {
21545 + uint16_t wDepth;
21546 +} __attribute__ ((packed));
21547 +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
21548 +
21549 +/**
21550 + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
21551 + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
21552 + * to the data returned in the data stage of a 3-stage Control Write requests.
21553 + */
21554 +struct cfi_usb_ctrlrequest {
21555 + uint8_t bRequestType;
21556 + uint8_t bRequest;
21557 + uint16_t wValue;
21558 + uint16_t wIndex;
21559 + uint16_t wLength;
21560 + uint8_t *data;
21561 +} UPACKED;
21562 +
21563 +/*---------------------------------------------------------------------------*/
21564 +
21565 +/**
21566 + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
21567 + * This structure is used to store the buffer setup data for any
21568 + * enabled endpoint in the PCD.
21569 + */
21570 +struct cfi_ep {
21571 + /* Entry for the list container */
21572 + dwc_list_link_t lh;
21573 + /* Pointer to the active PCD endpoint structure */
21574 + struct dwc_otg_pcd_ep *ep;
21575 + /* The last descriptor in the chain of DMA descriptors of the endpoint */
21576 + struct dwc_otg_dma_desc *dma_desc_last;
21577 + /* The SG feature value */
21578 + ddma_sg_buffer_setup_t *bm_sg;
21579 + /* The Circular feature value */
21580 + ddma_sg_buffer_setup_t *bm_circ;
21581 + /* The Concatenation feature value */
21582 + ddma_concat_buffer_setup_t *bm_concat;
21583 + /* The Alignment feature value */
21584 + ddma_align_buffer_setup_t *bm_align;
21585 + /* XFER length */
21586 + uint32_t xfer_len;
21587 + /*
21588 + * Count of DMA descriptors currently used.
21589 + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
21590 + * defined in the dwc_otg_cil.h
21591 + */
21592 + uint32_t desc_count;
21593 +};
21594 +typedef struct cfi_ep cfi_ep_t;
21595 +
21596 +typedef struct cfi_dma_buff {
21597 +#define CFI_IN_BUF_LEN 1024
21598 +#define CFI_OUT_BUF_LEN 1024
21599 + dma_addr_t addr;
21600 + uint8_t *buf;
21601 +} cfi_dma_buff_t;
21602 +
21603 +struct cfiobject;
21604 +
21605 +/**
21606 + * This is the interface for the CFI operations.
21607 + *
21608 + * @param ep_enable Called when any endpoint is enabled and activated.
21609 + * @param release Called when the CFI object is released and it needs to correctly
21610 + * deallocate the dynamic memory
21611 + * @param ctrl_write_complete Called when the data stage of the request is complete
21612 + */
21613 +typedef struct cfi_ops {
21614 + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
21615 + struct dwc_otg_pcd_ep * ep);
21616 + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
21617 + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
21618 + unsigned size, gfp_t flags);
21619 + void (*release) (struct cfiobject * cfi);
21620 + int (*ctrl_write_complete) (struct cfiobject * cfi,
21621 + struct dwc_otg_pcd * pcd);
21622 + void (*build_descriptors) (struct cfiobject * cfi,
21623 + struct dwc_otg_pcd * pcd,
21624 + struct dwc_otg_pcd_ep * ep,
21625 + dwc_otg_pcd_request_t * req);
21626 +} cfi_ops_t;
21627 +
21628 +struct cfiobject {
21629 + cfi_ops_t ops;
21630 + struct dwc_otg_pcd *pcd;
21631 + struct usb_gadget *gadget;
21632 +
21633 + /* Buffers used to send/receive CFI-related request data */
21634 + cfi_dma_buff_t buf_in;
21635 + cfi_dma_buff_t buf_out;
21636 +
21637 + /* CFI specific Control request wrapper */
21638 + struct cfi_usb_ctrlrequest ctrl_req;
21639 +
21640 + /* The list of active EP's in the PCD of type cfi_ep_t */
21641 + dwc_list_link_t active_eps;
21642 +
21643 + /* This flag shall control the propagation of a specific request
21644 + * to the gadget's processing routines.
21645 + * 0 - no gadget handling
21646 + * 1 - the gadget needs to know about this request (w/o completing a status
21647 + * phase - just return a 0 to the _setup callback)
21648 + */
21649 + uint8_t need_gadget_att;
21650 +
21651 + /* Flag indicating whether the status IN phase needs to be
21652 + * completed by the PCD
21653 + */
21654 + uint8_t need_status_in_complete;
21655 +};
21656 +typedef struct cfiobject cfiobject_t;
21657 +
21658 +#define DUMP_MSG
21659 +
21660 +#if defined(DUMP_MSG)
21661 +static inline void dump_msg(const u8 * buf, unsigned int length)
21662 +{
21663 + unsigned int start, num, i;
21664 + char line[52], *p;
21665 +
21666 + if (length >= 512)
21667 + return;
21668 +
21669 + start = 0;
21670 + while (length > 0) {
21671 + num = min(length, 16u);
21672 + p = line;
21673 + for (i = 0; i < num; ++i) {
21674 + if (i == 8)
21675 + *p++ = ' ';
21676 + DWC_SPRINTF(p, " %02x", buf[i]);
21677 + p += 3;
21678 + }
21679 + *p = 0;
21680 + DWC_DEBUG("%6x: %s\n", start, line);
21681 + buf += num;
21682 + start += num;
21683 + length -= num;
21684 + }
21685 +}
21686 +#else
21687 +static inline void dump_msg(const u8 * buf, unsigned int length)
21688 +{
21689 +}
21690 +#endif
21691 +
21692 +/**
21693 + * This function returns a pointer to cfi_ep_t object with the addr address.
21694 + */
21695 +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
21696 + uint8_t addr)
21697 +{
21698 + struct cfi_ep *pcfiep;
21699 + dwc_list_link_t *tmp;
21700 +
21701 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
21702 + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
21703 +
21704 + if (pcfiep->ep->desc->bEndpointAddress == addr) {
21705 + return pcfiep;
21706 + }
21707 + }
21708 +
21709 + return NULL;
21710 +}
21711 +
21712 +/**
21713 + * This function returns a pointer to cfi_ep_t object that matches
21714 + * the dwc_otg_pcd_ep object.
21715 + */
21716 +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
21717 + struct dwc_otg_pcd_ep *ep)
21718 +{
21719 + struct cfi_ep *pcfiep = NULL;
21720 + dwc_list_link_t *tmp;
21721 +
21722 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
21723 + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
21724 + if (pcfiep->ep == ep) {
21725 + return pcfiep;
21726 + }
21727 + }
21728 + return NULL;
21729 +}
21730 +
21731 +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
21732 +
21733 +#endif /* (__DWC_OTG_CFI_H__) */
21734 --- /dev/null
21735 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
21736 @@ -0,0 +1,7146 @@
21737 +/* ==========================================================================
21738 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
21739 + * $Revision: #191 $
21740 + * $Date: 2012/08/10 $
21741 + * $Change: 2047372 $
21742 + *
21743 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
21744 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
21745 + * otherwise expressly agreed to in writing between Synopsys and you.
21746 + *
21747 + * The Software IS NOT an item of Licensed Software or Licensed Product under
21748 + * any End User Software License Agreement or Agreement for Licensed Product
21749 + * with Synopsys or any supplement thereto. You are permitted to use and
21750 + * redistribute this Software in source and binary forms, with or without
21751 + * modification, provided that redistributions of source code must retain this
21752 + * notice. You may not view, use, disclose, copy or distribute this file or
21753 + * any information contained herein except pursuant to this license grant from
21754 + * Synopsys. If you do not agree with this notice, including the disclaimer
21755 + * below, then you are not authorized to use the Software.
21756 + *
21757 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
21758 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21759 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21760 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
21761 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21762 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21763 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21764 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21765 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21766 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
21767 + * DAMAGE.
21768 + * ========================================================================== */
21769 +
21770 +/** @file
21771 + *
21772 + * The Core Interface Layer provides basic services for accessing and
21773 + * managing the DWC_otg hardware. These services are used by both the
21774 + * Host Controller Driver and the Peripheral Controller Driver.
21775 + *
21776 + * The CIL manages the memory map for the core so that the HCD and PCD
21777 + * don't have to do this separately. It also handles basic tasks like
21778 + * reading/writing the registers and data FIFOs in the controller.
21779 + * Some of the data access functions provide encapsulation of several
21780 + * operations required to perform a task, such as writing multiple
21781 + * registers to start a transfer. Finally, the CIL performs basic
21782 + * services that are not specific to either the host or device modes
21783 + * of operation. These services include management of the OTG Host
21784 + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
21785 + * Diagnostic API is also provided to allow testing of the controller
21786 + * hardware.
21787 + *
21788 + * The Core Interface Layer has the following requirements:
21789 + * - Provides basic controller operations.
21790 + * - Minimal use of OS services.
21791 + * - The OS services used will be abstracted by using inline functions
21792 + * or macros.
21793 + *
21794 + */
21795 +
21796 +#include "dwc_os.h"
21797 +#include "dwc_otg_regs.h"
21798 +#include "dwc_otg_cil.h"
21799 +
21800 +extern bool cil_force_host;
21801 +
21802 +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
21803 +
21804 +/**
21805 + * This function is called to initialize the DWC_otg CSR data
21806 + * structures. The register addresses in the device and host
21807 + * structures are initialized from the base address supplied by the
21808 + * caller. The calling function must make the OS calls to get the
21809 + * base address of the DWC_otg controller registers. The core_params
21810 + * argument holds the parameters that specify how the core should be
21811 + * configured.
21812 + *
21813 + * @param reg_base_addr Base address of DWC_otg core registers
21814 + *
21815 + */
21816 +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
21817 +{
21818 + dwc_otg_core_if_t *core_if = 0;
21819 + dwc_otg_dev_if_t *dev_if = 0;
21820 + dwc_otg_host_if_t *host_if = 0;
21821 + uint8_t *reg_base = (uint8_t *) reg_base_addr;
21822 + int i = 0;
21823 +
21824 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
21825 +
21826 + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
21827 +
21828 + if (core_if == NULL) {
21829 + DWC_DEBUGPL(DBG_CIL,
21830 + "Allocation of dwc_otg_core_if_t failed\n");
21831 + return 0;
21832 + }
21833 + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
21834 +
21835 + /*
21836 + * Allocate the Device Mode structures.
21837 + */
21838 + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
21839 +
21840 + if (dev_if == NULL) {
21841 + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
21842 + DWC_FREE(core_if);
21843 + return 0;
21844 + }
21845 +
21846 + dev_if->dev_global_regs =
21847 + (dwc_otg_device_global_regs_t *) (reg_base +
21848 + DWC_DEV_GLOBAL_REG_OFFSET);
21849 +
21850 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21851 + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
21852 + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
21853 + (i * DWC_EP_REG_OFFSET));
21854 +
21855 + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
21856 + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
21857 + (i * DWC_EP_REG_OFFSET));
21858 + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
21859 + i, &dev_if->in_ep_regs[i]->diepctl);
21860 + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
21861 + i, &dev_if->out_ep_regs[i]->doepctl);
21862 + }
21863 +
21864 + dev_if->speed = 0; // unknown
21865 +
21866 + core_if->dev_if = dev_if;
21867 +
21868 + /*
21869 + * Allocate the Host Mode structures.
21870 + */
21871 + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
21872 +
21873 + if (host_if == NULL) {
21874 + DWC_DEBUGPL(DBG_CIL,
21875 + "Allocation of dwc_otg_host_if_t failed\n");
21876 + DWC_FREE(dev_if);
21877 + DWC_FREE(core_if);
21878 + return 0;
21879 + }
21880 +
21881 + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
21882 + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
21883 +
21884 + host_if->hprt0 =
21885 + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
21886 +
21887 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21888 + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
21889 + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
21890 + (i * DWC_OTG_CHAN_REGS_OFFSET));
21891 + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
21892 + i, &host_if->hc_regs[i]->hcchar);
21893 + }
21894 +
21895 + host_if->num_host_channels = MAX_EPS_CHANNELS;
21896 + core_if->host_if = host_if;
21897 +
21898 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21899 + core_if->data_fifo[i] =
21900 + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
21901 + (i * DWC_OTG_DATA_FIFO_SIZE));
21902 + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
21903 + i, (unsigned long)core_if->data_fifo[i]);
21904 + }
21905 +
21906 + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
21907 +
21908 + /* Initiate lx_state to L3 disconnected state */
21909 + core_if->lx_state = DWC_OTG_L3;
21910 + /*
21911 + * Store the contents of the hardware configuration registers here for
21912 + * easy access later.
21913 + */
21914 + core_if->hwcfg1.d32 =
21915 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
21916 + core_if->hwcfg2.d32 =
21917 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
21918 + core_if->hwcfg3.d32 =
21919 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
21920 + core_if->hwcfg4.d32 =
21921 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
21922 +
21923 + /* Force host mode to get HPTXFSIZ exact power on value */
21924 + {
21925 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
21926 + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
21927 + gusbcfg.b.force_host_mode = 1;
21928 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
21929 + dwc_mdelay(100);
21930 + core_if->hptxfsiz.d32 =
21931 + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
21932 + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
21933 + if (cil_force_host)
21934 + gusbcfg.b.force_host_mode = 1;
21935 + else
21936 + gusbcfg.b.force_host_mode = 0;
21937 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
21938 + dwc_mdelay(100);
21939 + }
21940 +
21941 + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
21942 + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
21943 + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
21944 + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
21945 +
21946 + core_if->hcfg.d32 =
21947 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
21948 + core_if->dcfg.d32 =
21949 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
21950 +
21951 + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
21952 + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
21953 +
21954 + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
21955 + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
21956 + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
21957 + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
21958 + core_if->hwcfg2.b.num_host_chan);
21959 + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
21960 + core_if->hwcfg2.b.nonperio_tx_q_depth);
21961 + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
21962 + core_if->hwcfg2.b.host_perio_tx_q_depth);
21963 + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
21964 + core_if->hwcfg2.b.dev_token_q_depth);
21965 +
21966 + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
21967 + core_if->hwcfg3.b.dfifo_depth);
21968 + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
21969 + core_if->hwcfg3.b.xfer_size_cntr_width);
21970 +
21971 + /*
21972 + * Set the SRP sucess bit for FS-I2c
21973 + */
21974 + core_if->srp_success = 0;
21975 + core_if->srp_timer_started = 0;
21976 +
21977 + /*
21978 + * Create new workqueue and init works
21979 + */
21980 + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
21981 + if (core_if->wq_otg == 0) {
21982 + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
21983 + DWC_FREE(host_if);
21984 + DWC_FREE(dev_if);
21985 + DWC_FREE(core_if);
21986 + return 0;
21987 + }
21988 +
21989 + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
21990 +
21991 + DWC_PRINTF("Core Release: %x.%x%x%x\n",
21992 + (core_if->snpsid >> 12 & 0xF),
21993 + (core_if->snpsid >> 8 & 0xF),
21994 + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
21995 +
21996 + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
21997 + w_wakeup_detected, core_if);
21998 + if (core_if->wkp_timer == 0) {
21999 + DWC_WARN("DWC_TIMER_ALLOC failed\n");
22000 + DWC_FREE(host_if);
22001 + DWC_FREE(dev_if);
22002 + DWC_WORKQ_FREE(core_if->wq_otg);
22003 + DWC_FREE(core_if);
22004 + return 0;
22005 + }
22006 +
22007 + if (dwc_otg_setup_params(core_if)) {
22008 + DWC_WARN("Error while setting core params\n");
22009 + }
22010 +
22011 + core_if->hibernation_suspend = 0;
22012 +
22013 + /** ADP initialization */
22014 + dwc_otg_adp_init(core_if);
22015 +
22016 + return core_if;
22017 +}
22018 +
22019 +/**
22020 + * This function frees the structures allocated by dwc_otg_cil_init().
22021 + *
22022 + * @param core_if The core interface pointer returned from
22023 + * dwc_otg_cil_init().
22024 + *
22025 + */
22026 +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
22027 +{
22028 + dctl_data_t dctl = {.d32 = 0 };
22029 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
22030 +
22031 + /* Disable all interrupts */
22032 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
22033 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
22034 +
22035 + dctl.b.sftdiscon = 1;
22036 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
22037 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
22038 + dctl.d32);
22039 + }
22040 +
22041 + if (core_if->wq_otg) {
22042 + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
22043 + DWC_WORKQ_FREE(core_if->wq_otg);
22044 + }
22045 + if (core_if->dev_if) {
22046 + DWC_FREE(core_if->dev_if);
22047 + }
22048 + if (core_if->host_if) {
22049 + DWC_FREE(core_if->host_if);
22050 + }
22051 +
22052 + /** Remove ADP Stuff */
22053 + dwc_otg_adp_remove(core_if);
22054 + if (core_if->core_params) {
22055 + DWC_FREE(core_if->core_params);
22056 + }
22057 + if (core_if->wkp_timer) {
22058 + DWC_TIMER_FREE(core_if->wkp_timer);
22059 + }
22060 + if (core_if->srp_timer) {
22061 + DWC_TIMER_FREE(core_if->srp_timer);
22062 + }
22063 + DWC_FREE(core_if);
22064 +}
22065 +
22066 +/**
22067 + * This function enables the controller's Global Interrupt in the AHB Config
22068 + * register.
22069 + *
22070 + * @param core_if Programming view of DWC_otg controller.
22071 + */
22072 +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
22073 +{
22074 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
22075 + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
22076 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
22077 +}
22078 +
22079 +/**
22080 + * This function disables the controller's Global Interrupt in the AHB Config
22081 + * register.
22082 + *
22083 + * @param core_if Programming view of DWC_otg controller.
22084 + */
22085 +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
22086 +{
22087 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
22088 + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
22089 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
22090 +}
22091 +
22092 +/**
22093 + * This function initializes the commmon interrupts, used in both
22094 + * device and host modes.
22095 + *
22096 + * @param core_if Programming view of the DWC_otg controller
22097 + *
22098 + */
22099 +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
22100 +{
22101 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
22102 + gintmsk_data_t intr_mask = {.d32 = 0 };
22103 +
22104 + /* Clear any pending OTG Interrupts */
22105 + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
22106 +
22107 + /* Clear any pending interrupts */
22108 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
22109 +
22110 + /*
22111 + * Enable the interrupts in the GINTMSK.
22112 + */
22113 + intr_mask.b.modemismatch = 1;
22114 + intr_mask.b.otgintr = 1;
22115 +
22116 + if (!core_if->dma_enable) {
22117 + intr_mask.b.rxstsqlvl = 1;
22118 + }
22119 +
22120 + intr_mask.b.conidstschng = 1;
22121 + intr_mask.b.wkupintr = 1;
22122 + intr_mask.b.disconnect = 0;
22123 + intr_mask.b.usbsuspend = 1;
22124 + intr_mask.b.sessreqintr = 1;
22125 +#ifdef CONFIG_USB_DWC_OTG_LPM
22126 + if (core_if->core_params->lpm_enable) {
22127 + intr_mask.b.lpmtranrcvd = 1;
22128 + }
22129 +#endif
22130 + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
22131 +}
22132 +
22133 +/*
22134 + * The restore operation is modified to support Synopsys Emulated Powerdown and
22135 + * Hibernation. This function is for exiting from Device mode hibernation by
22136 + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
22137 + * @param core_if Programming view of DWC_otg controller.
22138 + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
22139 + * @param reset - indicates whether resume is initiated by Reset.
22140 + */
22141 +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
22142 + int rem_wakeup, int reset)
22143 +{
22144 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
22145 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
22146 + dctl_data_t dctl = {.d32 = 0 };
22147 +
22148 + int timeout = 2000;
22149 +
22150 + if (!core_if->hibernation_suspend) {
22151 + DWC_PRINTF("Already exited from Hibernation\n");
22152 + return 1;
22153 + }
22154 +
22155 + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
22156 + /* Switch-on voltage to the core */
22157 + gpwrdn.b.pwrdnswtch = 1;
22158 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22159 + dwc_udelay(10);
22160 +
22161 + /* Reset core */
22162 + gpwrdn.d32 = 0;
22163 + gpwrdn.b.pwrdnrstn = 1;
22164 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22165 + dwc_udelay(10);
22166 +
22167 + /* Assert Restore signal */
22168 + gpwrdn.d32 = 0;
22169 + gpwrdn.b.restore = 1;
22170 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22171 + dwc_udelay(10);
22172 +
22173 + /* Disable power clamps */
22174 + gpwrdn.d32 = 0;
22175 + gpwrdn.b.pwrdnclmp = 1;
22176 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22177 +
22178 + if (rem_wakeup) {
22179 + dwc_udelay(70);
22180 + }
22181 +
22182 + /* Deassert Reset core */
22183 + gpwrdn.d32 = 0;
22184 + gpwrdn.b.pwrdnrstn = 1;
22185 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22186 + dwc_udelay(10);
22187 +
22188 + /* Disable PMU interrupt */
22189 + gpwrdn.d32 = 0;
22190 + gpwrdn.b.pmuintsel = 1;
22191 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22192 +
22193 + /* Mask interrupts from gpwrdn */
22194 + gpwrdn.d32 = 0;
22195 + gpwrdn.b.connect_det_msk = 1;
22196 + gpwrdn.b.srp_det_msk = 1;
22197 + gpwrdn.b.disconn_det_msk = 1;
22198 + gpwrdn.b.rst_det_msk = 1;
22199 + gpwrdn.b.lnstchng_msk = 1;
22200 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22201 +
22202 + /* Indicates that we are going out from hibernation */
22203 + core_if->hibernation_suspend = 0;
22204 +
22205 + /*
22206 + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
22207 + * indicates restore from remote_wakeup
22208 + */
22209 + restore_essential_regs(core_if, rem_wakeup, 0);
22210 +
22211 + /*
22212 + * Wait a little for seeing new value of variable hibernation_suspend if
22213 + * Restore done interrupt received before polling
22214 + */
22215 + dwc_udelay(10);
22216 +
22217 + if (core_if->hibernation_suspend == 0) {
22218 + /*
22219 + * Wait For Restore_done Interrupt. This mechanism of polling the
22220 + * interrupt is introduced to avoid any possible race conditions
22221 + */
22222 + do {
22223 + gintsts_data_t gintsts;
22224 + gintsts.d32 =
22225 + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
22226 + if (gintsts.b.restoredone) {
22227 + gintsts.d32 = 0;
22228 + gintsts.b.restoredone = 1;
22229 + DWC_WRITE_REG32(&core_if->core_global_regs->
22230 + gintsts, gintsts.d32);
22231 + DWC_PRINTF("Restore Done Interrupt seen\n");
22232 + break;
22233 + }
22234 + dwc_udelay(10);
22235 + } while (--timeout);
22236 + if (!timeout) {
22237 + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
22238 + }
22239 + }
22240 + /* Clear all pending interupts */
22241 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22242 +
22243 + /* De-assert Restore */
22244 + gpwrdn.d32 = 0;
22245 + gpwrdn.b.restore = 1;
22246 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22247 + dwc_udelay(10);
22248 +
22249 + if (!rem_wakeup) {
22250 + pcgcctl.d32 = 0;
22251 + pcgcctl.b.rstpdwnmodule = 1;
22252 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
22253 + }
22254 +
22255 + /* Restore GUSBCFG and DCFG */
22256 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
22257 + core_if->gr_backup->gusbcfg_local);
22258 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
22259 + core_if->dr_backup->dcfg);
22260 +
22261 + /* De-assert Wakeup Logic */
22262 + gpwrdn.d32 = 0;
22263 + gpwrdn.b.pmuactv = 1;
22264 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22265 + dwc_udelay(10);
22266 +
22267 + if (!rem_wakeup) {
22268 + /* Set Device programming done bit */
22269 + dctl.b.pwronprgdone = 1;
22270 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
22271 + } else {
22272 + /* Start Remote Wakeup Signaling */
22273 + dctl.d32 = core_if->dr_backup->dctl;
22274 + dctl.b.rmtwkupsig = 1;
22275 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
22276 + }
22277 +
22278 + dwc_mdelay(2);
22279 + /* Clear all pending interupts */
22280 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22281 +
22282 + /* Restore global registers */
22283 + dwc_otg_restore_global_regs(core_if);
22284 + /* Restore device global registers */
22285 + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
22286 +
22287 + if (rem_wakeup) {
22288 + dwc_mdelay(7);
22289 + dctl.d32 = 0;
22290 + dctl.b.rmtwkupsig = 1;
22291 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
22292 + }
22293 +
22294 + core_if->hibernation_suspend = 0;
22295 + /* The core will be in ON STATE */
22296 + core_if->lx_state = DWC_OTG_L0;
22297 + DWC_PRINTF("Hibernation recovery completes here\n");
22298 +
22299 + return 1;
22300 +}
22301 +
22302 +/*
22303 + * The restore operation is modified to support Synopsys Emulated Powerdown and
22304 + * Hibernation. This function is for exiting from Host mode hibernation by
22305 + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
22306 + * @param core_if Programming view of DWC_otg controller.
22307 + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
22308 + * @param reset - indicates whether resume is initiated by Reset.
22309 + */
22310 +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
22311 + int rem_wakeup, int reset)
22312 +{
22313 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
22314 + hprt0_data_t hprt0 = {.d32 = 0 };
22315 +
22316 + int timeout = 2000;
22317 +
22318 + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
22319 + /* Switch-on voltage to the core */
22320 + gpwrdn.b.pwrdnswtch = 1;
22321 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22322 + dwc_udelay(10);
22323 +
22324 + /* Reset core */
22325 + gpwrdn.d32 = 0;
22326 + gpwrdn.b.pwrdnrstn = 1;
22327 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22328 + dwc_udelay(10);
22329 +
22330 + /* Assert Restore signal */
22331 + gpwrdn.d32 = 0;
22332 + gpwrdn.b.restore = 1;
22333 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22334 + dwc_udelay(10);
22335 +
22336 + /* Disable power clamps */
22337 + gpwrdn.d32 = 0;
22338 + gpwrdn.b.pwrdnclmp = 1;
22339 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22340 +
22341 + if (!rem_wakeup) {
22342 + dwc_udelay(50);
22343 + }
22344 +
22345 + /* Deassert Reset core */
22346 + gpwrdn.d32 = 0;
22347 + gpwrdn.b.pwrdnrstn = 1;
22348 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22349 + dwc_udelay(10);
22350 +
22351 + /* Disable PMU interrupt */
22352 + gpwrdn.d32 = 0;
22353 + gpwrdn.b.pmuintsel = 1;
22354 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22355 +
22356 + gpwrdn.d32 = 0;
22357 + gpwrdn.b.connect_det_msk = 1;
22358 + gpwrdn.b.srp_det_msk = 1;
22359 + gpwrdn.b.disconn_det_msk = 1;
22360 + gpwrdn.b.rst_det_msk = 1;
22361 + gpwrdn.b.lnstchng_msk = 1;
22362 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22363 +
22364 + /* Indicates that we are going out from hibernation */
22365 + core_if->hibernation_suspend = 0;
22366 +
22367 + /* Set Restore Essential Regs bit in PCGCCTL register */
22368 + restore_essential_regs(core_if, rem_wakeup, 1);
22369 +
22370 + /* Wait a little for seeing new value of variable hibernation_suspend if
22371 + * Restore done interrupt received before polling */
22372 + dwc_udelay(10);
22373 +
22374 + if (core_if->hibernation_suspend == 0) {
22375 + /* Wait For Restore_done Interrupt. This mechanism of polling the
22376 + * interrupt is introduced to avoid any possible race conditions
22377 + */
22378 + do {
22379 + gintsts_data_t gintsts;
22380 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
22381 + if (gintsts.b.restoredone) {
22382 + gintsts.d32 = 0;
22383 + gintsts.b.restoredone = 1;
22384 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
22385 + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
22386 + break;
22387 + }
22388 + dwc_udelay(10);
22389 + } while (--timeout);
22390 + if (!timeout) {
22391 + DWC_WARN("Restore Done interrupt wasn't generated\n");
22392 + }
22393 + }
22394 +
22395 + /* Set the flag's value to 0 again after receiving restore done interrupt */
22396 + core_if->hibernation_suspend = 0;
22397 +
22398 + /* This step is not described in functional spec but if not wait for this
22399 + * delay, mismatch interrupts occurred because just after restore core is
22400 + * in Device mode(gintsts.curmode == 0) */
22401 + dwc_mdelay(100);
22402 +
22403 + /* Clear all pending interrupts */
22404 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22405 +
22406 + /* De-assert Restore */
22407 + gpwrdn.d32 = 0;
22408 + gpwrdn.b.restore = 1;
22409 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22410 + dwc_udelay(10);
22411 +
22412 + /* Restore GUSBCFG and HCFG */
22413 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
22414 + core_if->gr_backup->gusbcfg_local);
22415 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
22416 + core_if->hr_backup->hcfg_local);
22417 +
22418 + /* De-assert Wakeup Logic */
22419 + gpwrdn.d32 = 0;
22420 + gpwrdn.b.pmuactv = 1;
22421 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22422 + dwc_udelay(10);
22423 +
22424 + /* Start the Resume operation by programming HPRT0 */
22425 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22426 + hprt0.b.prtpwr = 1;
22427 + hprt0.b.prtena = 0;
22428 + hprt0.b.prtsusp = 0;
22429 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22430 +
22431 + DWC_PRINTF("Resume Starts Now\n");
22432 + if (!reset) { // Indicates it is Resume Operation
22433 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22434 + hprt0.b.prtres = 1;
22435 + hprt0.b.prtpwr = 1;
22436 + hprt0.b.prtena = 0;
22437 + hprt0.b.prtsusp = 0;
22438 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22439 +
22440 + if (!rem_wakeup)
22441 + hprt0.b.prtres = 0;
22442 + /* Wait for Resume time and then program HPRT again */
22443 + dwc_mdelay(100);
22444 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22445 +
22446 + } else { // Indicates it is Reset Operation
22447 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22448 + hprt0.b.prtrst = 1;
22449 + hprt0.b.prtpwr = 1;
22450 + hprt0.b.prtena = 0;
22451 + hprt0.b.prtsusp = 0;
22452 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22453 + /* Wait for Reset time and then program HPRT again */
22454 + dwc_mdelay(60);
22455 + hprt0.b.prtrst = 0;
22456 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22457 + }
22458 + /* Clear all interrupt status */
22459 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
22460 + hprt0.b.prtconndet = 1;
22461 + hprt0.b.prtenchng = 1;
22462 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22463 +
22464 + /* Clear all pending interupts */
22465 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22466 +
22467 + /* Restore global registers */
22468 + dwc_otg_restore_global_regs(core_if);
22469 + /* Restore host global registers */
22470 + dwc_otg_restore_host_regs(core_if, reset);
22471 +
22472 + /* The core will be in ON STATE */
22473 + core_if->lx_state = DWC_OTG_L0;
22474 + DWC_PRINTF("Hibernation recovery is complete here\n");
22475 + return 0;
22476 +}
22477 +
22478 +/** Saves some register values into system memory. */
22479 +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
22480 +{
22481 + struct dwc_otg_global_regs_backup *gr;
22482 + int i;
22483 +
22484 + gr = core_if->gr_backup;
22485 + if (!gr) {
22486 + gr = DWC_ALLOC(sizeof(*gr));
22487 + if (!gr) {
22488 + return -DWC_E_NO_MEMORY;
22489 + }
22490 + core_if->gr_backup = gr;
22491 + }
22492 +
22493 + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
22494 + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
22495 + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
22496 + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
22497 + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
22498 + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
22499 + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
22500 +#ifdef CONFIG_USB_DWC_OTG_LPM
22501 + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
22502 +#endif
22503 + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
22504 + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
22505 + gr->gdfifocfg_local =
22506 + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
22507 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
22508 + gr->dtxfsiz_local[i] =
22509 + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
22510 + }
22511 +
22512 + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
22513 + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
22514 + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
22515 + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
22516 + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
22517 + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
22518 + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
22519 + gr->gnptxfsiz_local);
22520 + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
22521 + gr->hptxfsiz_local);
22522 +#ifdef CONFIG_USB_DWC_OTG_LPM
22523 + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
22524 +#endif
22525 + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
22526 + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
22527 + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
22528 +
22529 + return 0;
22530 +}
22531 +
22532 +/** Saves GINTMSK register before setting the msk bits. */
22533 +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
22534 +{
22535 + struct dwc_otg_global_regs_backup *gr;
22536 +
22537 + gr = core_if->gr_backup;
22538 + if (!gr) {
22539 + gr = DWC_ALLOC(sizeof(*gr));
22540 + if (!gr) {
22541 + return -DWC_E_NO_MEMORY;
22542 + }
22543 + core_if->gr_backup = gr;
22544 + }
22545 +
22546 + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
22547 +
22548 + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
22549 + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
22550 +
22551 + return 0;
22552 +}
22553 +
22554 +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
22555 +{
22556 + struct dwc_otg_dev_regs_backup *dr;
22557 + int i;
22558 +
22559 + dr = core_if->dr_backup;
22560 + if (!dr) {
22561 + dr = DWC_ALLOC(sizeof(*dr));
22562 + if (!dr) {
22563 + return -DWC_E_NO_MEMORY;
22564 + }
22565 + core_if->dr_backup = dr;
22566 + }
22567 +
22568 + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
22569 + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
22570 + dr->daintmsk =
22571 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
22572 + dr->diepmsk =
22573 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
22574 + dr->doepmsk =
22575 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
22576 +
22577 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22578 + dr->diepctl[i] =
22579 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
22580 + dr->dieptsiz[i] =
22581 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
22582 + dr->diepdma[i] =
22583 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
22584 + }
22585 +
22586 + DWC_DEBUGPL(DBG_ANY,
22587 + "=============Backing Host registers==============\n");
22588 + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
22589 + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
22590 + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
22591 + dr->daintmsk);
22592 + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
22593 + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
22594 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22595 + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
22596 + dr->diepctl[i]);
22597 + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
22598 + i, dr->dieptsiz[i]);
22599 + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
22600 + dr->diepdma[i]);
22601 + }
22602 +
22603 + return 0;
22604 +}
22605 +
22606 +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
22607 +{
22608 + struct dwc_otg_host_regs_backup *hr;
22609 + int i;
22610 +
22611 + hr = core_if->hr_backup;
22612 + if (!hr) {
22613 + hr = DWC_ALLOC(sizeof(*hr));
22614 + if (!hr) {
22615 + return -DWC_E_NO_MEMORY;
22616 + }
22617 + core_if->hr_backup = hr;
22618 + }
22619 +
22620 + hr->hcfg_local =
22621 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
22622 + hr->haintmsk_local =
22623 + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
22624 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22625 + hr->hcintmsk_local[i] =
22626 + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
22627 + }
22628 + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
22629 + hr->hfir_local =
22630 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
22631 +
22632 + DWC_DEBUGPL(DBG_ANY,
22633 + "=============Backing Host registers===============\n");
22634 + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
22635 + hr->hcfg_local);
22636 + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
22637 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22638 + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
22639 + hr->hcintmsk_local[i]);
22640 + }
22641 + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
22642 + hr->hprt0_local);
22643 + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
22644 + hr->hfir_local);
22645 +
22646 + return 0;
22647 +}
22648 +
22649 +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
22650 +{
22651 + struct dwc_otg_global_regs_backup *gr;
22652 + int i;
22653 +
22654 + gr = core_if->gr_backup;
22655 + if (!gr) {
22656 + return -DWC_E_INVALID;
22657 + }
22658 +
22659 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
22660 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
22661 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
22662 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
22663 + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
22664 + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
22665 + gr->gnptxfsiz_local);
22666 + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
22667 + gr->hptxfsiz_local);
22668 + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
22669 + gr->gdfifocfg_local);
22670 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
22671 + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
22672 + gr->dtxfsiz_local[i]);
22673 + }
22674 +
22675 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22676 + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
22677 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
22678 + (gr->gahbcfg_local));
22679 + return 0;
22680 +}
22681 +
22682 +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
22683 +{
22684 + struct dwc_otg_dev_regs_backup *dr;
22685 + int i;
22686 +
22687 + dr = core_if->dr_backup;
22688 +
22689 + if (!dr) {
22690 + return -DWC_E_INVALID;
22691 + }
22692 +
22693 + if (!rem_wakeup) {
22694 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
22695 + dr->dctl);
22696 + }
22697 +
22698 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
22699 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
22700 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
22701 +
22702 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22703 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
22704 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
22705 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
22706 + }
22707 +
22708 + return 0;
22709 +}
22710 +
22711 +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
22712 +{
22713 + struct dwc_otg_host_regs_backup *hr;
22714 + int i;
22715 + hr = core_if->hr_backup;
22716 +
22717 + if (!hr) {
22718 + return -DWC_E_INVALID;
22719 + }
22720 +
22721 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
22722 + //if (!reset)
22723 + //{
22724 + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
22725 + //}
22726 +
22727 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
22728 + hr->haintmsk_local);
22729 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22730 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
22731 + hr->hcintmsk_local[i]);
22732 + }
22733 +
22734 + return 0;
22735 +}
22736 +
22737 +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
22738 +{
22739 + struct dwc_otg_global_regs_backup *gr;
22740 +
22741 + gr = core_if->gr_backup;
22742 +
22743 + /* Restore values for LPM and I2C */
22744 +#ifdef CONFIG_USB_DWC_OTG_LPM
22745 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
22746 +#endif
22747 + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
22748 +
22749 + return 0;
22750 +}
22751 +
22752 +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
22753 +{
22754 + struct dwc_otg_global_regs_backup *gr;
22755 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
22756 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
22757 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
22758 + gintmsk_data_t gintmsk = {.d32 = 0 };
22759 +
22760 + /* Restore LPM and I2C registers */
22761 + restore_lpm_i2c_regs(core_if);
22762 +
22763 + /* Set PCGCCTL to 0 */
22764 + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
22765 +
22766 + gr = core_if->gr_backup;
22767 + /* Load restore values for [31:14] bits */
22768 + DWC_WRITE_REG32(core_if->pcgcctl,
22769 + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
22770 +
22771 + /* Umnask global Interrupt in GAHBCFG and restore it */
22772 + gahbcfg.d32 = gr->gahbcfg_local;
22773 + gahbcfg.b.glblintrmsk = 1;
22774 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
22775 +
22776 + /* Clear all pending interupts */
22777 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22778 +
22779 + /* Unmask restore done interrupt */
22780 + gintmsk.b.restoredone = 1;
22781 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
22782 +
22783 + /* Restore GUSBCFG and HCFG/DCFG */
22784 + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
22785 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
22786 +
22787 + if (is_host) {
22788 + hcfg_data_t hcfg = {.d32 = 0 };
22789 + hcfg.d32 = core_if->hr_backup->hcfg_local;
22790 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
22791 + hcfg.d32);
22792 +
22793 + /* Load restore values for [31:14] bits */
22794 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22795 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22796 +
22797 + if (rmode)
22798 + pcgcctl.b.restoremode = 1;
22799 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22800 + dwc_udelay(10);
22801 +
22802 + /* Load restore values for [31:14] bits and set EssRegRestored bit */
22803 + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
22804 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22805 + pcgcctl.b.ess_reg_restored = 1;
22806 + if (rmode)
22807 + pcgcctl.b.restoremode = 1;
22808 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22809 + } else {
22810 + dcfg_data_t dcfg = {.d32 = 0 };
22811 + dcfg.d32 = core_if->dr_backup->dcfg;
22812 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
22813 +
22814 + /* Load restore values for [31:14] bits */
22815 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22816 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22817 + if (!rmode) {
22818 + pcgcctl.d32 |= 0x208;
22819 + }
22820 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22821 + dwc_udelay(10);
22822 +
22823 + /* Load restore values for [31:14] bits */
22824 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22825 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22826 + pcgcctl.b.ess_reg_restored = 1;
22827 + if (!rmode)
22828 + pcgcctl.d32 |= 0x208;
22829 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22830 + }
22831 +
22832 + return 0;
22833 +}
22834 +
22835 +/**
22836 + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
22837 + * type.
22838 + */
22839 +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
22840 +{
22841 + uint32_t val;
22842 + hcfg_data_t hcfg;
22843 +
22844 + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
22845 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22846 + (core_if->core_params->ulpi_fs_ls)) ||
22847 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22848 + /* Full speed PHY */
22849 + val = DWC_HCFG_48_MHZ;
22850 + } else {
22851 + /* High speed PHY running at full speed or high speed */
22852 + val = DWC_HCFG_30_60_MHZ;
22853 + }
22854 +
22855 + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
22856 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
22857 + hcfg.b.fslspclksel = val;
22858 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
22859 +}
22860 +
22861 +/**
22862 + * Initializes the DevSpd field of the DCFG register depending on the PHY type
22863 + * and the enumeration speed of the device.
22864 + */
22865 +static void init_devspd(dwc_otg_core_if_t * core_if)
22866 +{
22867 + uint32_t val;
22868 + dcfg_data_t dcfg;
22869 +
22870 + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
22871 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22872 + (core_if->core_params->ulpi_fs_ls)) ||
22873 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22874 + /* Full speed PHY */
22875 + val = 0x3;
22876 + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
22877 + /* High speed PHY running at full speed */
22878 + val = 0x1;
22879 + } else {
22880 + /* High speed PHY running at high speed */
22881 + val = 0x0;
22882 + }
22883 +
22884 + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
22885 +
22886 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
22887 + dcfg.b.devspd = val;
22888 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
22889 +}
22890 +
22891 +/**
22892 + * This function calculates the number of IN EPS
22893 + * using GHWCFG1 and GHWCFG2 registers values
22894 + *
22895 + * @param core_if Programming view of the DWC_otg controller
22896 + */
22897 +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
22898 +{
22899 + uint32_t num_in_eps = 0;
22900 + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
22901 + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
22902 + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
22903 + int i;
22904 +
22905 + for (i = 0; i < num_eps; ++i) {
22906 + if (!(hwcfg1 & 0x1))
22907 + num_in_eps++;
22908 +
22909 + hwcfg1 >>= 2;
22910 + }
22911 +
22912 + if (core_if->hwcfg4.b.ded_fifo_en) {
22913 + num_in_eps =
22914 + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
22915 + }
22916 +
22917 + return num_in_eps;
22918 +}
22919 +
22920 +/**
22921 + * This function calculates the number of OUT EPS
22922 + * using GHWCFG1 and GHWCFG2 registers values
22923 + *
22924 + * @param core_if Programming view of the DWC_otg controller
22925 + */
22926 +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
22927 +{
22928 + uint32_t num_out_eps = 0;
22929 + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
22930 + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
22931 + int i;
22932 +
22933 + for (i = 0; i < num_eps; ++i) {
22934 + if (!(hwcfg1 & 0x1))
22935 + num_out_eps++;
22936 +
22937 + hwcfg1 >>= 2;
22938 + }
22939 + return num_out_eps;
22940 +}
22941 +
22942 +/**
22943 + * This function initializes the DWC_otg controller registers and
22944 + * prepares the core for device mode or host mode operation.
22945 + *
22946 + * @param core_if Programming view of the DWC_otg controller
22947 + *
22948 + */
22949 +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
22950 +{
22951 + int i = 0;
22952 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
22953 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
22954 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
22955 + gusbcfg_data_t usbcfg = {.d32 = 0 };
22956 + gi2cctl_data_t i2cctl = {.d32 = 0 };
22957 +
22958 + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
22959 + core_if, global_regs);
22960 +
22961 + /* Common Initialization */
22962 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22963 +
22964 + /* Program the ULPI External VBUS bit if needed */
22965 + usbcfg.b.ulpi_ext_vbus_drv =
22966 + (core_if->core_params->phy_ulpi_ext_vbus ==
22967 + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
22968 +
22969 + /* Set external TS Dline pulsing */
22970 + usbcfg.b.term_sel_dl_pulse =
22971 + (core_if->core_params->ts_dline == 1) ? 1 : 0;
22972 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22973 +
22974 + /* Reset the Controller */
22975 + dwc_otg_core_reset(core_if);
22976 +
22977 + core_if->adp_enable = core_if->core_params->adp_supp_enable;
22978 + core_if->power_down = core_if->core_params->power_down;
22979 + core_if->otg_sts = 0;
22980 +
22981 + /* Initialize parameters from Hardware configuration registers. */
22982 + dev_if->num_in_eps = calc_num_in_eps(core_if);
22983 + dev_if->num_out_eps = calc_num_out_eps(core_if);
22984 +
22985 + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
22986 + core_if->hwcfg4.b.num_dev_perio_in_ep);
22987 +
22988 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
22989 + dev_if->perio_tx_fifo_size[i] =
22990 + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
22991 + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
22992 + i, dev_if->perio_tx_fifo_size[i]);
22993 + }
22994 +
22995 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
22996 + dev_if->tx_fifo_size[i] =
22997 + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
22998 + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
22999 + i, dev_if->tx_fifo_size[i]);
23000 + }
23001 +
23002 + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
23003 + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
23004 + core_if->nperio_tx_fifo_size =
23005 + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
23006 +
23007 + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
23008 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
23009 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
23010 + core_if->nperio_tx_fifo_size);
23011 +
23012 + /* This programming sequence needs to happen in FS mode before any other
23013 + * programming occurs */
23014 + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
23015 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
23016 + /* If FS mode with FS PHY */
23017 +
23018 + /* core_init() is now called on every switch so only call the
23019 + * following for the first time through. */
23020 + if (!core_if->phy_init_done) {
23021 + core_if->phy_init_done = 1;
23022 + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
23023 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
23024 + usbcfg.b.physel = 1;
23025 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23026 +
23027 + /* Reset after a PHY select */
23028 + dwc_otg_core_reset(core_if);
23029 + }
23030 +
23031 + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
23032 + * do this on HNP Dev/Host mode switches (done in dev_init and
23033 + * host_init). */
23034 + if (dwc_otg_is_host_mode(core_if)) {
23035 + init_fslspclksel(core_if);
23036 + } else {
23037 + init_devspd(core_if);
23038 + }
23039 +
23040 + if (core_if->core_params->i2c_enable) {
23041 + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
23042 + /* Program GUSBCFG.OtgUtmifsSel to I2C */
23043 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
23044 + usbcfg.b.otgutmifssel = 1;
23045 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23046 +
23047 + /* Program GI2CCTL.I2CEn */
23048 + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
23049 + i2cctl.b.i2cdevaddr = 1;
23050 + i2cctl.b.i2cen = 0;
23051 + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
23052 + i2cctl.b.i2cen = 1;
23053 + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
23054 + }
23055 +
23056 + } /* endif speed == DWC_SPEED_PARAM_FULL */
23057 + else {
23058 + /* High speed PHY. */
23059 + if (!core_if->phy_init_done) {
23060 + core_if->phy_init_done = 1;
23061 + /* HS PHY parameters. These parameters are preserved
23062 + * during soft reset so only program the first time. Do
23063 + * a soft reset immediately after setting phyif. */
23064 +
23065 + if (core_if->core_params->phy_type == 2) {
23066 + /* ULPI interface */
23067 + usbcfg.b.ulpi_utmi_sel = 1;
23068 + usbcfg.b.phyif = 0;
23069 + usbcfg.b.ddrsel =
23070 + core_if->core_params->phy_ulpi_ddr;
23071 + } else if (core_if->core_params->phy_type == 1) {
23072 + /* UTMI+ interface */
23073 + usbcfg.b.ulpi_utmi_sel = 0;
23074 + if (core_if->core_params->phy_utmi_width == 16) {
23075 + usbcfg.b.phyif = 1;
23076 +
23077 + } else {
23078 + usbcfg.b.phyif = 0;
23079 + }
23080 + } else {
23081 + DWC_ERROR("FS PHY TYPE\n");
23082 + }
23083 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23084 + /* Reset after setting the PHY parameters */
23085 + dwc_otg_core_reset(core_if);
23086 + }
23087 + }
23088 +
23089 + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
23090 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
23091 + (core_if->core_params->ulpi_fs_ls)) {
23092 + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
23093 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
23094 + usbcfg.b.ulpi_fsls = 1;
23095 + usbcfg.b.ulpi_clk_sus_m = 1;
23096 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23097 + } else {
23098 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
23099 + usbcfg.b.ulpi_fsls = 0;
23100 + usbcfg.b.ulpi_clk_sus_m = 0;
23101 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23102 + }
23103 +
23104 + /* Program the GAHBCFG Register. */
23105 + switch (core_if->hwcfg2.b.architecture) {
23106 +
23107 + case DWC_SLAVE_ONLY_ARCH:
23108 + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
23109 + ahbcfg.b.nptxfemplvl_txfemplvl =
23110 + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
23111 + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
23112 + core_if->dma_enable = 0;
23113 + core_if->dma_desc_enable = 0;
23114 + break;
23115 +
23116 + case DWC_EXT_DMA_ARCH:
23117 + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
23118 + {
23119 + uint8_t brst_sz = core_if->core_params->dma_burst_size;
23120 + ahbcfg.b.hburstlen = 0;
23121 + while (brst_sz > 1) {
23122 + ahbcfg.b.hburstlen++;
23123 + brst_sz >>= 1;
23124 + }
23125 + }
23126 + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
23127 + core_if->dma_desc_enable =
23128 + (core_if->core_params->dma_desc_enable != 0);
23129 + break;
23130 +
23131 + case DWC_INT_DMA_ARCH:
23132 + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
23133 + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
23134 + Host mode ISOC in issue fix - vahrama */
23135 + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
23136 + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
23137 + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
23138 + core_if->dma_desc_enable =
23139 + (core_if->core_params->dma_desc_enable != 0);
23140 + break;
23141 +
23142 + }
23143 + if (core_if->dma_enable) {
23144 + if (core_if->dma_desc_enable) {
23145 + DWC_PRINTF("Using Descriptor DMA mode\n");
23146 + } else {
23147 + DWC_PRINTF("Using Buffer DMA mode\n");
23148 +
23149 + }
23150 + } else {
23151 + DWC_PRINTF("Using Slave mode\n");
23152 + core_if->dma_desc_enable = 0;
23153 + }
23154 +
23155 + if (core_if->core_params->ahb_single) {
23156 + ahbcfg.b.ahbsingle = 1;
23157 + }
23158 +
23159 + ahbcfg.b.dmaenable = core_if->dma_enable;
23160 + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
23161 +
23162 + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
23163 +
23164 + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
23165 + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
23166 + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
23167 + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
23168 + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
23169 + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
23170 +
23171 + /*
23172 + * Program the GUSBCFG register.
23173 + */
23174 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
23175 +
23176 + switch (core_if->hwcfg2.b.op_mode) {
23177 + case DWC_MODE_HNP_SRP_CAPABLE:
23178 + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
23179 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
23180 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
23181 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
23182 + break;
23183 +
23184 + case DWC_MODE_SRP_ONLY_CAPABLE:
23185 + usbcfg.b.hnpcap = 0;
23186 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
23187 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
23188 + break;
23189 +
23190 + case DWC_MODE_NO_HNP_SRP_CAPABLE:
23191 + usbcfg.b.hnpcap = 0;
23192 + usbcfg.b.srpcap = 0;
23193 + break;
23194 +
23195 + case DWC_MODE_SRP_CAPABLE_DEVICE:
23196 + usbcfg.b.hnpcap = 0;
23197 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
23198 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
23199 + break;
23200 +
23201 + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
23202 + usbcfg.b.hnpcap = 0;
23203 + usbcfg.b.srpcap = 0;
23204 + break;
23205 +
23206 + case DWC_MODE_SRP_CAPABLE_HOST:
23207 + usbcfg.b.hnpcap = 0;
23208 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
23209 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
23210 + break;
23211 +
23212 + case DWC_MODE_NO_SRP_CAPABLE_HOST:
23213 + usbcfg.b.hnpcap = 0;
23214 + usbcfg.b.srpcap = 0;
23215 + break;
23216 + }
23217 +
23218 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23219 +
23220 +#ifdef CONFIG_USB_DWC_OTG_LPM
23221 + if (core_if->core_params->lpm_enable) {
23222 + glpmcfg_data_t lpmcfg = {.d32 = 0 };
23223 +
23224 + /* To enable LPM support set lpm_cap_en bit */
23225 + lpmcfg.b.lpm_cap_en = 1;
23226 +
23227 + /* Make AppL1Res ACK */
23228 + lpmcfg.b.appl_resp = 1;
23229 +
23230 + /* Retry 3 times */
23231 + lpmcfg.b.retry_count = 3;
23232 +
23233 + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
23234 + 0, lpmcfg.d32);
23235 +
23236 + }
23237 +#endif
23238 + if (core_if->core_params->ic_usb_cap) {
23239 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
23240 + gusbcfg.b.ic_usb_cap = 1;
23241 + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
23242 + 0, gusbcfg.d32);
23243 + }
23244 + {
23245 + gotgctl_data_t gotgctl = {.d32 = 0 };
23246 + gotgctl.b.otgver = core_if->core_params->otg_ver;
23247 + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
23248 + gotgctl.d32);
23249 + /* Set OTG version supported */
23250 + core_if->otg_ver = core_if->core_params->otg_ver;
23251 + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
23252 + core_if->core_params->otg_ver, core_if->otg_ver);
23253 + }
23254 +
23255 +
23256 + /* Enable common interrupts */
23257 + dwc_otg_enable_common_interrupts(core_if);
23258 +
23259 + /* Do device or host intialization based on mode during PCD
23260 + * and HCD initialization */
23261 + if (dwc_otg_is_host_mode(core_if)) {
23262 + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
23263 + core_if->op_state = A_HOST;
23264 + } else {
23265 + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
23266 + core_if->op_state = B_PERIPHERAL;
23267 +#ifdef DWC_DEVICE_ONLY
23268 + dwc_otg_core_dev_init(core_if);
23269 +#endif
23270 + }
23271 +}
23272 +
23273 +/**
23274 + * This function enables the Device mode interrupts.
23275 + *
23276 + * @param core_if Programming view of DWC_otg controller
23277 + */
23278 +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
23279 +{
23280 + gintmsk_data_t intr_mask = {.d32 = 0 };
23281 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23282 +
23283 + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
23284 +
23285 + /* Disable all interrupts. */
23286 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
23287 +
23288 + /* Clear any pending interrupts */
23289 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
23290 +
23291 + /* Enable the common interrupts */
23292 + dwc_otg_enable_common_interrupts(core_if);
23293 +
23294 + /* Enable interrupts */
23295 + intr_mask.b.usbreset = 1;
23296 + intr_mask.b.enumdone = 1;
23297 + /* Disable Disconnect interrupt in Device mode */
23298 + intr_mask.b.disconnect = 0;
23299 +
23300 + if (!core_if->multiproc_int_enable) {
23301 + intr_mask.b.inepintr = 1;
23302 + intr_mask.b.outepintr = 1;
23303 + }
23304 +
23305 + intr_mask.b.erlysuspend = 1;
23306 +
23307 + if (core_if->en_multiple_tx_fifo == 0) {
23308 + intr_mask.b.epmismatch = 1;
23309 + }
23310 +
23311 + //intr_mask.b.incomplisoout = 1;
23312 + intr_mask.b.incomplisoin = 1;
23313 +
23314 +/* Enable the ignore frame number for ISOC xfers - MAS */
23315 +/* Disable to support high bandwith ISOC transfers - manukz */
23316 +#if 0
23317 +#ifdef DWC_UTE_PER_IO
23318 + if (core_if->dma_enable) {
23319 + if (core_if->dma_desc_enable) {
23320 + dctl_data_t dctl1 = {.d32 = 0 };
23321 + dctl1.b.ifrmnum = 1;
23322 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
23323 + dctl, 0, dctl1.d32);
23324 + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
23325 + DWC_READ_REG32(&core_if->dev_if->
23326 + dev_global_regs->dctl));
23327 + }
23328 + }
23329 +#endif
23330 +#endif
23331 +#ifdef DWC_EN_ISOC
23332 + if (core_if->dma_enable) {
23333 + if (core_if->dma_desc_enable == 0) {
23334 + if (core_if->pti_enh_enable) {
23335 + dctl_data_t dctl = {.d32 = 0 };
23336 + dctl.b.ifrmnum = 1;
23337 + DWC_MODIFY_REG32(&core_if->
23338 + dev_if->dev_global_regs->dctl,
23339 + 0, dctl.d32);
23340 + } else {
23341 + intr_mask.b.incomplisoin = 1;
23342 + intr_mask.b.incomplisoout = 1;
23343 + }
23344 + }
23345 + } else {
23346 + intr_mask.b.incomplisoin = 1;
23347 + intr_mask.b.incomplisoout = 1;
23348 + }
23349 +#endif /* DWC_EN_ISOC */
23350 +
23351 + /** @todo NGS: Should this be a module parameter? */
23352 +#ifdef USE_PERIODIC_EP
23353 + intr_mask.b.isooutdrop = 1;
23354 + intr_mask.b.eopframe = 1;
23355 + intr_mask.b.incomplisoin = 1;
23356 + intr_mask.b.incomplisoout = 1;
23357 +#endif
23358 +
23359 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
23360 +
23361 + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
23362 + DWC_READ_REG32(&global_regs->gintmsk));
23363 +}
23364 +
23365 +/**
23366 + * This function initializes the DWC_otg controller registers for
23367 + * device mode.
23368 + *
23369 + * @param core_if Programming view of DWC_otg controller
23370 + *
23371 + */
23372 +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
23373 +{
23374 + int i;
23375 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23376 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
23377 + dwc_otg_core_params_t *params = core_if->core_params;
23378 + dcfg_data_t dcfg = {.d32 = 0 };
23379 + depctl_data_t diepctl = {.d32 = 0 };
23380 + grstctl_t resetctl = {.d32 = 0 };
23381 + uint32_t rx_fifo_size;
23382 + fifosize_data_t nptxfifosize;
23383 + fifosize_data_t txfifosize;
23384 + dthrctl_data_t dthrctl;
23385 + fifosize_data_t ptxfifosize;
23386 + uint16_t rxfsiz, nptxfsiz;
23387 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
23388 + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
23389 +
23390 + /* Restart the Phy Clock */
23391 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
23392 +
23393 + /* Device configuration register */
23394 + init_devspd(core_if);
23395 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
23396 + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
23397 + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
23398 + /* Enable Device OUT NAK in case of DDMA mode*/
23399 + if (core_if->core_params->dev_out_nak) {
23400 + dcfg.b.endevoutnak = 1;
23401 + }
23402 +
23403 + if (core_if->core_params->cont_on_bna) {
23404 + dctl_data_t dctl = {.d32 = 0 };
23405 + dctl.b.encontonbna = 1;
23406 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
23407 + }
23408 +
23409 +
23410 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
23411 +
23412 + /* Configure data FIFO sizes */
23413 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
23414 + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
23415 + core_if->total_fifo_size);
23416 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
23417 + params->dev_rx_fifo_size);
23418 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
23419 + params->dev_nperio_tx_fifo_size);
23420 +
23421 + /* Rx FIFO */
23422 + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
23423 + DWC_READ_REG32(&global_regs->grxfsiz));
23424 +
23425 +#ifdef DWC_UTE_CFI
23426 + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
23427 + core_if->init_rxfsiz = params->dev_rx_fifo_size;
23428 +#endif
23429 + rx_fifo_size = params->dev_rx_fifo_size;
23430 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
23431 +
23432 + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
23433 + DWC_READ_REG32(&global_regs->grxfsiz));
23434 +
23435 + /** Set Periodic Tx FIFO Mask all bits 0 */
23436 + core_if->p_tx_msk = 0;
23437 +
23438 + /** Set Tx FIFO Mask all bits 0 */
23439 + core_if->tx_msk = 0;
23440 +
23441 + if (core_if->en_multiple_tx_fifo == 0) {
23442 + /* Non-periodic Tx FIFO */
23443 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23444 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23445 +
23446 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
23447 + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
23448 +
23449 + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
23450 + nptxfifosize.d32);
23451 +
23452 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23453 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23454 +
23455 + /**@todo NGS: Fix Periodic FIFO Sizing! */
23456 + /*
23457 + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
23458 + * Indexes of the FIFO size module parameters in the
23459 + * dev_perio_tx_fifo_size array and the FIFO size registers in
23460 + * the dptxfsiz array run from 0 to 14.
23461 + */
23462 + /** @todo Finish debug of this */
23463 + ptxfifosize.b.startaddr =
23464 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23465 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
23466 + ptxfifosize.b.depth =
23467 + params->dev_perio_tx_fifo_size[i];
23468 + DWC_DEBUGPL(DBG_CIL,
23469 + "initial dtxfsiz[%d]=%08x\n", i,
23470 + DWC_READ_REG32(&global_regs->dtxfsiz
23471 + [i]));
23472 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
23473 + ptxfifosize.d32);
23474 + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
23475 + i,
23476 + DWC_READ_REG32(&global_regs->dtxfsiz
23477 + [i]));
23478 + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
23479 + }
23480 + } else {
23481 + /*
23482 + * Tx FIFOs These FIFOs are numbered from 1 to 15.
23483 + * Indexes of the FIFO size module parameters in the
23484 + * dev_tx_fifo_size array and the FIFO size registers in
23485 + * the dtxfsiz array run from 0 to 14.
23486 + */
23487 +
23488 + /* Non-periodic Tx FIFO */
23489 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23490 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23491 +
23492 +#ifdef DWC_UTE_CFI
23493 + core_if->pwron_gnptxfsiz =
23494 + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23495 + core_if->init_gnptxfsiz =
23496 + params->dev_nperio_tx_fifo_size;
23497 +#endif
23498 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
23499 + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
23500 +
23501 + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
23502 + nptxfifosize.d32);
23503 +
23504 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23505 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23506 +
23507 + txfifosize.b.startaddr =
23508 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23509 +
23510 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
23511 +
23512 + txfifosize.b.depth =
23513 + params->dev_tx_fifo_size[i];
23514 +
23515 + DWC_DEBUGPL(DBG_CIL,
23516 + "initial dtxfsiz[%d]=%08x\n",
23517 + i,
23518 + DWC_READ_REG32(&global_regs->dtxfsiz
23519 + [i]));
23520 +
23521 +#ifdef DWC_UTE_CFI
23522 + core_if->pwron_txfsiz[i] =
23523 + (DWC_READ_REG32
23524 + (&global_regs->dtxfsiz[i]) >> 16);
23525 + core_if->init_txfsiz[i] =
23526 + params->dev_tx_fifo_size[i];
23527 +#endif
23528 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
23529 + txfifosize.d32);
23530 +
23531 + DWC_DEBUGPL(DBG_CIL,
23532 + "new dtxfsiz[%d]=%08x\n",
23533 + i,
23534 + DWC_READ_REG32(&global_regs->dtxfsiz
23535 + [i]));
23536 +
23537 + txfifosize.b.startaddr += txfifosize.b.depth;
23538 + }
23539 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
23540 + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
23541 + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
23542 + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
23543 + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
23544 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23545 + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
23546 + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23547 + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
23548 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23549 + }
23550 + }
23551 +
23552 + /* Flush the FIFOs */
23553 + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
23554 + dwc_otg_flush_rx_fifo(core_if);
23555 +
23556 + /* Flush the Learning Queue. */
23557 + resetctl.b.intknqflsh = 1;
23558 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
23559 +
23560 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
23561 + core_if->start_predict = 0;
23562 + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
23563 + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
23564 + }
23565 + core_if->nextep_seq[0] = 0;
23566 + core_if->first_in_nextep_seq = 0;
23567 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
23568 + diepctl.b.nextep = 0;
23569 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
23570 +
23571 + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
23572 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
23573 + dcfg.b.epmscnt = 2;
23574 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
23575 +
23576 + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
23577 + __func__, core_if->first_in_nextep_seq);
23578 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
23579 + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
23580 + }
23581 + DWC_DEBUGPL(DBG_CILV,"\n");
23582 + }
23583 +
23584 + /* Clear all pending Device Interrupts */
23585 + /** @todo - if the condition needed to be checked
23586 + * or in any case all pending interrutps should be cleared?
23587 + */
23588 + if (core_if->multiproc_int_enable) {
23589 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
23590 + DWC_WRITE_REG32(&dev_if->
23591 + dev_global_regs->diepeachintmsk[i], 0);
23592 + }
23593 + }
23594 +
23595 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
23596 + DWC_WRITE_REG32(&dev_if->
23597 + dev_global_regs->doepeachintmsk[i], 0);
23598 + }
23599 +
23600 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
23601 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
23602 + } else {
23603 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
23604 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
23605 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
23606 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
23607 + }
23608 +
23609 + for (i = 0; i <= dev_if->num_in_eps; i++) {
23610 + depctl_data_t depctl;
23611 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
23612 + if (depctl.b.epena) {
23613 + depctl.d32 = 0;
23614 + depctl.b.epdis = 1;
23615 + depctl.b.snak = 1;
23616 + } else {
23617 + depctl.d32 = 0;
23618 + }
23619 +
23620 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
23621 +
23622 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
23623 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
23624 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
23625 + }
23626 +
23627 + for (i = 0; i <= dev_if->num_out_eps; i++) {
23628 + depctl_data_t depctl;
23629 + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
23630 + if (depctl.b.epena) {
23631 + dctl_data_t dctl = {.d32 = 0 };
23632 + gintmsk_data_t gintsts = {.d32 = 0 };
23633 + doepint_data_t doepint = {.d32 = 0 };
23634 + dctl.b.sgoutnak = 1;
23635 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
23636 + do {
23637 + dwc_udelay(10);
23638 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
23639 + } while (!gintsts.b.goutnakeff);
23640 + gintsts.d32 = 0;
23641 + gintsts.b.goutnakeff = 1;
23642 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
23643 +
23644 + depctl.d32 = 0;
23645 + depctl.b.epdis = 1;
23646 + depctl.b.snak = 1;
23647 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
23648 + do {
23649 + dwc_udelay(10);
23650 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
23651 + out_ep_regs[i]->doepint);
23652 + } while (!doepint.b.epdisabled);
23653 +
23654 + doepint.b.epdisabled = 1;
23655 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
23656 +
23657 + dctl.d32 = 0;
23658 + dctl.b.cgoutnak = 1;
23659 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
23660 + } else {
23661 + depctl.d32 = 0;
23662 + }
23663 +
23664 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
23665 +
23666 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
23667 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
23668 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
23669 + }
23670 +
23671 + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
23672 + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
23673 + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
23674 + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
23675 +
23676 + dev_if->rx_thr_length = params->rx_thr_length;
23677 + dev_if->tx_thr_length = params->tx_thr_length;
23678 +
23679 + dev_if->setup_desc_index = 0;
23680 +
23681 + dthrctl.d32 = 0;
23682 + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
23683 + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
23684 + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
23685 + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
23686 + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
23687 + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
23688 +
23689 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
23690 + dthrctl.d32);
23691 +
23692 + DWC_DEBUGPL(DBG_CIL,
23693 + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
23694 + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
23695 + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
23696 + dthrctl.b.rx_thr_len);
23697 +
23698 + }
23699 +
23700 + dwc_otg_enable_device_interrupts(core_if);
23701 +
23702 + {
23703 + diepmsk_data_t msk = {.d32 = 0 };
23704 + msk.b.txfifoundrn = 1;
23705 + if (core_if->multiproc_int_enable) {
23706 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
23707 + diepeachintmsk[0], msk.d32, msk.d32);
23708 + } else {
23709 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
23710 + msk.d32, msk.d32);
23711 + }
23712 + }
23713 +
23714 + if (core_if->multiproc_int_enable) {
23715 + /* Set NAK on Babble */
23716 + dctl_data_t dctl = {.d32 = 0 };
23717 + dctl.b.nakonbble = 1;
23718 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
23719 + }
23720 +
23721 + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
23722 + dctl_data_t dctl = {.d32 = 0 };
23723 + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
23724 + dctl.b.sftdiscon = 0;
23725 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
23726 + }
23727 +}
23728 +
23729 +/**
23730 + * This function enables the Host mode interrupts.
23731 + *
23732 + * @param core_if Programming view of DWC_otg controller
23733 + */
23734 +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
23735 +{
23736 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23737 + gintmsk_data_t intr_mask = {.d32 = 0 };
23738 +
23739 + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
23740 +
23741 + /* Disable all interrupts. */
23742 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
23743 +
23744 + /* Clear any pending interrupts. */
23745 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
23746 +
23747 + /* Enable the common interrupts */
23748 + dwc_otg_enable_common_interrupts(core_if);
23749 +
23750 + /*
23751 + * Enable host mode interrupts without disturbing common
23752 + * interrupts.
23753 + */
23754 +
23755 + intr_mask.b.disconnect = 1;
23756 + intr_mask.b.portintr = 1;
23757 + intr_mask.b.hcintr = 1;
23758 +
23759 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
23760 +}
23761 +
23762 +/**
23763 + * This function disables the Host Mode interrupts.
23764 + *
23765 + * @param core_if Programming view of DWC_otg controller
23766 + */
23767 +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
23768 +{
23769 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23770 + gintmsk_data_t intr_mask = {.d32 = 0 };
23771 +
23772 + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
23773 +
23774 + /*
23775 + * Disable host mode interrupts without disturbing common
23776 + * interrupts.
23777 + */
23778 + intr_mask.b.sofintr = 1;
23779 + intr_mask.b.portintr = 1;
23780 + intr_mask.b.hcintr = 1;
23781 + intr_mask.b.ptxfempty = 1;
23782 + intr_mask.b.nptxfempty = 1;
23783 +
23784 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
23785 +}
23786 +
23787 +/**
23788 + * This function initializes the DWC_otg controller registers for
23789 + * host mode.
23790 + *
23791 + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
23792 + * request queues. Host channels are reset to ensure that they are ready for
23793 + * performing transfers.
23794 + *
23795 + * @param core_if Programming view of DWC_otg controller
23796 + *
23797 + */
23798 +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
23799 +{
23800 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23801 + dwc_otg_host_if_t *host_if = core_if->host_if;
23802 + dwc_otg_core_params_t *params = core_if->core_params;
23803 + hprt0_data_t hprt0 = {.d32 = 0 };
23804 + fifosize_data_t nptxfifosize;
23805 + fifosize_data_t ptxfifosize;
23806 + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
23807 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
23808 + int i;
23809 + hcchar_data_t hcchar;
23810 + hcfg_data_t hcfg;
23811 + hfir_data_t hfir;
23812 + dwc_otg_hc_regs_t *hc_regs;
23813 + int num_channels;
23814 + gotgctl_data_t gotgctl = {.d32 = 0 };
23815 +
23816 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
23817 +
23818 + /* Restart the Phy Clock */
23819 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
23820 +
23821 + /* Initialize Host Configuration Register */
23822 + init_fslspclksel(core_if);
23823 + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
23824 + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
23825 + hcfg.b.fslssupp = 1;
23826 + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
23827 +
23828 + }
23829 +
23830 + /* This bit allows dynamic reloading of the HFIR register
23831 + * during runtime. This bit needs to be programmed during
23832 + * initial configuration and its value must not be changed
23833 + * during runtime.*/
23834 + if (core_if->core_params->reload_ctl == 1) {
23835 + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
23836 + hfir.b.hfirrldctrl = 1;
23837 + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
23838 + }
23839 +
23840 + if (core_if->core_params->dma_desc_enable) {
23841 + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
23842 + if (!
23843 + (core_if->hwcfg4.b.desc_dma
23844 + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
23845 + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
23846 + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
23847 + || (op_mode ==
23848 + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
23849 + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
23850 + || (op_mode ==
23851 + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
23852 +
23853 + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
23854 + "Either core version is below 2.90a or "
23855 + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
23856 + "To run the driver in Buffer DMA host mode set dma_desc_enable "
23857 + "module parameter to 0.\n");
23858 + return;
23859 + }
23860 + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
23861 + hcfg.b.descdma = 1;
23862 + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
23863 + }
23864 +
23865 + /* Configure data FIFO sizes */
23866 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
23867 + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
23868 + core_if->total_fifo_size);
23869 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
23870 + params->host_rx_fifo_size);
23871 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
23872 + params->host_nperio_tx_fifo_size);
23873 + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
23874 + params->host_perio_tx_fifo_size);
23875 +
23876 + /* Rx FIFO */
23877 + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
23878 + DWC_READ_REG32(&global_regs->grxfsiz));
23879 + DWC_WRITE_REG32(&global_regs->grxfsiz,
23880 + params->host_rx_fifo_size);
23881 + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
23882 + DWC_READ_REG32(&global_regs->grxfsiz));
23883 +
23884 + /* Non-periodic Tx FIFO */
23885 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23886 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23887 + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
23888 + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
23889 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
23890 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23891 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23892 +
23893 + /* Periodic Tx FIFO */
23894 + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
23895 + DWC_READ_REG32(&global_regs->hptxfsiz));
23896 + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
23897 + ptxfifosize.b.startaddr =
23898 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23899 + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
23900 + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
23901 + DWC_READ_REG32(&global_regs->hptxfsiz));
23902 +
23903 + if (core_if->en_multiple_tx_fifo
23904 + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
23905 + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
23906 + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
23907 + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
23908 + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23909 + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
23910 + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
23911 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23912 + }
23913 + }
23914 +
23915 + /* TODO - check this */
23916 + /* Clear Host Set HNP Enable in the OTG Control Register */
23917 + gotgctl.b.hstsethnpen = 1;
23918 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
23919 + /* Make sure the FIFOs are flushed. */
23920 + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
23921 + dwc_otg_flush_rx_fifo(core_if);
23922 +
23923 + /* Clear Host Set HNP Enable in the OTG Control Register */
23924 + gotgctl.b.hstsethnpen = 1;
23925 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
23926 +
23927 + if (!core_if->core_params->dma_desc_enable) {
23928 + /* Flush out any leftover queued requests. */
23929 + num_channels = core_if->core_params->host_channels;
23930 +
23931 + for (i = 0; i < num_channels; i++) {
23932 + hc_regs = core_if->host_if->hc_regs[i];
23933 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23934 + hcchar.b.chen = 0;
23935 + hcchar.b.chdis = 1;
23936 + hcchar.b.epdir = 0;
23937 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23938 + }
23939 +
23940 + /* Halt all channels to put them into a known state. */
23941 + for (i = 0; i < num_channels; i++) {
23942 + int count = 0;
23943 + hc_regs = core_if->host_if->hc_regs[i];
23944 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23945 + hcchar.b.chen = 1;
23946 + hcchar.b.chdis = 1;
23947 + hcchar.b.epdir = 0;
23948 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23949 + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
23950 + do {
23951 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23952 + if (++count > 1000) {
23953 + DWC_ERROR
23954 + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
23955 + __func__, i, hcchar.d32, &hc_regs->hcchar);
23956 + break;
23957 + }
23958 + dwc_udelay(1);
23959 + } while (hcchar.b.chen);
23960 + }
23961 + }
23962 +
23963 + /* Turn on the vbus power. */
23964 + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
23965 + if (core_if->op_state == A_HOST) {
23966 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
23967 + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
23968 + if (hprt0.b.prtpwr == 0) {
23969 + hprt0.b.prtpwr = 1;
23970 + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
23971 + }
23972 + }
23973 +
23974 + dwc_otg_enable_host_interrupts(core_if);
23975 +}
23976 +
23977 +/**
23978 + * Prepares a host channel for transferring packets to/from a specific
23979 + * endpoint. The HCCHARn register is set up with the characteristics specified
23980 + * in _hc. Host channel interrupts that may need to be serviced while this
23981 + * transfer is in progress are enabled.
23982 + *
23983 + * @param core_if Programming view of DWC_otg controller
23984 + * @param hc Information needed to initialize the host channel
23985 + */
23986 +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
23987 +{
23988 + hcintmsk_data_t hc_intr_mask;
23989 + hcchar_data_t hcchar;
23990 + hcsplt_data_t hcsplt;
23991 +
23992 + uint8_t hc_num = hc->hc_num;
23993 + dwc_otg_host_if_t *host_if = core_if->host_if;
23994 + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
23995 +
23996 + /* Clear old interrupt conditions for this host channel. */
23997 + hc_intr_mask.d32 = 0xFFFFFFFF;
23998 + hc_intr_mask.b.reserved14_31 = 0;
23999 + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
24000 +
24001 + /* Enable channel interrupts required for this transfer. */
24002 + hc_intr_mask.d32 = 0;
24003 + hc_intr_mask.b.chhltd = 1;
24004 + if (core_if->dma_enable) {
24005 + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
24006 + if (!core_if->dma_desc_enable)
24007 + hc_intr_mask.b.ahberr = 1;
24008 + else {
24009 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24010 + hc_intr_mask.b.xfercompl = 1;
24011 + }
24012 +
24013 + if (hc->error_state && !hc->do_split &&
24014 + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
24015 + hc_intr_mask.b.ack = 1;
24016 + if (hc->ep_is_in) {
24017 + hc_intr_mask.b.datatglerr = 1;
24018 + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
24019 + hc_intr_mask.b.nak = 1;
24020 + }
24021 + }
24022 + }
24023 + } else {
24024 + switch (hc->ep_type) {
24025 + case DWC_OTG_EP_TYPE_CONTROL:
24026 + case DWC_OTG_EP_TYPE_BULK:
24027 + hc_intr_mask.b.xfercompl = 1;
24028 + hc_intr_mask.b.stall = 1;
24029 + hc_intr_mask.b.xacterr = 1;
24030 + hc_intr_mask.b.datatglerr = 1;
24031 + if (hc->ep_is_in) {
24032 + hc_intr_mask.b.bblerr = 1;
24033 + } else {
24034 + hc_intr_mask.b.nak = 1;
24035 + hc_intr_mask.b.nyet = 1;
24036 + if (hc->do_ping) {
24037 + hc_intr_mask.b.ack = 1;
24038 + }
24039 + }
24040 +
24041 + if (hc->do_split) {
24042 + hc_intr_mask.b.nak = 1;
24043 + if (hc->complete_split) {
24044 + hc_intr_mask.b.nyet = 1;
24045 + } else {
24046 + hc_intr_mask.b.ack = 1;
24047 + }
24048 + }
24049 +
24050 + if (hc->error_state) {
24051 + hc_intr_mask.b.ack = 1;
24052 + }
24053 + break;
24054 + case DWC_OTG_EP_TYPE_INTR:
24055 + hc_intr_mask.b.xfercompl = 1;
24056 + hc_intr_mask.b.nak = 1;
24057 + hc_intr_mask.b.stall = 1;
24058 + hc_intr_mask.b.xacterr = 1;
24059 + hc_intr_mask.b.datatglerr = 1;
24060 + hc_intr_mask.b.frmovrun = 1;
24061 +
24062 + if (hc->ep_is_in) {
24063 + hc_intr_mask.b.bblerr = 1;
24064 + }
24065 + if (hc->error_state) {
24066 + hc_intr_mask.b.ack = 1;
24067 + }
24068 + if (hc->do_split) {
24069 + if (hc->complete_split) {
24070 + hc_intr_mask.b.nyet = 1;
24071 + } else {
24072 + hc_intr_mask.b.ack = 1;
24073 + }
24074 + }
24075 + break;
24076 + case DWC_OTG_EP_TYPE_ISOC:
24077 + hc_intr_mask.b.xfercompl = 1;
24078 + hc_intr_mask.b.frmovrun = 1;
24079 + hc_intr_mask.b.ack = 1;
24080 +
24081 + if (hc->ep_is_in) {
24082 + hc_intr_mask.b.xacterr = 1;
24083 + hc_intr_mask.b.bblerr = 1;
24084 + }
24085 + break;
24086 + }
24087 + }
24088 + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
24089 +
24090 + /*
24091 + * Program the HCCHARn register with the endpoint characteristics for
24092 + * the current transfer.
24093 + */
24094 + hcchar.d32 = 0;
24095 + hcchar.b.devaddr = hc->dev_addr;
24096 + hcchar.b.epnum = hc->ep_num;
24097 + hcchar.b.epdir = hc->ep_is_in;
24098 + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
24099 + hcchar.b.eptype = hc->ep_type;
24100 + hcchar.b.mps = hc->max_packet;
24101 +
24102 + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
24103 +
24104 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
24105 + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
24106 + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
24107 + "Max Pkt %d, Multi Cnt %d\n",
24108 + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
24109 + hcchar.b.mps, hcchar.b.multicnt);
24110 +
24111 + /*
24112 + * Program the HCSPLIT register for SPLITs
24113 + */
24114 + hcsplt.d32 = 0;
24115 + if (hc->do_split) {
24116 + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
24117 + hc->hc_num,
24118 + hc->complete_split ? "CSPLIT" : "SSPLIT");
24119 + hcsplt.b.compsplt = hc->complete_split;
24120 + hcsplt.b.xactpos = hc->xact_pos;
24121 + hcsplt.b.hubaddr = hc->hub_addr;
24122 + hcsplt.b.prtaddr = hc->port_addr;
24123 + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
24124 + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
24125 + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
24126 + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
24127 + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
24128 + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
24129 + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
24130 + }
24131 + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
24132 +
24133 +}
24134 +
24135 +/**
24136 + * Attempts to halt a host channel. This function should only be called in
24137 + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
24138 + * normal circumstances in DMA mode, the controller halts the channel when the
24139 + * transfer is complete or a condition occurs that requires application
24140 + * intervention.
24141 + *
24142 + * In slave mode, checks for a free request queue entry, then sets the Channel
24143 + * Enable and Channel Disable bits of the Host Channel Characteristics
24144 + * register of the specified channel to intiate the halt. If there is no free
24145 + * request queue entry, sets only the Channel Disable bit of the HCCHARn
24146 + * register to flush requests for this channel. In the latter case, sets a
24147 + * flag to indicate that the host channel needs to be halted when a request
24148 + * queue slot is open.
24149 + *
24150 + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
24151 + * HCCHARn register. The controller ensures there is space in the request
24152 + * queue before submitting the halt request.
24153 + *
24154 + * Some time may elapse before the core flushes any posted requests for this
24155 + * host channel and halts. The Channel Halted interrupt handler completes the
24156 + * deactivation of the host channel.
24157 + *
24158 + * @param core_if Controller register interface.
24159 + * @param hc Host channel to halt.
24160 + * @param halt_status Reason for halting the channel.
24161 + */
24162 +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
24163 + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
24164 +{
24165 + gnptxsts_data_t nptxsts;
24166 + hptxsts_data_t hptxsts;
24167 + hcchar_data_t hcchar;
24168 + dwc_otg_hc_regs_t *hc_regs;
24169 + dwc_otg_core_global_regs_t *global_regs;
24170 + dwc_otg_host_global_regs_t *host_global_regs;
24171 +
24172 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24173 + global_regs = core_if->core_global_regs;
24174 + host_global_regs = core_if->host_if->host_global_regs;
24175 +
24176 + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
24177 + "halt_status = %d\n", halt_status);
24178 +
24179 + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
24180 + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
24181 + /*
24182 + * Disable all channel interrupts except Ch Halted. The QTD
24183 + * and QH state associated with this transfer has been cleared
24184 + * (in the case of URB_DEQUEUE), so the channel needs to be
24185 + * shut down carefully to prevent crashes.
24186 + */
24187 + hcintmsk_data_t hcintmsk;
24188 + hcintmsk.d32 = 0;
24189 + hcintmsk.b.chhltd = 1;
24190 + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
24191 +
24192 + /*
24193 + * Make sure no other interrupts besides halt are currently
24194 + * pending. Handling another interrupt could cause a crash due
24195 + * to the QTD and QH state.
24196 + */
24197 + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
24198 +
24199 + /*
24200 + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
24201 + * even if the channel was already halted for some other
24202 + * reason.
24203 + */
24204 + hc->halt_status = halt_status;
24205 +
24206 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24207 + if (hcchar.b.chen == 0) {
24208 + /*
24209 + * The channel is either already halted or it hasn't
24210 + * started yet. In DMA mode, the transfer may halt if
24211 + * it finishes normally or a condition occurs that
24212 + * requires driver intervention. Don't want to halt
24213 + * the channel again. In either Slave or DMA mode,
24214 + * it's possible that the transfer has been assigned
24215 + * to a channel, but not started yet when an URB is
24216 + * dequeued. Don't want to halt a channel that hasn't
24217 + * started yet.
24218 + */
24219 + return;
24220 + }
24221 + }
24222 + if (hc->halt_pending) {
24223 + /*
24224 + * A halt has already been issued for this channel. This might
24225 + * happen when a transfer is aborted by a higher level in
24226 + * the stack.
24227 + */
24228 +#ifdef DEBUG
24229 + DWC_PRINTF
24230 + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
24231 + __func__, hc->hc_num);
24232 +
24233 +#endif
24234 + return;
24235 + }
24236 +
24237 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24238 +
24239 + /* No need to set the bit in DDMA for disabling the channel */
24240 + //TODO check it everywhere channel is disabled
24241 + if (!core_if->core_params->dma_desc_enable)
24242 + hcchar.b.chen = 1;
24243 + hcchar.b.chdis = 1;
24244 +
24245 + if (!core_if->dma_enable) {
24246 + /* Check for space in the request queue to issue the halt. */
24247 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
24248 + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
24249 + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
24250 + if (nptxsts.b.nptxqspcavail == 0) {
24251 + hcchar.b.chen = 0;
24252 + }
24253 + } else {
24254 + hptxsts.d32 =
24255 + DWC_READ_REG32(&host_global_regs->hptxsts);
24256 + if ((hptxsts.b.ptxqspcavail == 0)
24257 + || (core_if->queuing_high_bandwidth)) {
24258 + hcchar.b.chen = 0;
24259 + }
24260 + }
24261 + }
24262 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24263 +
24264 + hc->halt_status = halt_status;
24265 +
24266 + if (hcchar.b.chen) {
24267 + hc->halt_pending = 1;
24268 + hc->halt_on_queue = 0;
24269 + } else {
24270 + hc->halt_on_queue = 1;
24271 + }
24272 +
24273 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24274 + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
24275 + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
24276 + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
24277 + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
24278 +
24279 + return;
24280 +}
24281 +
24282 +/**
24283 + * Clears the transfer state for a host channel. This function is normally
24284 + * called after a transfer is done and the host channel is being released.
24285 + *
24286 + * @param core_if Programming view of DWC_otg controller.
24287 + * @param hc Identifies the host channel to clean up.
24288 + */
24289 +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24290 +{
24291 + dwc_otg_hc_regs_t *hc_regs;
24292 +
24293 + hc->xfer_started = 0;
24294 +
24295 + /*
24296 + * Clear channel interrupt enables and any unhandled channel interrupt
24297 + * conditions.
24298 + */
24299 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24300 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
24301 + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
24302 +#ifdef DEBUG
24303 + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
24304 +#endif
24305 +}
24306 +
24307 +/**
24308 + * Sets the channel property that indicates in which frame a periodic transfer
24309 + * should occur. This is always set to the _next_ frame. This function has no
24310 + * effect on non-periodic transfers.
24311 + *
24312 + * @param core_if Programming view of DWC_otg controller.
24313 + * @param hc Identifies the host channel to set up and its properties.
24314 + * @param hcchar Current value of the HCCHAR register for the specified host
24315 + * channel.
24316 + */
24317 +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
24318 + dwc_hc_t * hc, hcchar_data_t * hcchar)
24319 +{
24320 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24321 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24322 + hfnum_data_t hfnum;
24323 + hfnum.d32 =
24324 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
24325 +
24326 + /* 1 if _next_ frame is odd, 0 if it's even */
24327 + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
24328 +#ifdef DEBUG
24329 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
24330 + && !hc->complete_split) {
24331 + switch (hfnum.b.frnum & 0x7) {
24332 + case 7:
24333 + core_if->hfnum_7_samples++;
24334 + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
24335 + break;
24336 + case 0:
24337 + core_if->hfnum_0_samples++;
24338 + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
24339 + break;
24340 + default:
24341 + core_if->hfnum_other_samples++;
24342 + core_if->hfnum_other_frrem_accum +=
24343 + hfnum.b.frrem;
24344 + break;
24345 + }
24346 + }
24347 +#endif
24348 + }
24349 +}
24350 +
24351 +#ifdef DEBUG
24352 +void hc_xfer_timeout(void *ptr)
24353 +{
24354 + hc_xfer_info_t *xfer_info = NULL;
24355 + int hc_num = 0;
24356 +
24357 + if (ptr)
24358 + xfer_info = (hc_xfer_info_t *) ptr;
24359 +
24360 + if (!xfer_info->hc) {
24361 + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
24362 + return;
24363 + }
24364 +
24365 + hc_num = xfer_info->hc->hc_num;
24366 + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
24367 + DWC_WARN(" start_hcchar_val 0x%08x\n",
24368 + xfer_info->core_if->start_hcchar_val[hc_num]);
24369 +}
24370 +#endif
24371 +
24372 +void ep_xfer_timeout(void *ptr)
24373 +{
24374 + ep_xfer_info_t *xfer_info = NULL;
24375 + int ep_num = 0;
24376 + dctl_data_t dctl = {.d32 = 0 };
24377 + gintsts_data_t gintsts = {.d32 = 0 };
24378 + gintmsk_data_t gintmsk = {.d32 = 0 };
24379 +
24380 + if (ptr)
24381 + xfer_info = (ep_xfer_info_t *) ptr;
24382 +
24383 + if (!xfer_info->ep) {
24384 + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
24385 + return;
24386 + }
24387 +
24388 + ep_num = xfer_info->ep->num;
24389 + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
24390 + /* Put the sate to 2 as it was time outed */
24391 + xfer_info->state = 2;
24392 +
24393 + dctl.d32 =
24394 + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
24395 + gintsts.d32 =
24396 + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
24397 + gintmsk.d32 =
24398 + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
24399 +
24400 + if (!gintmsk.b.goutnakeff) {
24401 + /* Unmask it */
24402 + gintmsk.b.goutnakeff = 1;
24403 + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
24404 + gintmsk.d32);
24405 +
24406 + }
24407 +
24408 + if (!gintsts.b.goutnakeff) {
24409 + dctl.b.sgoutnak = 1;
24410 + }
24411 + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
24412 + dctl.d32);
24413 +
24414 +}
24415 +
24416 +void set_pid_isoc(dwc_hc_t * hc)
24417 +{
24418 + /* Set up the initial PID for the transfer. */
24419 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
24420 + if (hc->ep_is_in) {
24421 + if (hc->multi_count == 1) {
24422 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24423 + } else if (hc->multi_count == 2) {
24424 + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
24425 + } else {
24426 + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
24427 + }
24428 + } else {
24429 + if (hc->multi_count == 1) {
24430 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24431 + } else {
24432 + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
24433 + }
24434 + }
24435 + } else {
24436 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24437 + }
24438 +}
24439 +
24440 +/**
24441 + * This function does the setup for a data transfer for a host channel and
24442 + * starts the transfer. May be called in either Slave mode or DMA mode. In
24443 + * Slave mode, the caller must ensure that there is sufficient space in the
24444 + * request queue and Tx Data FIFO.
24445 + *
24446 + * For an OUT transfer in Slave mode, it loads a data packet into the
24447 + * appropriate FIFO. If necessary, additional data packets will be loaded in
24448 + * the Host ISR.
24449 + *
24450 + * For an IN transfer in Slave mode, a data packet is requested. The data
24451 + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
24452 + * additional data packets are requested in the Host ISR.
24453 + *
24454 + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
24455 + * register along with a packet count of 1 and the channel is enabled. This
24456 + * causes a single PING transaction to occur. Other fields in HCTSIZ are
24457 + * simply set to 0 since no data transfer occurs in this case.
24458 + *
24459 + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
24460 + * all the information required to perform the subsequent data transfer. In
24461 + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
24462 + * controller performs the entire PING protocol, then starts the data
24463 + * transfer.
24464 + *
24465 + * @param core_if Programming view of DWC_otg controller.
24466 + * @param hc Information needed to initialize the host channel. The xfer_len
24467 + * value may be reduced to accommodate the max widths of the XferSize and
24468 + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
24469 + * to reflect the final xfer_len value.
24470 + */
24471 +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24472 +{
24473 + hcchar_data_t hcchar;
24474 + hctsiz_data_t hctsiz;
24475 + uint16_t num_packets;
24476 + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
24477 + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
24478 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24479 +
24480 + hctsiz.d32 = 0;
24481 +
24482 + if (hc->do_ping) {
24483 + if (!core_if->dma_enable) {
24484 + dwc_otg_hc_do_ping(core_if, hc);
24485 + hc->xfer_started = 1;
24486 + return;
24487 + } else {
24488 + hctsiz.b.dopng = 1;
24489 + }
24490 + }
24491 +
24492 + if (hc->do_split) {
24493 + num_packets = 1;
24494 +
24495 + if (hc->complete_split && !hc->ep_is_in) {
24496 + /* For CSPLIT OUT Transfer, set the size to 0 so the
24497 + * core doesn't expect any data written to the FIFO */
24498 + hc->xfer_len = 0;
24499 + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
24500 + hc->xfer_len = hc->max_packet;
24501 + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
24502 + hc->xfer_len = 188;
24503 + }
24504 +
24505 + hctsiz.b.xfersize = hc->xfer_len;
24506 + } else {
24507 + /*
24508 + * Ensure that the transfer length and packet count will fit
24509 + * in the widths allocated for them in the HCTSIZn register.
24510 + */
24511 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24512 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24513 + /*
24514 + * Make sure the transfer size is no larger than one
24515 + * (micro)frame's worth of data. (A check was done
24516 + * when the periodic transfer was accepted to ensure
24517 + * that a (micro)frame's worth of data can be
24518 + * programmed into a channel.)
24519 + */
24520 + uint32_t max_periodic_len =
24521 + hc->multi_count * hc->max_packet;
24522 + if (hc->xfer_len > max_periodic_len) {
24523 + hc->xfer_len = max_periodic_len;
24524 + } else {
24525 + }
24526 + } else if (hc->xfer_len > max_hc_xfer_size) {
24527 + /* Make sure that xfer_len is a multiple of max packet size. */
24528 + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
24529 + }
24530 +
24531 + if (hc->xfer_len > 0) {
24532 + num_packets =
24533 + (hc->xfer_len + hc->max_packet -
24534 + 1) / hc->max_packet;
24535 + if (num_packets > max_hc_pkt_count) {
24536 + num_packets = max_hc_pkt_count;
24537 + hc->xfer_len = num_packets * hc->max_packet;
24538 + }
24539 + } else {
24540 + /* Need 1 packet for transfer length of 0. */
24541 + num_packets = 1;
24542 + }
24543 +
24544 + if (hc->ep_is_in) {
24545 + /* Always program an integral # of max packets for IN transfers. */
24546 + hc->xfer_len = num_packets * hc->max_packet;
24547 + }
24548 +
24549 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24550 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24551 + /*
24552 + * Make sure that the multi_count field matches the
24553 + * actual transfer length.
24554 + */
24555 + hc->multi_count = num_packets;
24556 + }
24557 +
24558 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24559 + set_pid_isoc(hc);
24560 +
24561 + hctsiz.b.xfersize = hc->xfer_len;
24562 + }
24563 +
24564 + hc->start_pkt_count = num_packets;
24565 + hctsiz.b.pktcnt = num_packets;
24566 + hctsiz.b.pid = hc->data_pid_start;
24567 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24568 +
24569 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24570 + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
24571 + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
24572 + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
24573 +
24574 + if (core_if->dma_enable) {
24575 + dwc_dma_t dma_addr;
24576 + if (hc->align_buff) {
24577 + dma_addr = hc->align_buff;
24578 + } else {
24579 + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
24580 + }
24581 + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
24582 + }
24583 +
24584 + /* Start the split */
24585 + if (hc->do_split) {
24586 + hcsplt_data_t hcsplt;
24587 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
24588 + hcsplt.b.spltena = 1;
24589 + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
24590 + }
24591 +
24592 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24593 + hcchar.b.multicnt = hc->multi_count;
24594 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24595 +#ifdef DEBUG
24596 + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
24597 + if (hcchar.b.chdis) {
24598 + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
24599 + __func__, hc->hc_num, hcchar.d32);
24600 + }
24601 +#endif
24602 +
24603 + /* Set host channel enable after all other setup is complete. */
24604 + hcchar.b.chen = 1;
24605 + hcchar.b.chdis = 0;
24606 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24607 +
24608 + hc->xfer_started = 1;
24609 + hc->requests++;
24610 +
24611 + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
24612 + /* Load OUT packet into the appropriate Tx FIFO. */
24613 + dwc_otg_hc_write_packet(core_if, hc);
24614 + }
24615 +#ifdef DEBUG
24616 + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
24617 + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
24618 + hc->hc_num, core_if);//GRAYG
24619 + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
24620 + core_if->hc_xfer_info[hc->hc_num].hc = hc;
24621 +
24622 + /* Start a timer for this transfer. */
24623 + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
24624 + }
24625 +#endif
24626 +}
24627 +
24628 +/**
24629 + * This function does the setup for a data transfer for a host channel
24630 + * and starts the transfer in Descriptor DMA mode.
24631 + *
24632 + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
24633 + * Sets PID and NTD values. For periodic transfers
24634 + * initializes SCHED_INFO field with micro-frame bitmap.
24635 + *
24636 + * Initializes HCDMA register with descriptor list address and CTD value
24637 + * then starts the transfer via enabling the channel.
24638 + *
24639 + * @param core_if Programming view of DWC_otg controller.
24640 + * @param hc Information needed to initialize the host channel.
24641 + */
24642 +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24643 +{
24644 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24645 + hcchar_data_t hcchar;
24646 + hctsiz_data_t hctsiz;
24647 + hcdma_data_t hcdma;
24648 +
24649 + hctsiz.d32 = 0;
24650 +
24651 + if (hc->do_ping)
24652 + hctsiz.b_ddma.dopng = 1;
24653 +
24654 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24655 + set_pid_isoc(hc);
24656 +
24657 + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
24658 + hctsiz.b_ddma.pid = hc->data_pid_start;
24659 + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
24660 + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
24661 +
24662 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24663 + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
24664 + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
24665 +
24666 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24667 +
24668 + hcdma.d32 = 0;
24669 + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
24670 +
24671 + /* Always start from first descriptor. */
24672 + hcdma.b.ctd = 0;
24673 + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
24674 +
24675 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24676 + hcchar.b.multicnt = hc->multi_count;
24677 +
24678 +#ifdef DEBUG
24679 + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
24680 + if (hcchar.b.chdis) {
24681 + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
24682 + __func__, hc->hc_num, hcchar.d32);
24683 + }
24684 +#endif
24685 +
24686 + /* Set host channel enable after all other setup is complete. */
24687 + hcchar.b.chen = 1;
24688 + hcchar.b.chdis = 0;
24689 +
24690 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24691 +
24692 + hc->xfer_started = 1;
24693 + hc->requests++;
24694 +
24695 +#ifdef DEBUG
24696 + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
24697 + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
24698 + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
24699 + hc->hc_num, core_if);//GRAYG
24700 + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
24701 + core_if->hc_xfer_info[hc->hc_num].hc = hc;
24702 + /* Start a timer for this transfer. */
24703 + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
24704 + }
24705 +#endif
24706 +
24707 +}
24708 +
24709 +/**
24710 + * This function continues a data transfer that was started by previous call
24711 + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
24712 + * sufficient space in the request queue and Tx Data FIFO. This function
24713 + * should only be called in Slave mode. In DMA mode, the controller acts
24714 + * autonomously to complete transfers programmed to a host channel.
24715 + *
24716 + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
24717 + * if there is any data remaining to be queued. For an IN transfer, another
24718 + * data packet is always requested. For the SETUP phase of a control transfer,
24719 + * this function does nothing.
24720 + *
24721 + * @return 1 if a new request is queued, 0 if no more requests are required
24722 + * for this transfer.
24723 + */
24724 +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24725 +{
24726 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24727 +
24728 + if (hc->do_split) {
24729 + /* SPLITs always queue just once per channel */
24730 + return 0;
24731 + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
24732 + /* SETUPs are queued only once since they can't be NAKed. */
24733 + return 0;
24734 + } else if (hc->ep_is_in) {
24735 + /*
24736 + * Always queue another request for other IN transfers. If
24737 + * back-to-back INs are issued and NAKs are received for both,
24738 + * the driver may still be processing the first NAK when the
24739 + * second NAK is received. When the interrupt handler clears
24740 + * the NAK interrupt for the first NAK, the second NAK will
24741 + * not be seen. So we can't depend on the NAK interrupt
24742 + * handler to requeue a NAKed request. Instead, IN requests
24743 + * are issued each time this function is called. When the
24744 + * transfer completes, the extra requests for the channel will
24745 + * be flushed.
24746 + */
24747 + hcchar_data_t hcchar;
24748 + dwc_otg_hc_regs_t *hc_regs =
24749 + core_if->host_if->hc_regs[hc->hc_num];
24750 +
24751 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24752 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24753 + hcchar.b.chen = 1;
24754 + hcchar.b.chdis = 0;
24755 + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
24756 + hcchar.d32);
24757 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24758 + hc->requests++;
24759 + return 1;
24760 + } else {
24761 + /* OUT transfers. */
24762 + if (hc->xfer_count < hc->xfer_len) {
24763 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24764 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24765 + hcchar_data_t hcchar;
24766 + dwc_otg_hc_regs_t *hc_regs;
24767 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24768 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24769 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24770 + }
24771 +
24772 + /* Load OUT packet into the appropriate Tx FIFO. */
24773 + dwc_otg_hc_write_packet(core_if, hc);
24774 + hc->requests++;
24775 + return 1;
24776 + } else {
24777 + return 0;
24778 + }
24779 + }
24780 +}
24781 +
24782 +/**
24783 + * Starts a PING transfer. This function should only be called in Slave mode.
24784 + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
24785 + */
24786 +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24787 +{
24788 + hcchar_data_t hcchar;
24789 + hctsiz_data_t hctsiz;
24790 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24791 +
24792 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24793 +
24794 + hctsiz.d32 = 0;
24795 + hctsiz.b.dopng = 1;
24796 + hctsiz.b.pktcnt = 1;
24797 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24798 +
24799 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24800 + hcchar.b.chen = 1;
24801 + hcchar.b.chdis = 0;
24802 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24803 +}
24804 +
24805 +/*
24806 + * This function writes a packet into the Tx FIFO associated with the Host
24807 + * Channel. For a channel associated with a non-periodic EP, the non-periodic
24808 + * Tx FIFO is written. For a channel associated with a periodic EP, the
24809 + * periodic Tx FIFO is written. This function should only be called in Slave
24810 + * mode.
24811 + *
24812 + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
24813 + * then number of bytes written to the Tx FIFO.
24814 + */
24815 +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24816 +{
24817 + uint32_t i;
24818 + uint32_t remaining_count;
24819 + uint32_t byte_count;
24820 + uint32_t dword_count;
24821 +
24822 + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
24823 + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
24824 +
24825 + remaining_count = hc->xfer_len - hc->xfer_count;
24826 + if (remaining_count > hc->max_packet) {
24827 + byte_count = hc->max_packet;
24828 + } else {
24829 + byte_count = remaining_count;
24830 + }
24831 +
24832 + dword_count = (byte_count + 3) / 4;
24833 +
24834 + if ((((unsigned long)data_buff) & 0x3) == 0) {
24835 + /* xfer_buff is DWORD aligned. */
24836 + for (i = 0; i < dword_count; i++, data_buff++) {
24837 + DWC_WRITE_REG32(data_fifo, *data_buff);
24838 + }
24839 + } else {
24840 + /* xfer_buff is not DWORD aligned. */
24841 + for (i = 0; i < dword_count; i++, data_buff++) {
24842 + uint32_t data;
24843 + data =
24844 + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
24845 + 16 | data_buff[3] << 24);
24846 + DWC_WRITE_REG32(data_fifo, data);
24847 + }
24848 + }
24849 +
24850 + hc->xfer_count += byte_count;
24851 + hc->xfer_buff += byte_count;
24852 +}
24853 +
24854 +/**
24855 + * Gets the current USB frame number. This is the frame number from the last
24856 + * SOF packet.
24857 + */
24858 +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
24859 +{
24860 + dsts_data_t dsts;
24861 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
24862 +
24863 + /* read current frame/microframe number from DSTS register */
24864 + return dsts.b.soffn;
24865 +}
24866 +
24867 +/**
24868 + * Calculates and gets the frame Interval value of HFIR register according PHY
24869 + * type and speed.The application can modify a value of HFIR register only after
24870 + * the Port Enable bit of the Host Port Control and Status register
24871 + * (HPRT.PrtEnaPort) has been set.
24872 +*/
24873 +
24874 +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
24875 +{
24876 + gusbcfg_data_t usbcfg;
24877 + hwcfg2_data_t hwcfg2;
24878 + hprt0_data_t hprt0;
24879 + int clock = 60; // default value
24880 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
24881 + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
24882 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
24883 + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
24884 + clock = 60;
24885 + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
24886 + clock = 48;
24887 + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24888 + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
24889 + clock = 30;
24890 + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24891 + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
24892 + clock = 60;
24893 + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24894 + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
24895 + clock = 48;
24896 + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
24897 + clock = 48;
24898 + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
24899 + clock = 48;
24900 + if (hprt0.b.prtspd == 0)
24901 + /* High speed case */
24902 + return 125 * clock - 1;
24903 + else
24904 + /* FS/LS case */
24905 + return 1000 * clock - 1;
24906 +}
24907 +
24908 +/**
24909 + * This function reads a setup packet from the Rx FIFO into the destination
24910 + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
24911 + * Interrupt routine when a SETUP packet has been received in Slave mode.
24912 + *
24913 + * @param core_if Programming view of DWC_otg controller.
24914 + * @param dest Destination buffer for packet data.
24915 + */
24916 +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
24917 +{
24918 + device_grxsts_data_t status;
24919 + /* Get the 8 bytes of a setup transaction data */
24920 +
24921 + /* Pop 2 DWORDS off the receive data FIFO into memory */
24922 + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
24923 + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
24924 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
24925 + status.d32 =
24926 + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
24927 + DWC_DEBUGPL(DBG_ANY,
24928 + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
24929 + status.b.epnum, status.b.bcnt, status.b.pktsts,
24930 + status.b.fn, status.b.fn);
24931 + }
24932 +}
24933 +
24934 +/**
24935 + * This function enables EP0 OUT to receive SETUP packets and configures EP0
24936 + * IN for transmitting packets. It is normally called when the
24937 + * "Enumeration Done" interrupt occurs.
24938 + *
24939 + * @param core_if Programming view of DWC_otg controller.
24940 + * @param ep The EP0 data.
24941 + */
24942 +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24943 +{
24944 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
24945 + dsts_data_t dsts;
24946 + depctl_data_t diepctl;
24947 + depctl_data_t doepctl;
24948 + dctl_data_t dctl = {.d32 = 0 };
24949 +
24950 + ep->stp_rollover = 0;
24951 + /* Read the Device Status and Endpoint 0 Control registers */
24952 + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
24953 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
24954 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
24955 +
24956 + /* Set the MPS of the IN EP based on the enumeration speed */
24957 + switch (dsts.b.enumspd) {
24958 + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
24959 + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
24960 + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
24961 + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
24962 + break;
24963 + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
24964 + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
24965 + break;
24966 + }
24967 +
24968 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
24969 +
24970 + /* Enable OUT EP for receive */
24971 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
24972 + doepctl.b.epena = 1;
24973 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
24974 + }
24975 +#ifdef VERBOSE
24976 + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
24977 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
24978 + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
24979 + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
24980 +#endif
24981 + dctl.b.cgnpinnak = 1;
24982 +
24983 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
24984 + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
24985 + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
24986 +
24987 +}
24988 +
24989 +/**
24990 + * This function activates an EP. The Device EP control register for
24991 + * the EP is configured as defined in the ep structure. Note: This
24992 + * function is not used for EP0.
24993 + *
24994 + * @param core_if Programming view of DWC_otg controller.
24995 + * @param ep The EP to activate.
24996 + */
24997 +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24998 +{
24999 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
25000 + depctl_data_t depctl;
25001 + volatile uint32_t *addr;
25002 + daint_data_t daintmsk = {.d32 = 0 };
25003 + dcfg_data_t dcfg;
25004 + uint8_t i;
25005 +
25006 + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
25007 + (ep->is_in ? "IN" : "OUT"));
25008 +
25009 +#ifdef DWC_UTE_PER_IO
25010 + ep->xiso_frame_num = 0xFFFFFFFF;
25011 + ep->xiso_active_xfers = 0;
25012 + ep->xiso_queued_xfers = 0;
25013 +#endif
25014 + /* Read DEPCTLn register */
25015 + if (ep->is_in == 1) {
25016 + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
25017 + daintmsk.ep.in = 1 << ep->num;
25018 + } else {
25019 + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
25020 + daintmsk.ep.out = 1 << ep->num;
25021 + }
25022 +
25023 + /* If the EP is already active don't change the EP Control
25024 + * register. */
25025 + depctl.d32 = DWC_READ_REG32(addr);
25026 + if (!depctl.b.usbactep) {
25027 + depctl.b.mps = ep->maxpacket;
25028 + depctl.b.eptype = ep->type;
25029 + depctl.b.txfnum = ep->tx_fifo_num;
25030 +
25031 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25032 + depctl.b.setd0pid = 1; // ???
25033 + } else {
25034 + depctl.b.setd0pid = 1;
25035 + }
25036 + depctl.b.usbactep = 1;
25037 +
25038 + /* Update nextep_seq array and EPMSCNT in DCFG*/
25039 + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
25040 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
25041 + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
25042 + break;
25043 + }
25044 + core_if->nextep_seq[i] = ep->num;
25045 + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
25046 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25047 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
25048 + dcfg.b.epmscnt++;
25049 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
25050 +
25051 + DWC_DEBUGPL(DBG_PCDV,
25052 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
25053 + __func__, core_if->first_in_nextep_seq);
25054 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
25055 + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
25056 + core_if->nextep_seq[i]);
25057 + }
25058 +
25059 + }
25060 +
25061 +
25062 + DWC_WRITE_REG32(addr, depctl.d32);
25063 + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
25064 + }
25065 +
25066 + /* Enable the Interrupt for this EP */
25067 + if (core_if->multiproc_int_enable) {
25068 + if (ep->is_in == 1) {
25069 + diepmsk_data_t diepmsk = {.d32 = 0 };
25070 + diepmsk.b.xfercompl = 1;
25071 + diepmsk.b.timeout = 1;
25072 + diepmsk.b.epdisabled = 1;
25073 + diepmsk.b.ahberr = 1;
25074 + diepmsk.b.intknepmis = 1;
25075 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
25076 + diepmsk.b.intknepmis = 0;
25077 + diepmsk.b.txfifoundrn = 1; //?????
25078 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25079 + diepmsk.b.nak = 1;
25080 + }
25081 +
25082 +
25083 +
25084 +/*
25085 + if (core_if->dma_desc_enable) {
25086 + diepmsk.b.bna = 1;
25087 + }
25088 +*/
25089 +/*
25090 + if (core_if->dma_enable) {
25091 + doepmsk.b.nak = 1;
25092 + }
25093 +*/
25094 + DWC_WRITE_REG32(&dev_if->dev_global_regs->
25095 + diepeachintmsk[ep->num], diepmsk.d32);
25096 +
25097 + } else {
25098 + doepmsk_data_t doepmsk = {.d32 = 0 };
25099 + doepmsk.b.xfercompl = 1;
25100 + doepmsk.b.ahberr = 1;
25101 + doepmsk.b.epdisabled = 1;
25102 + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
25103 + doepmsk.b.outtknepdis = 1;
25104 +
25105 +/*
25106 +
25107 + if (core_if->dma_desc_enable) {
25108 + doepmsk.b.bna = 1;
25109 + }
25110 +*/
25111 +/*
25112 + doepmsk.b.babble = 1;
25113 + doepmsk.b.nyet = 1;
25114 + doepmsk.b.nak = 1;
25115 +*/
25116 + DWC_WRITE_REG32(&dev_if->dev_global_regs->
25117 + doepeachintmsk[ep->num], doepmsk.d32);
25118 + }
25119 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
25120 + 0, daintmsk.d32);
25121 + } else {
25122 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25123 + if (ep->is_in) {
25124 + diepmsk_data_t diepmsk = {.d32 = 0 };
25125 + diepmsk.b.nak = 1;
25126 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
25127 + } else {
25128 + doepmsk_data_t doepmsk = {.d32 = 0 };
25129 + doepmsk.b.outtknepdis = 1;
25130 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
25131 + }
25132 + }
25133 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
25134 + 0, daintmsk.d32);
25135 + }
25136 +
25137 + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
25138 + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
25139 +
25140 + ep->stall_clear_flag = 0;
25141 +
25142 + return;
25143 +}
25144 +
25145 +/**
25146 + * This function deactivates an EP. This is done by clearing the USB Active
25147 + * EP bit in the Device EP control register. Note: This function is not used
25148 + * for EP0. EP0 cannot be deactivated.
25149 + *
25150 + * @param core_if Programming view of DWC_otg controller.
25151 + * @param ep The EP to deactivate.
25152 + */
25153 +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25154 +{
25155 + depctl_data_t depctl = {.d32 = 0 };
25156 + volatile uint32_t *addr;
25157 + daint_data_t daintmsk = {.d32 = 0 };
25158 + dcfg_data_t dcfg;
25159 + uint8_t i = 0;
25160 +
25161 +#ifdef DWC_UTE_PER_IO
25162 + ep->xiso_frame_num = 0xFFFFFFFF;
25163 + ep->xiso_active_xfers = 0;
25164 + ep->xiso_queued_xfers = 0;
25165 +#endif
25166 +
25167 + /* Read DEPCTLn register */
25168 + if (ep->is_in == 1) {
25169 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
25170 + daintmsk.ep.in = 1 << ep->num;
25171 + } else {
25172 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
25173 + daintmsk.ep.out = 1 << ep->num;
25174 + }
25175 +
25176 + depctl.d32 = DWC_READ_REG32(addr);
25177 +
25178 + depctl.b.usbactep = 0;
25179 +
25180 + /* Update nextep_seq array and EPMSCNT in DCFG*/
25181 + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
25182 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
25183 + if (core_if->nextep_seq[i] == ep->num)
25184 + break;
25185 + }
25186 + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
25187 + if (core_if->first_in_nextep_seq == ep->num)
25188 + core_if->first_in_nextep_seq = i;
25189 + core_if->nextep_seq[ep->num] = 0xff;
25190 + depctl.b.nextep = 0;
25191 + dcfg.d32 =
25192 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
25193 + dcfg.b.epmscnt--;
25194 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
25195 + dcfg.d32);
25196 +
25197 + DWC_DEBUGPL(DBG_PCDV,
25198 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
25199 + __func__, core_if->first_in_nextep_seq);
25200 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
25201 + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
25202 + }
25203 + }
25204 +
25205 + if (ep->is_in == 1)
25206 + depctl.b.txfnum = 0;
25207 +
25208 + if (core_if->dma_desc_enable)
25209 + depctl.b.epdis = 1;
25210 +
25211 + DWC_WRITE_REG32(addr, depctl.d32);
25212 + depctl.d32 = DWC_READ_REG32(addr);
25213 + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
25214 + && depctl.b.epena) {
25215 + depctl_data_t depctl = {.d32 = 0};
25216 + if (ep->is_in) {
25217 + diepint_data_t diepint = {.d32 = 0};
25218 +
25219 + depctl.b.snak = 1;
25220 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25221 + diepctl, depctl.d32);
25222 + do {
25223 + dwc_udelay(10);
25224 + diepint.d32 =
25225 + DWC_READ_REG32(&core_if->
25226 + dev_if->in_ep_regs[ep->num]->
25227 + diepint);
25228 + } while (!diepint.b.inepnakeff);
25229 + diepint.b.inepnakeff = 1;
25230 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25231 + diepint, diepint.d32);
25232 + depctl.d32 = 0;
25233 + depctl.b.epdis = 1;
25234 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25235 + diepctl, depctl.d32);
25236 + do {
25237 + dwc_udelay(10);
25238 + diepint.d32 =
25239 + DWC_READ_REG32(&core_if->
25240 + dev_if->in_ep_regs[ep->num]->
25241 + diepint);
25242 + } while (!diepint.b.epdisabled);
25243 + diepint.b.epdisabled = 1;
25244 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25245 + diepint, diepint.d32);
25246 + } else {
25247 + dctl_data_t dctl = {.d32 = 0};
25248 + gintmsk_data_t gintsts = {.d32 = 0};
25249 + doepint_data_t doepint = {.d32 = 0};
25250 + dctl.b.sgoutnak = 1;
25251 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
25252 + dctl, 0, dctl.d32);
25253 + do {
25254 + dwc_udelay(10);
25255 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
25256 + } while (!gintsts.b.goutnakeff);
25257 + gintsts.d32 = 0;
25258 + gintsts.b.goutnakeff = 1;
25259 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
25260 +
25261 + depctl.d32 = 0;
25262 + depctl.b.epdis = 1;
25263 + depctl.b.snak = 1;
25264 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
25265 + do
25266 + {
25267 + dwc_udelay(10);
25268 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
25269 + out_ep_regs[ep->num]->doepint);
25270 + } while (!doepint.b.epdisabled);
25271 +
25272 + doepint.b.epdisabled = 1;
25273 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
25274 +
25275 + dctl.d32 = 0;
25276 + dctl.b.cgoutnak = 1;
25277 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
25278 + }
25279 + }
25280 +
25281 + /* Disable the Interrupt for this EP */
25282 + if (core_if->multiproc_int_enable) {
25283 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
25284 + daintmsk.d32, 0);
25285 +
25286 + if (ep->is_in == 1) {
25287 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
25288 + diepeachintmsk[ep->num], 0);
25289 + } else {
25290 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
25291 + doepeachintmsk[ep->num], 0);
25292 + }
25293 + } else {
25294 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
25295 + daintmsk.d32, 0);
25296 + }
25297 +
25298 +}
25299 +
25300 +/**
25301 + * This function initializes dma descriptor chain.
25302 + *
25303 + * @param core_if Programming view of DWC_otg controller.
25304 + * @param ep The EP to start the transfer on.
25305 + */
25306 +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25307 +{
25308 + dwc_otg_dev_dma_desc_t *dma_desc;
25309 + uint32_t offset;
25310 + uint32_t xfer_est;
25311 + int i;
25312 + unsigned maxxfer_local, total_len;
25313 +
25314 + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
25315 + (ep->maxpacket%4)) {
25316 + maxxfer_local = ep->maxpacket;
25317 + total_len = ep->xfer_len;
25318 + } else {
25319 + maxxfer_local = ep->maxxfer;
25320 + total_len = ep->total_len;
25321 + }
25322 +
25323 + ep->desc_cnt = (total_len / maxxfer_local) +
25324 + ((total_len % maxxfer_local) ? 1 : 0);
25325 +
25326 + if (!ep->desc_cnt)
25327 + ep->desc_cnt = 1;
25328 +
25329 + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
25330 + ep->desc_cnt = MAX_DMA_DESC_CNT;
25331 +
25332 + dma_desc = ep->desc_addr;
25333 + if (maxxfer_local == ep->maxpacket) {
25334 + if ((total_len % maxxfer_local) &&
25335 + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
25336 + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
25337 + (total_len % maxxfer_local);
25338 + } else
25339 + xfer_est = ep->desc_cnt * maxxfer_local;
25340 + } else
25341 + xfer_est = total_len;
25342 + offset = 0;
25343 + for (i = 0; i < ep->desc_cnt; ++i) {
25344 + /** DMA Descriptor Setup */
25345 + if (xfer_est > maxxfer_local) {
25346 + dma_desc->status.b.bs = BS_HOST_BUSY;
25347 + dma_desc->status.b.l = 0;
25348 + dma_desc->status.b.ioc = 0;
25349 + dma_desc->status.b.sp = 0;
25350 + dma_desc->status.b.bytes = maxxfer_local;
25351 + dma_desc->buf = ep->dma_addr + offset;
25352 + dma_desc->status.b.sts = 0;
25353 + dma_desc->status.b.bs = BS_HOST_READY;
25354 +
25355 + xfer_est -= maxxfer_local;
25356 + offset += maxxfer_local;
25357 + } else {
25358 + dma_desc->status.b.bs = BS_HOST_BUSY;
25359 + dma_desc->status.b.l = 1;
25360 + dma_desc->status.b.ioc = 1;
25361 + if (ep->is_in) {
25362 + dma_desc->status.b.sp =
25363 + (xfer_est %
25364 + ep->maxpacket) ? 1 : ((ep->
25365 + sent_zlp) ? 1 : 0);
25366 + dma_desc->status.b.bytes = xfer_est;
25367 + } else {
25368 + if (maxxfer_local == ep->maxpacket)
25369 + dma_desc->status.b.bytes = xfer_est;
25370 + else
25371 + dma_desc->status.b.bytes =
25372 + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
25373 + }
25374 +
25375 + dma_desc->buf = ep->dma_addr + offset;
25376 + dma_desc->status.b.sts = 0;
25377 + dma_desc->status.b.bs = BS_HOST_READY;
25378 + }
25379 + dma_desc++;
25380 + }
25381 +}
25382 +/**
25383 + * This function is called when to write ISOC data into appropriate dedicated
25384 + * periodic FIFO.
25385 + */
25386 +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
25387 +{
25388 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
25389 + dwc_otg_dev_in_ep_regs_t *ep_regs;
25390 + dtxfsts_data_t txstatus = {.d32 = 0 };
25391 + uint32_t len = 0;
25392 + int epnum = dwc_ep->num;
25393 + int dwords;
25394 +
25395 + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
25396 +
25397 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
25398 +
25399 + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
25400 +
25401 + if (len > dwc_ep->maxpacket) {
25402 + len = dwc_ep->maxpacket;
25403 + }
25404 +
25405 + dwords = (len + 3) / 4;
25406 +
25407 + /* While there is space in the queue and space in the FIFO and
25408 + * More data to tranfer, Write packets to the Tx FIFO */
25409 + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
25410 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
25411 +
25412 + while (txstatus.b.txfspcavail > dwords &&
25413 + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
25414 + /* Write the FIFO */
25415 + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
25416 +
25417 + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
25418 + if (len > dwc_ep->maxpacket) {
25419 + len = dwc_ep->maxpacket;
25420 + }
25421 +
25422 + dwords = (len + 3) / 4;
25423 + txstatus.d32 =
25424 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
25425 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
25426 + txstatus.d32);
25427 + }
25428 +
25429 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
25430 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
25431 +
25432 + return 1;
25433 +}
25434 +/**
25435 + * This function does the setup for a data transfer for an EP and
25436 + * starts the transfer. For an IN transfer, the packets will be
25437 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
25438 + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
25439 + *
25440 + * @param core_if Programming view of DWC_otg controller.
25441 + * @param ep The EP to start the transfer on.
25442 + */
25443 +
25444 +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25445 +{
25446 + depctl_data_t depctl;
25447 + deptsiz_data_t deptsiz;
25448 + gintmsk_data_t intr_mask = {.d32 = 0 };
25449 +
25450 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
25451 + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
25452 + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
25453 + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
25454 + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
25455 + ep->total_len);
25456 + /* IN endpoint */
25457 + if (ep->is_in == 1) {
25458 + dwc_otg_dev_in_ep_regs_t *in_regs =
25459 + core_if->dev_if->in_ep_regs[ep->num];
25460 +
25461 + gnptxsts_data_t gtxstatus;
25462 +
25463 + gtxstatus.d32 =
25464 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25465 +
25466 + if (core_if->en_multiple_tx_fifo == 0
25467 + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
25468 +#ifdef DEBUG
25469 + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
25470 +#endif
25471 + return;
25472 + }
25473 +
25474 + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
25475 + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
25476 +
25477 + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
25478 + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
25479 + ep->maxxfer : (ep->total_len - ep->xfer_len);
25480 + else
25481 + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
25482 + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
25483 +
25484 +
25485 + /* Zero Length Packet? */
25486 + if ((ep->xfer_len - ep->xfer_count) == 0) {
25487 + deptsiz.b.xfersize = 0;
25488 + deptsiz.b.pktcnt = 1;
25489 + } else {
25490 + /* Program the transfer size and packet count
25491 + * as follows: xfersize = N * maxpacket +
25492 + * short_packet pktcnt = N + (short_packet
25493 + * exist ? 1 : 0)
25494 + */
25495 + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
25496 + deptsiz.b.pktcnt =
25497 + (ep->xfer_len - ep->xfer_count - 1 +
25498 + ep->maxpacket) / ep->maxpacket;
25499 + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
25500 + deptsiz.b.pktcnt = MAX_PKT_CNT;
25501 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
25502 + }
25503 + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
25504 + deptsiz.b.mc = deptsiz.b.pktcnt;
25505 + }
25506 +
25507 + /* Write the DMA register */
25508 + if (core_if->dma_enable) {
25509 + if (core_if->dma_desc_enable == 0) {
25510 + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
25511 + deptsiz.b.mc = 1;
25512 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25513 + deptsiz.d32);
25514 + DWC_WRITE_REG32(&(in_regs->diepdma),
25515 + (uint32_t) ep->dma_addr);
25516 + } else {
25517 +#ifdef DWC_UTE_CFI
25518 + /* The descriptor chain should be already initialized by now */
25519 + if (ep->buff_mode != BM_STANDARD) {
25520 + DWC_WRITE_REG32(&in_regs->diepdma,
25521 + ep->descs_dma_addr);
25522 + } else {
25523 +#endif
25524 + init_dma_desc_chain(core_if, ep);
25525 + /** DIEPDMAn Register write */
25526 + DWC_WRITE_REG32(&in_regs->diepdma,
25527 + ep->dma_desc_addr);
25528 +#ifdef DWC_UTE_CFI
25529 + }
25530 +#endif
25531 + }
25532 + } else {
25533 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25534 + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
25535 + /**
25536 + * Enable the Non-Periodic Tx FIFO empty interrupt,
25537 + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
25538 + * the data will be written into the fifo by the ISR.
25539 + */
25540 + if (core_if->en_multiple_tx_fifo == 0) {
25541 + intr_mask.b.nptxfempty = 1;
25542 + DWC_MODIFY_REG32
25543 + (&core_if->core_global_regs->gintmsk,
25544 + intr_mask.d32, intr_mask.d32);
25545 + } else {
25546 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25547 + if (ep->xfer_len > 0) {
25548 + uint32_t fifoemptymsk = 0;
25549 + fifoemptymsk = 1 << ep->num;
25550 + DWC_MODIFY_REG32
25551 + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25552 + 0, fifoemptymsk);
25553 +
25554 + }
25555 + }
25556 + } else {
25557 + write_isoc_tx_fifo(core_if, ep);
25558 + }
25559 + }
25560 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25561 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25562 +
25563 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25564 + dsts_data_t dsts = {.d32 = 0};
25565 + if (ep->bInterval == 1) {
25566 + dsts.d32 =
25567 + DWC_READ_REG32(&core_if->dev_if->
25568 + dev_global_regs->dsts);
25569 + ep->frame_num = dsts.b.soffn + ep->bInterval;
25570 + if (ep->frame_num > 0x3FFF) {
25571 + ep->frm_overrun = 1;
25572 + ep->frame_num &= 0x3FFF;
25573 + } else
25574 + ep->frm_overrun = 0;
25575 + if (ep->frame_num & 0x1) {
25576 + depctl.b.setd1pid = 1;
25577 + } else {
25578 + depctl.b.setd0pid = 1;
25579 + }
25580 + }
25581 + }
25582 + /* EP enable, IN data in FIFO */
25583 + depctl.b.cnak = 1;
25584 + depctl.b.epena = 1;
25585 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25586 +
25587 + } else {
25588 + /* OUT endpoint */
25589 + dwc_otg_dev_out_ep_regs_t *out_regs =
25590 + core_if->dev_if->out_ep_regs[ep->num];
25591 +
25592 + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
25593 + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
25594 +
25595 + if (!core_if->dma_desc_enable) {
25596 + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
25597 + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
25598 + ep->maxxfer : (ep->total_len - ep->xfer_len);
25599 + else
25600 + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
25601 + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
25602 + }
25603 +
25604 + /* Program the transfer size and packet count as follows:
25605 + *
25606 + * pktcnt = N
25607 + * xfersize = N * maxpacket
25608 + */
25609 + if ((ep->xfer_len - ep->xfer_count) == 0) {
25610 + /* Zero Length Packet */
25611 + deptsiz.b.xfersize = ep->maxpacket;
25612 + deptsiz.b.pktcnt = 1;
25613 + } else {
25614 + deptsiz.b.pktcnt =
25615 + (ep->xfer_len - ep->xfer_count +
25616 + (ep->maxpacket - 1)) / ep->maxpacket;
25617 + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
25618 + deptsiz.b.pktcnt = MAX_PKT_CNT;
25619 + }
25620 + if (!core_if->dma_desc_enable) {
25621 + ep->xfer_len =
25622 + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
25623 + }
25624 + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
25625 + }
25626 +
25627 + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
25628 + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
25629 +
25630 + if (core_if->dma_enable) {
25631 + if (!core_if->dma_desc_enable) {
25632 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25633 + deptsiz.d32);
25634 +
25635 + DWC_WRITE_REG32(&(out_regs->doepdma),
25636 + (uint32_t) ep->dma_addr);
25637 + } else {
25638 +#ifdef DWC_UTE_CFI
25639 + /* The descriptor chain should be already initialized by now */
25640 + if (ep->buff_mode != BM_STANDARD) {
25641 + DWC_WRITE_REG32(&out_regs->doepdma,
25642 + ep->descs_dma_addr);
25643 + } else {
25644 +#endif
25645 + /** This is used for interrupt out transfers*/
25646 + if (!ep->xfer_len)
25647 + ep->xfer_len = ep->total_len;
25648 + init_dma_desc_chain(core_if, ep);
25649 +
25650 + if (core_if->core_params->dev_out_nak) {
25651 + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
25652 + deptsiz.b.pktcnt = (ep->total_len +
25653 + (ep->maxpacket - 1)) / ep->maxpacket;
25654 + deptsiz.b.xfersize = ep->total_len;
25655 + /* Remember initial value of doeptsiz */
25656 + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
25657 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25658 + deptsiz.d32);
25659 + }
25660 + }
25661 + /** DOEPDMAn Register write */
25662 + DWC_WRITE_REG32(&out_regs->doepdma,
25663 + ep->dma_desc_addr);
25664 +#ifdef DWC_UTE_CFI
25665 + }
25666 +#endif
25667 + }
25668 + } else {
25669 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25670 + }
25671 +
25672 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25673 + dsts_data_t dsts = {.d32 = 0};
25674 + if (ep->bInterval == 1) {
25675 + dsts.d32 =
25676 + DWC_READ_REG32(&core_if->dev_if->
25677 + dev_global_regs->dsts);
25678 + ep->frame_num = dsts.b.soffn + ep->bInterval;
25679 + if (ep->frame_num > 0x3FFF) {
25680 + ep->frm_overrun = 1;
25681 + ep->frame_num &= 0x3FFF;
25682 + } else
25683 + ep->frm_overrun = 0;
25684 +
25685 + if (ep->frame_num & 0x1) {
25686 + depctl.b.setd1pid = 1;
25687 + } else {
25688 + depctl.b.setd0pid = 1;
25689 + }
25690 + }
25691 + }
25692 +
25693 + /* EP enable */
25694 + depctl.b.cnak = 1;
25695 + depctl.b.epena = 1;
25696 +
25697 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25698 +
25699 + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
25700 + DWC_READ_REG32(&out_regs->doepctl),
25701 + DWC_READ_REG32(&out_regs->doeptsiz));
25702 + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
25703 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
25704 + daintmsk),
25705 + DWC_READ_REG32(&core_if->core_global_regs->
25706 + gintmsk));
25707 +
25708 + /* Timer is scheduling only for out bulk transfers for
25709 + * "Device DDMA OUT NAK Enhancement" feature to inform user
25710 + * about received data payload in case of timeout
25711 + */
25712 + if (core_if->core_params->dev_out_nak) {
25713 + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
25714 + core_if->ep_xfer_info[ep->num].core_if = core_if;
25715 + core_if->ep_xfer_info[ep->num].ep = ep;
25716 + core_if->ep_xfer_info[ep->num].state = 1;
25717 +
25718 + /* Start a timer for this transfer. */
25719 + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
25720 + }
25721 + }
25722 + }
25723 +}
25724 +
25725 +/**
25726 + * This function setup a zero length transfer in Buffer DMA and
25727 + * Slave modes for usb requests with zero field set
25728 + *
25729 + * @param core_if Programming view of DWC_otg controller.
25730 + * @param ep The EP to start the transfer on.
25731 + *
25732 + */
25733 +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25734 +{
25735 +
25736 + depctl_data_t depctl;
25737 + deptsiz_data_t deptsiz;
25738 + gintmsk_data_t intr_mask = {.d32 = 0 };
25739 +
25740 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
25741 + DWC_PRINTF("zero length transfer is called\n");
25742 +
25743 + /* IN endpoint */
25744 + if (ep->is_in == 1) {
25745 + dwc_otg_dev_in_ep_regs_t *in_regs =
25746 + core_if->dev_if->in_ep_regs[ep->num];
25747 +
25748 + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
25749 + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
25750 +
25751 + deptsiz.b.xfersize = 0;
25752 + deptsiz.b.pktcnt = 1;
25753 +
25754 + /* Write the DMA register */
25755 + if (core_if->dma_enable) {
25756 + if (core_if->dma_desc_enable == 0) {
25757 + deptsiz.b.mc = 1;
25758 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25759 + deptsiz.d32);
25760 + DWC_WRITE_REG32(&(in_regs->diepdma),
25761 + (uint32_t) ep->dma_addr);
25762 + }
25763 + } else {
25764 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25765 + /**
25766 + * Enable the Non-Periodic Tx FIFO empty interrupt,
25767 + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
25768 + * the data will be written into the fifo by the ISR.
25769 + */
25770 + if (core_if->en_multiple_tx_fifo == 0) {
25771 + intr_mask.b.nptxfempty = 1;
25772 + DWC_MODIFY_REG32(&core_if->
25773 + core_global_regs->gintmsk,
25774 + intr_mask.d32, intr_mask.d32);
25775 + } else {
25776 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25777 + if (ep->xfer_len > 0) {
25778 + uint32_t fifoemptymsk = 0;
25779 + fifoemptymsk = 1 << ep->num;
25780 + DWC_MODIFY_REG32(&core_if->
25781 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25782 + 0, fifoemptymsk);
25783 + }
25784 + }
25785 + }
25786 +
25787 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25788 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25789 + /* EP enable, IN data in FIFO */
25790 + depctl.b.cnak = 1;
25791 + depctl.b.epena = 1;
25792 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25793 +
25794 + } else {
25795 + /* OUT endpoint */
25796 + dwc_otg_dev_out_ep_regs_t *out_regs =
25797 + core_if->dev_if->out_ep_regs[ep->num];
25798 +
25799 + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
25800 + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
25801 +
25802 + /* Zero Length Packet */
25803 + deptsiz.b.xfersize = ep->maxpacket;
25804 + deptsiz.b.pktcnt = 1;
25805 +
25806 + if (core_if->dma_enable) {
25807 + if (!core_if->dma_desc_enable) {
25808 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25809 + deptsiz.d32);
25810 +
25811 + DWC_WRITE_REG32(&(out_regs->doepdma),
25812 + (uint32_t) ep->dma_addr);
25813 + }
25814 + } else {
25815 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25816 + }
25817 +
25818 + /* EP enable */
25819 + depctl.b.cnak = 1;
25820 + depctl.b.epena = 1;
25821 +
25822 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25823 +
25824 + }
25825 +}
25826 +
25827 +/**
25828 + * This function does the setup for a data transfer for EP0 and starts
25829 + * the transfer. For an IN transfer, the packets will be loaded into
25830 + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
25831 + * unloaded from the Rx FIFO in the ISR.
25832 + *
25833 + * @param core_if Programming view of DWC_otg controller.
25834 + * @param ep The EP0 data.
25835 + */
25836 +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25837 +{
25838 + depctl_data_t depctl;
25839 + deptsiz0_data_t deptsiz;
25840 + gintmsk_data_t intr_mask = {.d32 = 0 };
25841 + dwc_otg_dev_dma_desc_t *dma_desc;
25842 +
25843 + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
25844 + "xfer_buff=%p start_xfer_buff=%p \n",
25845 + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
25846 + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
25847 +
25848 + ep->total_len = ep->xfer_len;
25849 +
25850 + /* IN endpoint */
25851 + if (ep->is_in == 1) {
25852 + dwc_otg_dev_in_ep_regs_t *in_regs =
25853 + core_if->dev_if->in_ep_regs[0];
25854 +
25855 + gnptxsts_data_t gtxstatus;
25856 +
25857 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
25858 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25859 + if (depctl.b.epena)
25860 + return;
25861 + }
25862 +
25863 + gtxstatus.d32 =
25864 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25865 +
25866 + /* If dedicated FIFO every time flush fifo before enable ep*/
25867 + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
25868 + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
25869 +
25870 + if (core_if->en_multiple_tx_fifo == 0
25871 + && gtxstatus.b.nptxqspcavail == 0
25872 + && !core_if->dma_enable) {
25873 +#ifdef DEBUG
25874 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25875 + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
25876 + DWC_READ_REG32(&in_regs->diepctl));
25877 + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
25878 + deptsiz.d32,
25879 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
25880 + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
25881 + gtxstatus.d32);
25882 +#endif
25883 + return;
25884 + }
25885 +
25886 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25887 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25888 +
25889 + /* Zero Length Packet? */
25890 + if (ep->xfer_len == 0) {
25891 + deptsiz.b.xfersize = 0;
25892 + deptsiz.b.pktcnt = 1;
25893 + } else {
25894 + /* Program the transfer size and packet count
25895 + * as follows: xfersize = N * maxpacket +
25896 + * short_packet pktcnt = N + (short_packet
25897 + * exist ? 1 : 0)
25898 + */
25899 + if (ep->xfer_len > ep->maxpacket) {
25900 + ep->xfer_len = ep->maxpacket;
25901 + deptsiz.b.xfersize = ep->maxpacket;
25902 + } else {
25903 + deptsiz.b.xfersize = ep->xfer_len;
25904 + }
25905 + deptsiz.b.pktcnt = 1;
25906 +
25907 + }
25908 + DWC_DEBUGPL(DBG_PCDV,
25909 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
25910 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
25911 + deptsiz.d32);
25912 +
25913 + /* Write the DMA register */
25914 + if (core_if->dma_enable) {
25915 + if (core_if->dma_desc_enable == 0) {
25916 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25917 + deptsiz.d32);
25918 +
25919 + DWC_WRITE_REG32(&(in_regs->diepdma),
25920 + (uint32_t) ep->dma_addr);
25921 + } else {
25922 + dma_desc = core_if->dev_if->in_desc_addr;
25923 +
25924 + /** DMA Descriptor Setup */
25925 + dma_desc->status.b.bs = BS_HOST_BUSY;
25926 + dma_desc->status.b.l = 1;
25927 + dma_desc->status.b.ioc = 1;
25928 + dma_desc->status.b.sp =
25929 + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
25930 + dma_desc->status.b.bytes = ep->xfer_len;
25931 + dma_desc->buf = ep->dma_addr;
25932 + dma_desc->status.b.sts = 0;
25933 + dma_desc->status.b.bs = BS_HOST_READY;
25934 +
25935 + /** DIEPDMA0 Register write */
25936 + DWC_WRITE_REG32(&in_regs->diepdma,
25937 + core_if->
25938 + dev_if->dma_in_desc_addr);
25939 + }
25940 + } else {
25941 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25942 + }
25943 +
25944 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25945 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25946 + /* EP enable, IN data in FIFO */
25947 + depctl.b.cnak = 1;
25948 + depctl.b.epena = 1;
25949 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25950 +
25951 + /**
25952 + * Enable the Non-Periodic Tx FIFO empty interrupt, the
25953 + * data will be written into the fifo by the ISR.
25954 + */
25955 + if (!core_if->dma_enable) {
25956 + if (core_if->en_multiple_tx_fifo == 0) {
25957 + intr_mask.b.nptxfempty = 1;
25958 + DWC_MODIFY_REG32(&core_if->
25959 + core_global_regs->gintmsk,
25960 + intr_mask.d32, intr_mask.d32);
25961 + } else {
25962 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25963 + if (ep->xfer_len > 0) {
25964 + uint32_t fifoemptymsk = 0;
25965 + fifoemptymsk |= 1 << ep->num;
25966 + DWC_MODIFY_REG32(&core_if->
25967 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25968 + 0, fifoemptymsk);
25969 + }
25970 + }
25971 + }
25972 + } else {
25973 + /* OUT endpoint */
25974 + dwc_otg_dev_out_ep_regs_t *out_regs =
25975 + core_if->dev_if->out_ep_regs[0];
25976 +
25977 + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
25978 + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
25979 +
25980 + /* Program the transfer size and packet count as follows:
25981 + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
25982 + * pktcnt = N */
25983 + /* Zero Length Packet */
25984 + deptsiz.b.xfersize = ep->maxpacket;
25985 + deptsiz.b.pktcnt = 1;
25986 + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
25987 + deptsiz.b.supcnt = 3;
25988 +
25989 + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
25990 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
25991 +
25992 + if (core_if->dma_enable) {
25993 + if (!core_if->dma_desc_enable) {
25994 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25995 + deptsiz.d32);
25996 +
25997 + DWC_WRITE_REG32(&(out_regs->doepdma),
25998 + (uint32_t) ep->dma_addr);
25999 + } else {
26000 + dma_desc = core_if->dev_if->out_desc_addr;
26001 +
26002 + /** DMA Descriptor Setup */
26003 + dma_desc->status.b.bs = BS_HOST_BUSY;
26004 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
26005 + dma_desc->status.b.mtrf = 0;
26006 + dma_desc->status.b.sr = 0;
26007 + }
26008 + dma_desc->status.b.l = 1;
26009 + dma_desc->status.b.ioc = 1;
26010 + dma_desc->status.b.bytes = ep->maxpacket;
26011 + dma_desc->buf = ep->dma_addr;
26012 + dma_desc->status.b.sts = 0;
26013 + dma_desc->status.b.bs = BS_HOST_READY;
26014 +
26015 + /** DOEPDMA0 Register write */
26016 + DWC_WRITE_REG32(&out_regs->doepdma,
26017 + core_if->dev_if->
26018 + dma_out_desc_addr);
26019 + }
26020 + } else {
26021 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
26022 + }
26023 +
26024 + /* EP enable */
26025 + depctl.b.cnak = 1;
26026 + depctl.b.epena = 1;
26027 + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
26028 + }
26029 +}
26030 +
26031 +/**
26032 + * This function continues control IN transfers started by
26033 + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
26034 + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
26035 + * bit for the packet count.
26036 + *
26037 + * @param core_if Programming view of DWC_otg controller.
26038 + * @param ep The EP0 data.
26039 + */
26040 +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26041 +{
26042 + depctl_data_t depctl;
26043 + deptsiz0_data_t deptsiz;
26044 + gintmsk_data_t intr_mask = {.d32 = 0 };
26045 + dwc_otg_dev_dma_desc_t *dma_desc;
26046 +
26047 + if (ep->is_in == 1) {
26048 + dwc_otg_dev_in_ep_regs_t *in_regs =
26049 + core_if->dev_if->in_ep_regs[0];
26050 + gnptxsts_data_t tx_status = {.d32 = 0 };
26051 +
26052 + tx_status.d32 =
26053 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
26054 + /** @todo Should there be check for room in the Tx
26055 + * Status Queue. If not remove the code above this comment. */
26056 +
26057 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
26058 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
26059 +
26060 + /* Program the transfer size and packet count
26061 + * as follows: xfersize = N * maxpacket +
26062 + * short_packet pktcnt = N + (short_packet
26063 + * exist ? 1 : 0)
26064 + */
26065 +
26066 + if (core_if->dma_desc_enable == 0) {
26067 + deptsiz.b.xfersize =
26068 + (ep->total_len - ep->xfer_count) >
26069 + ep->maxpacket ? ep->maxpacket : (ep->total_len -
26070 + ep->xfer_count);
26071 + deptsiz.b.pktcnt = 1;
26072 + if (core_if->dma_enable == 0) {
26073 + ep->xfer_len += deptsiz.b.xfersize;
26074 + } else {
26075 + ep->xfer_len = deptsiz.b.xfersize;
26076 + }
26077 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
26078 + } else {
26079 + ep->xfer_len =
26080 + (ep->total_len - ep->xfer_count) >
26081 + ep->maxpacket ? ep->maxpacket : (ep->total_len -
26082 + ep->xfer_count);
26083 +
26084 + dma_desc = core_if->dev_if->in_desc_addr;
26085 +
26086 + /** DMA Descriptor Setup */
26087 + dma_desc->status.b.bs = BS_HOST_BUSY;
26088 + dma_desc->status.b.l = 1;
26089 + dma_desc->status.b.ioc = 1;
26090 + dma_desc->status.b.sp =
26091 + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
26092 + dma_desc->status.b.bytes = ep->xfer_len;
26093 + dma_desc->buf = ep->dma_addr;
26094 + dma_desc->status.b.sts = 0;
26095 + dma_desc->status.b.bs = BS_HOST_READY;
26096 +
26097 + /** DIEPDMA0 Register write */
26098 + DWC_WRITE_REG32(&in_regs->diepdma,
26099 + core_if->dev_if->dma_in_desc_addr);
26100 + }
26101 +
26102 + DWC_DEBUGPL(DBG_PCDV,
26103 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
26104 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
26105 + deptsiz.d32);
26106 +
26107 + /* Write the DMA register */
26108 + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
26109 + if (core_if->dma_desc_enable == 0)
26110 + DWC_WRITE_REG32(&(in_regs->diepdma),
26111 + (uint32_t) ep->dma_addr);
26112 + }
26113 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
26114 + depctl.b.nextep = core_if->nextep_seq[ep->num];
26115 + /* EP enable, IN data in FIFO */
26116 + depctl.b.cnak = 1;
26117 + depctl.b.epena = 1;
26118 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
26119 +
26120 + /**
26121 + * Enable the Non-Periodic Tx FIFO empty interrupt, the
26122 + * data will be written into the fifo by the ISR.
26123 + */
26124 + if (!core_if->dma_enable) {
26125 + if (core_if->en_multiple_tx_fifo == 0) {
26126 + /* First clear it from GINTSTS */
26127 + intr_mask.b.nptxfempty = 1;
26128 + DWC_MODIFY_REG32(&core_if->
26129 + core_global_regs->gintmsk,
26130 + intr_mask.d32, intr_mask.d32);
26131 +
26132 + } else {
26133 + /* Enable the Tx FIFO Empty Interrupt for this EP */
26134 + if (ep->xfer_len > 0) {
26135 + uint32_t fifoemptymsk = 0;
26136 + fifoemptymsk |= 1 << ep->num;
26137 + DWC_MODIFY_REG32(&core_if->
26138 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
26139 + 0, fifoemptymsk);
26140 + }
26141 + }
26142 + }
26143 + } else {
26144 + dwc_otg_dev_out_ep_regs_t *out_regs =
26145 + core_if->dev_if->out_ep_regs[0];
26146 +
26147 + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
26148 + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
26149 +
26150 + /* Program the transfer size and packet count
26151 + * as follows: xfersize = N * maxpacket +
26152 + * short_packet pktcnt = N + (short_packet
26153 + * exist ? 1 : 0)
26154 + */
26155 + deptsiz.b.xfersize = ep->maxpacket;
26156 + deptsiz.b.pktcnt = 1;
26157 +
26158 + if (core_if->dma_desc_enable == 0) {
26159 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
26160 + } else {
26161 + dma_desc = core_if->dev_if->out_desc_addr;
26162 +
26163 + /** DMA Descriptor Setup */
26164 + dma_desc->status.b.bs = BS_HOST_BUSY;
26165 + dma_desc->status.b.l = 1;
26166 + dma_desc->status.b.ioc = 1;
26167 + dma_desc->status.b.bytes = ep->maxpacket;
26168 + dma_desc->buf = ep->dma_addr;
26169 + dma_desc->status.b.sts = 0;
26170 + dma_desc->status.b.bs = BS_HOST_READY;
26171 +
26172 + /** DOEPDMA0 Register write */
26173 + DWC_WRITE_REG32(&out_regs->doepdma,
26174 + core_if->dev_if->dma_out_desc_addr);
26175 + }
26176 +
26177 + DWC_DEBUGPL(DBG_PCDV,
26178 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
26179 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
26180 + deptsiz.d32);
26181 +
26182 + /* Write the DMA register */
26183 + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
26184 + if (core_if->dma_desc_enable == 0)
26185 + DWC_WRITE_REG32(&(out_regs->doepdma),
26186 + (uint32_t) ep->dma_addr);
26187 +
26188 + }
26189 +
26190 + /* EP enable, IN data in FIFO */
26191 + depctl.b.cnak = 1;
26192 + depctl.b.epena = 1;
26193 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
26194 +
26195 + }
26196 +}
26197 +
26198 +#ifdef DEBUG
26199 +void dump_msg(const u8 * buf, unsigned int length)
26200 +{
26201 + unsigned int start, num, i;
26202 + char line[52], *p;
26203 +
26204 + if (length >= 512)
26205 + return;
26206 + start = 0;
26207 + while (length > 0) {
26208 + num = length < 16u ? length : 16u;
26209 + p = line;
26210 + for (i = 0; i < num; ++i) {
26211 + if (i == 8)
26212 + *p++ = ' ';
26213 + DWC_SPRINTF(p, " %02x", buf[i]);
26214 + p += 3;
26215 + }
26216 + *p = 0;
26217 + DWC_PRINTF("%6x: %s\n", start, line);
26218 + buf += num;
26219 + start += num;
26220 + length -= num;
26221 + }
26222 +}
26223 +#else
26224 +static inline void dump_msg(const u8 * buf, unsigned int length)
26225 +{
26226 +}
26227 +#endif
26228 +
26229 +/**
26230 + * This function writes a packet into the Tx FIFO associated with the
26231 + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
26232 + * periodic EPs the periodic Tx FIFO associated with the EP is written
26233 + * with all packets for the next micro-frame.
26234 + *
26235 + * @param core_if Programming view of DWC_otg controller.
26236 + * @param ep The EP to write packet for.
26237 + * @param dma Indicates if DMA is being used.
26238 + */
26239 +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
26240 + int dma)
26241 +{
26242 + /**
26243 + * The buffer is padded to DWORD on a per packet basis in
26244 + * slave/dma mode if the MPS is not DWORD aligned. The last
26245 + * packet, if short, is also padded to a multiple of DWORD.
26246 + *
26247 + * ep->xfer_buff always starts DWORD aligned in memory and is a
26248 + * multiple of DWORD in length
26249 + *
26250 + * ep->xfer_len can be any number of bytes
26251 + *
26252 + * ep->xfer_count is a multiple of ep->maxpacket until the last
26253 + * packet
26254 + *
26255 + * FIFO access is DWORD */
26256 +
26257 + uint32_t i;
26258 + uint32_t byte_count;
26259 + uint32_t dword_count;
26260 + uint32_t *fifo;
26261 + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
26262 +
26263 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
26264 + ep);
26265 + if (ep->xfer_count >= ep->xfer_len) {
26266 + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
26267 + return;
26268 + }
26269 +
26270 + /* Find the byte length of the packet either short packet or MPS */
26271 + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
26272 + byte_count = ep->xfer_len - ep->xfer_count;
26273 + } else {
26274 + byte_count = ep->maxpacket;
26275 + }
26276 +
26277 + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
26278 + * is not a multiple of DWORD */
26279 + dword_count = (byte_count + 3) / 4;
26280 +
26281 +#ifdef VERBOSE
26282 + dump_msg(ep->xfer_buff, byte_count);
26283 +#endif
26284 +
26285 + /**@todo NGS Where are the Periodic Tx FIFO addresses
26286 + * intialized? What should this be? */
26287 +
26288 + fifo = core_if->data_fifo[ep->num];
26289 +
26290 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
26291 + fifo, data_buff, *data_buff, byte_count);
26292 +
26293 + if (!dma) {
26294 + for (i = 0; i < dword_count; i++, data_buff++) {
26295 + DWC_WRITE_REG32(fifo, *data_buff);
26296 + }
26297 + }
26298 +
26299 + ep->xfer_count += byte_count;
26300 + ep->xfer_buff += byte_count;
26301 + ep->dma_addr += byte_count;
26302 +}
26303 +
26304 +/**
26305 + * Set the EP STALL.
26306 + *
26307 + * @param core_if Programming view of DWC_otg controller.
26308 + * @param ep The EP to set the stall on.
26309 + */
26310 +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26311 +{
26312 + depctl_data_t depctl;
26313 + volatile uint32_t *depctl_addr;
26314 +
26315 + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
26316 + (ep->is_in ? "IN" : "OUT"));
26317 +
26318 + if (ep->is_in == 1) {
26319 + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
26320 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26321 +
26322 + /* set the disable and stall bits */
26323 + if (depctl.b.epena) {
26324 + depctl.b.epdis = 1;
26325 + }
26326 + depctl.b.stall = 1;
26327 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26328 + } else {
26329 + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
26330 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26331 +
26332 + /* set the stall bit */
26333 + depctl.b.stall = 1;
26334 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26335 + }
26336 +
26337 + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
26338 +
26339 + return;
26340 +}
26341 +
26342 +/**
26343 + * Clear the EP STALL.
26344 + *
26345 + * @param core_if Programming view of DWC_otg controller.
26346 + * @param ep The EP to clear stall from.
26347 + */
26348 +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26349 +{
26350 + depctl_data_t depctl;
26351 + volatile uint32_t *depctl_addr;
26352 +
26353 + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
26354 + (ep->is_in ? "IN" : "OUT"));
26355 +
26356 + if (ep->is_in == 1) {
26357 + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
26358 + } else {
26359 + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
26360 + }
26361 +
26362 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26363 +
26364 + /* clear the stall bits */
26365 + depctl.b.stall = 0;
26366 +
26367 + /*
26368 + * USB Spec 9.4.5: For endpoints using data toggle, regardless
26369 + * of whether an endpoint has the Halt feature set, a
26370 + * ClearFeature(ENDPOINT_HALT) request always results in the
26371 + * data toggle being reinitialized to DATA0.
26372 + */
26373 + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
26374 + ep->type == DWC_OTG_EP_TYPE_BULK) {
26375 + depctl.b.setd0pid = 1; /* DATA0 */
26376 + }
26377 +
26378 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26379 + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
26380 + return;
26381 +}
26382 +
26383 +/**
26384 + * This function reads a packet from the Rx FIFO into the destination
26385 + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
26386 + *
26387 + * @param core_if Programming view of DWC_otg controller.
26388 + * @param dest Destination buffer for the packet.
26389 + * @param bytes Number of bytes to copy to the destination.
26390 + */
26391 +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
26392 + uint8_t * dest, uint16_t bytes)
26393 +{
26394 + int i;
26395 + int word_count = (bytes + 3) / 4;
26396 +
26397 + volatile uint32_t *fifo = core_if->data_fifo[0];
26398 + uint32_t *data_buff = (uint32_t *) dest;
26399 +
26400 + /**
26401 + * @todo Account for the case where _dest is not dword aligned. This
26402 + * requires reading data from the FIFO into a uint32_t temp buffer,
26403 + * then moving it into the data buffer.
26404 + */
26405 +
26406 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
26407 + core_if, dest, bytes);
26408 +
26409 + for (i = 0; i < word_count; i++, data_buff++) {
26410 + *data_buff = DWC_READ_REG32(fifo);
26411 + }
26412 +
26413 + return;
26414 +}
26415 +
26416 +/**
26417 + * This functions reads the device registers and prints them
26418 + *
26419 + * @param core_if Programming view of DWC_otg controller.
26420 + */
26421 +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
26422 +{
26423 + int i;
26424 + volatile uint32_t *addr;
26425 +
26426 + DWC_PRINTF("Device Global Registers\n");
26427 + addr = &core_if->dev_if->dev_global_regs->dcfg;
26428 + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
26429 + (unsigned long)addr, DWC_READ_REG32(addr));
26430 + addr = &core_if->dev_if->dev_global_regs->dctl;
26431 + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
26432 + (unsigned long)addr, DWC_READ_REG32(addr));
26433 + addr = &core_if->dev_if->dev_global_regs->dsts;
26434 + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
26435 + (unsigned long)addr, DWC_READ_REG32(addr));
26436 + addr = &core_if->dev_if->dev_global_regs->diepmsk;
26437 + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26438 + DWC_READ_REG32(addr));
26439 + addr = &core_if->dev_if->dev_global_regs->doepmsk;
26440 + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26441 + DWC_READ_REG32(addr));
26442 + addr = &core_if->dev_if->dev_global_regs->daint;
26443 + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26444 + DWC_READ_REG32(addr));
26445 + addr = &core_if->dev_if->dev_global_regs->daintmsk;
26446 + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26447 + DWC_READ_REG32(addr));
26448 + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
26449 + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26450 + DWC_READ_REG32(addr));
26451 + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
26452 + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
26453 + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
26454 + (unsigned long)addr, DWC_READ_REG32(addr));
26455 + }
26456 +
26457 + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
26458 + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
26459 + DWC_READ_REG32(addr));
26460 +
26461 + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
26462 + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
26463 + (unsigned long)addr, DWC_READ_REG32(addr));
26464 +
26465 + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
26466 + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
26467 + (unsigned long)addr, DWC_READ_REG32(addr));
26468 +
26469 + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
26470 + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
26471 + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
26472 + (unsigned long)addr, DWC_READ_REG32(addr));
26473 + }
26474 +
26475 + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
26476 + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26477 + DWC_READ_REG32(addr));
26478 +
26479 + if (core_if->hwcfg2.b.multi_proc_int) {
26480 +
26481 + addr = &core_if->dev_if->dev_global_regs->deachint;
26482 + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
26483 + (unsigned long)addr, DWC_READ_REG32(addr));
26484 + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
26485 + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
26486 + (unsigned long)addr, DWC_READ_REG32(addr));
26487 +
26488 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
26489 + addr =
26490 + &core_if->dev_if->
26491 + dev_global_regs->diepeachintmsk[i];
26492 + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
26493 + i, (unsigned long)addr,
26494 + DWC_READ_REG32(addr));
26495 + }
26496 +
26497 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
26498 + addr =
26499 + &core_if->dev_if->
26500 + dev_global_regs->doepeachintmsk[i];
26501 + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
26502 + i, (unsigned long)addr,
26503 + DWC_READ_REG32(addr));
26504 + }
26505 + }
26506 +
26507 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
26508 + DWC_PRINTF("Device IN EP %d Registers\n", i);
26509 + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
26510 + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
26511 + (unsigned long)addr, DWC_READ_REG32(addr));
26512 + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
26513 + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
26514 + (unsigned long)addr, DWC_READ_REG32(addr));
26515 + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
26516 + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
26517 + (unsigned long)addr, DWC_READ_REG32(addr));
26518 + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
26519 + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
26520 + (unsigned long)addr, DWC_READ_REG32(addr));
26521 + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
26522 + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
26523 + (unsigned long)addr, DWC_READ_REG32(addr));
26524 + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
26525 + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
26526 + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
26527 + }
26528 +
26529 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
26530 + DWC_PRINTF("Device OUT EP %d Registers\n", i);
26531 + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
26532 + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
26533 + (unsigned long)addr, DWC_READ_REG32(addr));
26534 + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
26535 + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
26536 + (unsigned long)addr, DWC_READ_REG32(addr));
26537 + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
26538 + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
26539 + (unsigned long)addr, DWC_READ_REG32(addr));
26540 + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
26541 + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
26542 + (unsigned long)addr, DWC_READ_REG32(addr));
26543 + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
26544 + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
26545 + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
26546 + (unsigned long)addr, DWC_READ_REG32(addr));
26547 + }
26548 +
26549 + }
26550 +}
26551 +
26552 +/**
26553 + * This functions reads the SPRAM and prints its content
26554 + *
26555 + * @param core_if Programming view of DWC_otg controller.
26556 + */
26557 +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
26558 +{
26559 + volatile uint8_t *addr, *start_addr, *end_addr;
26560 +
26561 + DWC_PRINTF("SPRAM Data:\n");
26562 + start_addr = (void *)core_if->core_global_regs;
26563 + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
26564 + start_addr += 0x00028000;
26565 + end_addr = (void *)core_if->core_global_regs;
26566 + end_addr += 0x000280e0;
26567 +
26568 + for (addr = start_addr; addr < end_addr; addr += 16) {
26569 + DWC_PRINTF
26570 + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
26571 + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
26572 + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
26573 + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
26574 + );
26575 + }
26576 +
26577 + return;
26578 +}
26579 +
26580 +/**
26581 + * This function reads the host registers and prints them
26582 + *
26583 + * @param core_if Programming view of DWC_otg controller.
26584 + */
26585 +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
26586 +{
26587 + int i;
26588 + volatile uint32_t *addr;
26589 +
26590 + DWC_PRINTF("Host Global Registers\n");
26591 + addr = &core_if->host_if->host_global_regs->hcfg;
26592 + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
26593 + (unsigned long)addr, DWC_READ_REG32(addr));
26594 + addr = &core_if->host_if->host_global_regs->hfir;
26595 + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
26596 + (unsigned long)addr, DWC_READ_REG32(addr));
26597 + addr = &core_if->host_if->host_global_regs->hfnum;
26598 + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
26599 + DWC_READ_REG32(addr));
26600 + addr = &core_if->host_if->host_global_regs->hptxsts;
26601 + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26602 + DWC_READ_REG32(addr));
26603 + addr = &core_if->host_if->host_global_regs->haint;
26604 + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26605 + DWC_READ_REG32(addr));
26606 + addr = &core_if->host_if->host_global_regs->haintmsk;
26607 + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26608 + DWC_READ_REG32(addr));
26609 + if (core_if->dma_desc_enable) {
26610 + addr = &core_if->host_if->host_global_regs->hflbaddr;
26611 + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
26612 + (unsigned long)addr, DWC_READ_REG32(addr));
26613 + }
26614 +
26615 + addr = core_if->host_if->hprt0;
26616 + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26617 + DWC_READ_REG32(addr));
26618 +
26619 + for (i = 0; i < core_if->core_params->host_channels; i++) {
26620 + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
26621 + addr = &core_if->host_if->hc_regs[i]->hcchar;
26622 + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
26623 + (unsigned long)addr, DWC_READ_REG32(addr));
26624 + addr = &core_if->host_if->hc_regs[i]->hcsplt;
26625 + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
26626 + (unsigned long)addr, DWC_READ_REG32(addr));
26627 + addr = &core_if->host_if->hc_regs[i]->hcint;
26628 + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
26629 + (unsigned long)addr, DWC_READ_REG32(addr));
26630 + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
26631 + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
26632 + (unsigned long)addr, DWC_READ_REG32(addr));
26633 + addr = &core_if->host_if->hc_regs[i]->hctsiz;
26634 + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
26635 + (unsigned long)addr, DWC_READ_REG32(addr));
26636 + addr = &core_if->host_if->hc_regs[i]->hcdma;
26637 + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
26638 + (unsigned long)addr, DWC_READ_REG32(addr));
26639 + if (core_if->dma_desc_enable) {
26640 + addr = &core_if->host_if->hc_regs[i]->hcdmab;
26641 + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
26642 + (unsigned long)addr, DWC_READ_REG32(addr));
26643 + }
26644 +
26645 + }
26646 + return;
26647 +}
26648 +
26649 +/**
26650 + * This function reads the core global registers and prints them
26651 + *
26652 + * @param core_if Programming view of DWC_otg controller.
26653 + */
26654 +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
26655 +{
26656 + int i, ep_num;
26657 + volatile uint32_t *addr;
26658 + char *txfsiz;
26659 +
26660 + DWC_PRINTF("Core Global Registers\n");
26661 + addr = &core_if->core_global_regs->gotgctl;
26662 + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26663 + DWC_READ_REG32(addr));
26664 + addr = &core_if->core_global_regs->gotgint;
26665 + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26666 + DWC_READ_REG32(addr));
26667 + addr = &core_if->core_global_regs->gahbcfg;
26668 + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26669 + DWC_READ_REG32(addr));
26670 + addr = &core_if->core_global_regs->gusbcfg;
26671 + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26672 + DWC_READ_REG32(addr));
26673 + addr = &core_if->core_global_regs->grstctl;
26674 + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26675 + DWC_READ_REG32(addr));
26676 + addr = &core_if->core_global_regs->gintsts;
26677 + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26678 + DWC_READ_REG32(addr));
26679 + addr = &core_if->core_global_regs->gintmsk;
26680 + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26681 + DWC_READ_REG32(addr));
26682 + addr = &core_if->core_global_regs->grxstsr;
26683 + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
26684 + DWC_READ_REG32(addr));
26685 + addr = &core_if->core_global_regs->grxfsiz;
26686 + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26687 + DWC_READ_REG32(addr));
26688 + addr = &core_if->core_global_regs->gnptxfsiz;
26689 + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26690 + DWC_READ_REG32(addr));
26691 + addr = &core_if->core_global_regs->gnptxsts;
26692 + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26693 + DWC_READ_REG32(addr));
26694 + addr = &core_if->core_global_regs->gi2cctl;
26695 + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26696 + DWC_READ_REG32(addr));
26697 + addr = &core_if->core_global_regs->gpvndctl;
26698 + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26699 + DWC_READ_REG32(addr));
26700 + addr = &core_if->core_global_regs->ggpio;
26701 + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
26702 + DWC_READ_REG32(addr));
26703 + addr = &core_if->core_global_regs->guid;
26704 + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
26705 + (unsigned long)addr, DWC_READ_REG32(addr));
26706 + addr = &core_if->core_global_regs->gsnpsid;
26707 + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
26708 + DWC_READ_REG32(addr));
26709 + addr = &core_if->core_global_regs->ghwcfg1;
26710 + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26711 + DWC_READ_REG32(addr));
26712 + addr = &core_if->core_global_regs->ghwcfg2;
26713 + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26714 + DWC_READ_REG32(addr));
26715 + addr = &core_if->core_global_regs->ghwcfg3;
26716 + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26717 + DWC_READ_REG32(addr));
26718 + addr = &core_if->core_global_regs->ghwcfg4;
26719 + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26720 + DWC_READ_REG32(addr));
26721 + addr = &core_if->core_global_regs->glpmcfg;
26722 + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26723 + DWC_READ_REG32(addr));
26724 + addr = &core_if->core_global_regs->gpwrdn;
26725 + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
26726 + DWC_READ_REG32(addr));
26727 + addr = &core_if->core_global_regs->gdfifocfg;
26728 + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26729 + DWC_READ_REG32(addr));
26730 + addr = &core_if->core_global_regs->adpctl;
26731 + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26732 + dwc_otg_adp_read_reg(core_if));
26733 + addr = &core_if->core_global_regs->hptxfsiz;
26734 + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26735 + DWC_READ_REG32(addr));
26736 +
26737 + if (core_if->en_multiple_tx_fifo == 0) {
26738 + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
26739 + txfsiz = "DPTXFSIZ";
26740 + } else {
26741 + ep_num = core_if->hwcfg4.b.num_in_eps;
26742 + txfsiz = "DIENPTXF";
26743 + }
26744 + for (i = 0; i < ep_num; i++) {
26745 + addr = &core_if->core_global_regs->dtxfsiz[i];
26746 + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
26747 + (unsigned long)addr, DWC_READ_REG32(addr));
26748 + }
26749 + addr = core_if->pcgcctl;
26750 + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26751 + DWC_READ_REG32(addr));
26752 +}
26753 +
26754 +/**
26755 + * Flush a Tx FIFO.
26756 + *
26757 + * @param core_if Programming view of DWC_otg controller.
26758 + * @param num Tx FIFO to flush.
26759 + */
26760 +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
26761 +{
26762 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26763 + volatile grstctl_t greset = {.d32 = 0 };
26764 + int count = 0;
26765 +
26766 + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
26767 +
26768 + greset.b.txfflsh = 1;
26769 + greset.b.txfnum = num;
26770 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26771 +
26772 + do {
26773 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26774 + if (++count > 10000) {
26775 + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
26776 + __func__, greset.d32,
26777 + DWC_READ_REG32(&global_regs->gnptxsts));
26778 + break;
26779 + }
26780 + dwc_udelay(1);
26781 + } while (greset.b.txfflsh == 1);
26782 +
26783 + /* Wait for 3 PHY Clocks */
26784 + dwc_udelay(1);
26785 +}
26786 +
26787 +/**
26788 + * Flush Rx FIFO.
26789 + *
26790 + * @param core_if Programming view of DWC_otg controller.
26791 + */
26792 +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
26793 +{
26794 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26795 + volatile grstctl_t greset = {.d32 = 0 };
26796 + int count = 0;
26797 +
26798 + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
26799 + /*
26800 + *
26801 + */
26802 + greset.b.rxfflsh = 1;
26803 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26804 +
26805 + do {
26806 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26807 + if (++count > 10000) {
26808 + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
26809 + greset.d32);
26810 + break;
26811 + }
26812 + dwc_udelay(1);
26813 + } while (greset.b.rxfflsh == 1);
26814 +
26815 + /* Wait for 3 PHY Clocks */
26816 + dwc_udelay(1);
26817 +}
26818 +
26819 +/**
26820 + * Do core a soft reset of the core. Be careful with this because it
26821 + * resets all the internal state machines of the core.
26822 + */
26823 +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
26824 +{
26825 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26826 + volatile grstctl_t greset = {.d32 = 0 };
26827 + int count = 0;
26828 +
26829 + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
26830 + /* Wait for AHB master IDLE state. */
26831 + do {
26832 + dwc_udelay(10);
26833 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26834 + if (++count > 100000) {
26835 + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
26836 + greset.d32);
26837 + return;
26838 + }
26839 + }
26840 + while (greset.b.ahbidle == 0);
26841 +
26842 + /* Core Soft Reset */
26843 + count = 0;
26844 + greset.b.csftrst = 1;
26845 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26846 + do {
26847 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26848 + if (++count > 10000) {
26849 + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
26850 + __func__, greset.d32);
26851 + break;
26852 + }
26853 + dwc_udelay(1);
26854 + }
26855 + while (greset.b.csftrst == 1);
26856 +
26857 + /* Wait for 3 PHY Clocks */
26858 + dwc_mdelay(100);
26859 +}
26860 +
26861 +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
26862 +{
26863 + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
26864 +}
26865 +
26866 +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
26867 +{
26868 + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
26869 +}
26870 +
26871 +/**
26872 + * Register HCD callbacks. The callbacks are used to start and stop
26873 + * the HCD for interrupt processing.
26874 + *
26875 + * @param core_if Programming view of DWC_otg controller.
26876 + * @param cb the HCD callback structure.
26877 + * @param p pointer to be passed to callback function (usb_hcd*).
26878 + */
26879 +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
26880 + dwc_otg_cil_callbacks_t * cb, void *p)
26881 +{
26882 + core_if->hcd_cb = cb;
26883 + cb->p = p;
26884 +}
26885 +
26886 +/**
26887 + * Register PCD callbacks. The callbacks are used to start and stop
26888 + * the PCD for interrupt processing.
26889 + *
26890 + * @param core_if Programming view of DWC_otg controller.
26891 + * @param cb the PCD callback structure.
26892 + * @param p pointer to be passed to callback function (pcd*).
26893 + */
26894 +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
26895 + dwc_otg_cil_callbacks_t * cb, void *p)
26896 +{
26897 + core_if->pcd_cb = cb;
26898 + cb->p = p;
26899 +}
26900 +
26901 +#ifdef DWC_EN_ISOC
26902 +
26903 +/**
26904 + * This function writes isoc data per 1 (micro)frame into tx fifo
26905 + *
26906 + * @param core_if Programming view of DWC_otg controller.
26907 + * @param ep The EP to start the transfer on.
26908 + *
26909 + */
26910 +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26911 +{
26912 + dwc_otg_dev_in_ep_regs_t *ep_regs;
26913 + dtxfsts_data_t txstatus = {.d32 = 0 };
26914 + uint32_t len = 0;
26915 + uint32_t dwords;
26916 +
26917 + ep->xfer_len = ep->data_per_frame;
26918 + ep->xfer_count = 0;
26919 +
26920 + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
26921 +
26922 + len = ep->xfer_len - ep->xfer_count;
26923 +
26924 + if (len > ep->maxpacket) {
26925 + len = ep->maxpacket;
26926 + }
26927 +
26928 + dwords = (len + 3) / 4;
26929 +
26930 + /* While there is space in the queue and space in the FIFO and
26931 + * More data to tranfer, Write packets to the Tx FIFO */
26932 + txstatus.d32 =
26933 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
26934 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
26935 +
26936 + while (txstatus.b.txfspcavail > dwords &&
26937 + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
26938 + /* Write the FIFO */
26939 + dwc_otg_ep_write_packet(core_if, ep, 0);
26940 +
26941 + len = ep->xfer_len - ep->xfer_count;
26942 + if (len > ep->maxpacket) {
26943 + len = ep->maxpacket;
26944 + }
26945 +
26946 + dwords = (len + 3) / 4;
26947 + txstatus.d32 =
26948 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
26949 + dtxfsts);
26950 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
26951 + txstatus.d32);
26952 + }
26953 +}
26954 +
26955 +/**
26956 + * This function initializes a descriptor chain for Isochronous transfer
26957 + *
26958 + * @param core_if Programming view of DWC_otg controller.
26959 + * @param ep The EP to start the transfer on.
26960 + *
26961 + */
26962 +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
26963 + dwc_ep_t * ep)
26964 +{
26965 + deptsiz_data_t deptsiz = {.d32 = 0 };
26966 + depctl_data_t depctl = {.d32 = 0 };
26967 + dsts_data_t dsts = {.d32 = 0 };
26968 + volatile uint32_t *addr;
26969 +
26970 + if (ep->is_in) {
26971 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
26972 + } else {
26973 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
26974 + }
26975 +
26976 + ep->xfer_len = ep->data_per_frame;
26977 + ep->xfer_count = 0;
26978 + ep->xfer_buff = ep->cur_pkt_addr;
26979 + ep->dma_addr = ep->cur_pkt_dma_addr;
26980 +
26981 + if (ep->is_in) {
26982 + /* Program the transfer size and packet count
26983 + * as follows: xfersize = N * maxpacket +
26984 + * short_packet pktcnt = N + (short_packet
26985 + * exist ? 1 : 0)
26986 + */
26987 + deptsiz.b.xfersize = ep->xfer_len;
26988 + deptsiz.b.pktcnt =
26989 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
26990 + deptsiz.b.mc = deptsiz.b.pktcnt;
26991 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
26992 + deptsiz.d32);
26993 +
26994 + /* Write the DMA register */
26995 + if (core_if->dma_enable) {
26996 + DWC_WRITE_REG32(&
26997 + (core_if->dev_if->in_ep_regs[ep->num]->
26998 + diepdma), (uint32_t) ep->dma_addr);
26999 + }
27000 + } else {
27001 + deptsiz.b.pktcnt =
27002 + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
27003 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
27004 +
27005 + DWC_WRITE_REG32(&core_if->dev_if->
27006 + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
27007 +
27008 + if (core_if->dma_enable) {
27009 + DWC_WRITE_REG32(&
27010 + (core_if->dev_if->
27011 + out_ep_regs[ep->num]->doepdma),
27012 + (uint32_t) ep->dma_addr);
27013 + }
27014 + }
27015 +
27016 + /** Enable endpoint, clear nak */
27017 +
27018 + depctl.d32 = 0;
27019 + if (ep->bInterval == 1) {
27020 + dsts.d32 =
27021 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
27022 + ep->next_frame = dsts.b.soffn + ep->bInterval;
27023 +
27024 + if (ep->next_frame & 0x1) {
27025 + depctl.b.setd1pid = 1;
27026 + } else {
27027 + depctl.b.setd0pid = 1;
27028 + }
27029 + } else {
27030 + ep->next_frame += ep->bInterval;
27031 +
27032 + if (ep->next_frame & 0x1) {
27033 + depctl.b.setd1pid = 1;
27034 + } else {
27035 + depctl.b.setd0pid = 1;
27036 + }
27037 + }
27038 + depctl.b.epena = 1;
27039 + depctl.b.cnak = 1;
27040 +
27041 + DWC_MODIFY_REG32(addr, 0, depctl.d32);
27042 + depctl.d32 = DWC_READ_REG32(addr);
27043 +
27044 + if (ep->is_in && core_if->dma_enable == 0) {
27045 + write_isoc_frame_data(core_if, ep);
27046 + }
27047 +
27048 +}
27049 +#endif /* DWC_EN_ISOC */
27050 +
27051 +static void dwc_otg_set_uninitialized(int32_t * p, int size)
27052 +{
27053 + int i;
27054 + for (i = 0; i < size; i++) {
27055 + p[i] = -1;
27056 + }
27057 +}
27058 +
27059 +static int dwc_otg_param_initialized(int32_t val)
27060 +{
27061 + return val != -1;
27062 +}
27063 +
27064 +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
27065 +{
27066 + int i;
27067 + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
27068 + if (!core_if->core_params) {
27069 + return -DWC_E_NO_MEMORY;
27070 + }
27071 + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
27072 + sizeof(*core_if->core_params) /
27073 + sizeof(int32_t));
27074 + DWC_PRINTF("Setting default values for core params\n");
27075 + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
27076 + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
27077 + dwc_otg_set_param_dma_desc_enable(core_if,
27078 + dwc_param_dma_desc_enable_default);
27079 + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
27080 + dwc_otg_set_param_dma_burst_size(core_if,
27081 + dwc_param_dma_burst_size_default);
27082 + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
27083 + dwc_param_host_support_fs_ls_low_power_default);
27084 + dwc_otg_set_param_enable_dynamic_fifo(core_if,
27085 + dwc_param_enable_dynamic_fifo_default);
27086 + dwc_otg_set_param_data_fifo_size(core_if,
27087 + dwc_param_data_fifo_size_default);
27088 + dwc_otg_set_param_dev_rx_fifo_size(core_if,
27089 + dwc_param_dev_rx_fifo_size_default);
27090 + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
27091 + dwc_param_dev_nperio_tx_fifo_size_default);
27092 + dwc_otg_set_param_host_rx_fifo_size(core_if,
27093 + dwc_param_host_rx_fifo_size_default);
27094 + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
27095 + dwc_param_host_nperio_tx_fifo_size_default);
27096 + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
27097 + dwc_param_host_perio_tx_fifo_size_default);
27098 + dwc_otg_set_param_max_transfer_size(core_if,
27099 + dwc_param_max_transfer_size_default);
27100 + dwc_otg_set_param_max_packet_count(core_if,
27101 + dwc_param_max_packet_count_default);
27102 + dwc_otg_set_param_host_channels(core_if,
27103 + dwc_param_host_channels_default);
27104 + dwc_otg_set_param_dev_endpoints(core_if,
27105 + dwc_param_dev_endpoints_default);
27106 + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
27107 + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
27108 + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
27109 + dwc_param_host_ls_low_power_phy_clk_default);
27110 + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
27111 + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
27112 + dwc_param_phy_ulpi_ext_vbus_default);
27113 + dwc_otg_set_param_phy_utmi_width(core_if,
27114 + dwc_param_phy_utmi_width_default);
27115 + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
27116 + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
27117 + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
27118 + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
27119 + dwc_param_en_multiple_tx_fifo_default);
27120 + for (i = 0; i < 15; i++) {
27121 + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
27122 + dwc_param_dev_perio_tx_fifo_size_default,
27123 + i);
27124 + }
27125 +
27126 + for (i = 0; i < 15; i++) {
27127 + dwc_otg_set_param_dev_tx_fifo_size(core_if,
27128 + dwc_param_dev_tx_fifo_size_default,
27129 + i);
27130 + }
27131 + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
27132 + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
27133 + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
27134 + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
27135 + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
27136 + dwc_otg_set_param_tx_thr_length(core_if,
27137 + dwc_param_tx_thr_length_default);
27138 + dwc_otg_set_param_rx_thr_length(core_if,
27139 + dwc_param_rx_thr_length_default);
27140 + dwc_otg_set_param_ahb_thr_ratio(core_if,
27141 + dwc_param_ahb_thr_ratio_default);
27142 + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
27143 + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
27144 + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
27145 + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
27146 + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
27147 + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
27148 + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
27149 + DWC_PRINTF("Finished setting default values for core params\n");
27150 +
27151 + return 0;
27152 +}
27153 +
27154 +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
27155 +{
27156 + return core_if->dma_enable;
27157 +}
27158 +
27159 +/* Checks if the parameter is outside of its valid range of values */
27160 +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
27161 + (((_param_) < (_low_)) || \
27162 + ((_param_) > (_high_)))
27163 +
27164 +/* Parameter access functions */
27165 +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
27166 +{
27167 + int valid;
27168 + int retval = 0;
27169 + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
27170 + DWC_WARN("Wrong value for otg_cap parameter\n");
27171 + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
27172 + retval = -DWC_E_INVALID;
27173 + goto out;
27174 + }
27175 +
27176 + valid = 1;
27177 + switch (val) {
27178 + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
27179 + if (core_if->hwcfg2.b.op_mode !=
27180 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
27181 + valid = 0;
27182 + break;
27183 + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
27184 + if ((core_if->hwcfg2.b.op_mode !=
27185 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
27186 + && (core_if->hwcfg2.b.op_mode !=
27187 + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
27188 + && (core_if->hwcfg2.b.op_mode !=
27189 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
27190 + && (core_if->hwcfg2.b.op_mode !=
27191 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
27192 + valid = 0;
27193 + }
27194 + break;
27195 + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
27196 + /* always valid */
27197 + break;
27198 + }
27199 + if (!valid) {
27200 + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
27201 + DWC_ERROR
27202 + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
27203 + val);
27204 + }
27205 + val =
27206 + (((core_if->hwcfg2.b.op_mode ==
27207 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
27208 + || (core_if->hwcfg2.b.op_mode ==
27209 + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
27210 + || (core_if->hwcfg2.b.op_mode ==
27211 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
27212 + || (core_if->hwcfg2.b.op_mode ==
27213 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
27214 + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
27215 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
27216 + retval = -DWC_E_INVALID;
27217 + }
27218 +
27219 + core_if->core_params->otg_cap = val;
27220 +out:
27221 + return retval;
27222 +}
27223 +
27224 +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
27225 +{
27226 + return core_if->core_params->otg_cap;
27227 +}
27228 +
27229 +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
27230 +{
27231 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27232 + DWC_WARN("Wrong value for opt parameter\n");
27233 + return -DWC_E_INVALID;
27234 + }
27235 + core_if->core_params->opt = val;
27236 + return 0;
27237 +}
27238 +
27239 +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
27240 +{
27241 + return core_if->core_params->opt;
27242 +}
27243 +
27244 +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
27245 +{
27246 + int retval = 0;
27247 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27248 + DWC_WARN("Wrong value for dma enable\n");
27249 + return -DWC_E_INVALID;
27250 + }
27251 +
27252 + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
27253 + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
27254 + DWC_ERROR
27255 + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
27256 + val);
27257 + }
27258 + val = 0;
27259 + retval = -DWC_E_INVALID;
27260 + }
27261 +
27262 + core_if->core_params->dma_enable = val;
27263 + if (val == 0) {
27264 + dwc_otg_set_param_dma_desc_enable(core_if, 0);
27265 + }
27266 + return retval;
27267 +}
27268 +
27269 +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
27270 +{
27271 + return core_if->core_params->dma_enable;
27272 +}
27273 +
27274 +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
27275 +{
27276 + int retval = 0;
27277 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27278 + DWC_WARN("Wrong value for dma_enable\n");
27279 + DWC_WARN("dma_desc_enable must be 0 or 1\n");
27280 + return -DWC_E_INVALID;
27281 + }
27282 +
27283 + if ((val == 1)
27284 + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
27285 + || (core_if->hwcfg4.b.desc_dma == 0))) {
27286 + if (dwc_otg_param_initialized
27287 + (core_if->core_params->dma_desc_enable)) {
27288 + DWC_ERROR
27289 + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
27290 + val);
27291 + }
27292 + val = 0;
27293 + retval = -DWC_E_INVALID;
27294 + }
27295 + core_if->core_params->dma_desc_enable = val;
27296 + return retval;
27297 +}
27298 +
27299 +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
27300 +{
27301 + return core_if->core_params->dma_desc_enable;
27302 +}
27303 +
27304 +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
27305 + int32_t val)
27306 +{
27307 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27308 + DWC_WARN("Wrong value for host_support_fs_low_power\n");
27309 + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
27310 + return -DWC_E_INVALID;
27311 + }
27312 + core_if->core_params->host_support_fs_ls_low_power = val;
27313 + return 0;
27314 +}
27315 +
27316 +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
27317 + core_if)
27318 +{
27319 + return core_if->core_params->host_support_fs_ls_low_power;
27320 +}
27321 +
27322 +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
27323 + int32_t val)
27324 +{
27325 + int retval = 0;
27326 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27327 + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
27328 + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
27329 + return -DWC_E_INVALID;
27330 + }
27331 +
27332 + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
27333 + if (dwc_otg_param_initialized
27334 + (core_if->core_params->enable_dynamic_fifo)) {
27335 + DWC_ERROR
27336 + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
27337 + val);
27338 + }
27339 + val = 0;
27340 + retval = -DWC_E_INVALID;
27341 + }
27342 + core_if->core_params->enable_dynamic_fifo = val;
27343 + return retval;
27344 +}
27345 +
27346 +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
27347 +{
27348 + return core_if->core_params->enable_dynamic_fifo;
27349 +}
27350 +
27351 +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
27352 +{
27353 + int retval = 0;
27354 + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
27355 + DWC_WARN("Wrong value for data_fifo_size\n");
27356 + DWC_WARN("data_fifo_size must be 32-32768\n");
27357 + return -DWC_E_INVALID;
27358 + }
27359 +
27360 + if (val > core_if->hwcfg3.b.dfifo_depth) {
27361 + if (dwc_otg_param_initialized
27362 + (core_if->core_params->data_fifo_size)) {
27363 + DWC_ERROR
27364 + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
27365 + val);
27366 + }
27367 + val = core_if->hwcfg3.b.dfifo_depth;
27368 + retval = -DWC_E_INVALID;
27369 + }
27370 +
27371 + core_if->core_params->data_fifo_size = val;
27372 + return retval;
27373 +}
27374 +
27375 +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
27376 +{
27377 + return core_if->core_params->data_fifo_size;
27378 +}
27379 +
27380 +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
27381 +{
27382 + int retval = 0;
27383 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27384 + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
27385 + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
27386 + return -DWC_E_INVALID;
27387 + }
27388 +
27389 + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
27390 + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
27391 + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
27392 + }
27393 + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
27394 + retval = -DWC_E_INVALID;
27395 + }
27396 +
27397 + core_if->core_params->dev_rx_fifo_size = val;
27398 + return retval;
27399 +}
27400 +
27401 +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
27402 +{
27403 + return core_if->core_params->dev_rx_fifo_size;
27404 +}
27405 +
27406 +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27407 + int32_t val)
27408 +{
27409 + int retval = 0;
27410 +
27411 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27412 + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
27413 + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
27414 + return -DWC_E_INVALID;
27415 + }
27416 +
27417 + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
27418 + if (dwc_otg_param_initialized
27419 + (core_if->core_params->dev_nperio_tx_fifo_size)) {
27420 + DWC_ERROR
27421 + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
27422 + val);
27423 + }
27424 + val =
27425 + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
27426 + 16);
27427 + retval = -DWC_E_INVALID;
27428 + }
27429 +
27430 + core_if->core_params->dev_nperio_tx_fifo_size = val;
27431 + return retval;
27432 +}
27433 +
27434 +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27435 +{
27436 + return core_if->core_params->dev_nperio_tx_fifo_size;
27437 +}
27438 +
27439 +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
27440 + int32_t val)
27441 +{
27442 + int retval = 0;
27443 +
27444 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27445 + DWC_WARN("Wrong value for host_rx_fifo_size\n");
27446 + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
27447 + return -DWC_E_INVALID;
27448 + }
27449 +
27450 + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
27451 + if (dwc_otg_param_initialized
27452 + (core_if->core_params->host_rx_fifo_size)) {
27453 + DWC_ERROR
27454 + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
27455 + val);
27456 + }
27457 + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
27458 + retval = -DWC_E_INVALID;
27459 + }
27460 +
27461 + core_if->core_params->host_rx_fifo_size = val;
27462 + return retval;
27463 +
27464 +}
27465 +
27466 +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
27467 +{
27468 + return core_if->core_params->host_rx_fifo_size;
27469 +}
27470 +
27471 +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27472 + int32_t val)
27473 +{
27474 + int retval = 0;
27475 +
27476 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27477 + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
27478 + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
27479 + return -DWC_E_INVALID;
27480 + }
27481 +
27482 + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
27483 + if (dwc_otg_param_initialized
27484 + (core_if->core_params->host_nperio_tx_fifo_size)) {
27485 + DWC_ERROR
27486 + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
27487 + val);
27488 + }
27489 + val =
27490 + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
27491 + 16);
27492 + retval = -DWC_E_INVALID;
27493 + }
27494 +
27495 + core_if->core_params->host_nperio_tx_fifo_size = val;
27496 + return retval;
27497 +}
27498 +
27499 +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27500 +{
27501 + return core_if->core_params->host_nperio_tx_fifo_size;
27502 +}
27503 +
27504 +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27505 + int32_t val)
27506 +{
27507 + int retval = 0;
27508 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27509 + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
27510 + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
27511 + return -DWC_E_INVALID;
27512 + }
27513 +
27514 + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
27515 + if (dwc_otg_param_initialized
27516 + (core_if->core_params->host_perio_tx_fifo_size)) {
27517 + DWC_ERROR
27518 + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
27519 + val);
27520 + }
27521 + val = (core_if->hptxfsiz.d32) >> 16;
27522 + retval = -DWC_E_INVALID;
27523 + }
27524 +
27525 + core_if->core_params->host_perio_tx_fifo_size = val;
27526 + return retval;
27527 +}
27528 +
27529 +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27530 +{
27531 + return core_if->core_params->host_perio_tx_fifo_size;
27532 +}
27533 +
27534 +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
27535 + int32_t val)
27536 +{
27537 + int retval = 0;
27538 +
27539 + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
27540 + DWC_WARN("Wrong value for max_transfer_size\n");
27541 + DWC_WARN("max_transfer_size must be 2047-524288\n");
27542 + return -DWC_E_INVALID;
27543 + }
27544 +
27545 + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
27546 + if (dwc_otg_param_initialized
27547 + (core_if->core_params->max_transfer_size)) {
27548 + DWC_ERROR
27549 + ("%d invalid for max_transfer_size. Check HW configuration.\n",
27550 + val);
27551 + }
27552 + val =
27553 + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
27554 + 1);
27555 + retval = -DWC_E_INVALID;
27556 + }
27557 +
27558 + core_if->core_params->max_transfer_size = val;
27559 + return retval;
27560 +}
27561 +
27562 +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
27563 +{
27564 + return core_if->core_params->max_transfer_size;
27565 +}
27566 +
27567 +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
27568 +{
27569 + int retval = 0;
27570 +
27571 + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
27572 + DWC_WARN("Wrong value for max_packet_count\n");
27573 + DWC_WARN("max_packet_count must be 15-511\n");
27574 + return -DWC_E_INVALID;
27575 + }
27576 +
27577 + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
27578 + if (dwc_otg_param_initialized
27579 + (core_if->core_params->max_packet_count)) {
27580 + DWC_ERROR
27581 + ("%d invalid for max_packet_count. Check HW configuration.\n",
27582 + val);
27583 + }
27584 + val =
27585 + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
27586 + retval = -DWC_E_INVALID;
27587 + }
27588 +
27589 + core_if->core_params->max_packet_count = val;
27590 + return retval;
27591 +}
27592 +
27593 +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
27594 +{
27595 + return core_if->core_params->max_packet_count;
27596 +}
27597 +
27598 +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
27599 +{
27600 + int retval = 0;
27601 +
27602 + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
27603 + DWC_WARN("Wrong value for host_channels\n");
27604 + DWC_WARN("host_channels must be 1-16\n");
27605 + return -DWC_E_INVALID;
27606 + }
27607 +
27608 + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
27609 + if (dwc_otg_param_initialized
27610 + (core_if->core_params->host_channels)) {
27611 + DWC_ERROR
27612 + ("%d invalid for host_channels. Check HW configurations.\n",
27613 + val);
27614 + }
27615 + val = (core_if->hwcfg2.b.num_host_chan + 1);
27616 + retval = -DWC_E_INVALID;
27617 + }
27618 +
27619 + core_if->core_params->host_channels = val;
27620 + return retval;
27621 +}
27622 +
27623 +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
27624 +{
27625 + return core_if->core_params->host_channels;
27626 +}
27627 +
27628 +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
27629 +{
27630 + int retval = 0;
27631 +
27632 + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
27633 + DWC_WARN("Wrong value for dev_endpoints\n");
27634 + DWC_WARN("dev_endpoints must be 1-15\n");
27635 + return -DWC_E_INVALID;
27636 + }
27637 +
27638 + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
27639 + if (dwc_otg_param_initialized
27640 + (core_if->core_params->dev_endpoints)) {
27641 + DWC_ERROR
27642 + ("%d invalid for dev_endpoints. Check HW configurations.\n",
27643 + val);
27644 + }
27645 + val = core_if->hwcfg2.b.num_dev_ep;
27646 + retval = -DWC_E_INVALID;
27647 + }
27648 +
27649 + core_if->core_params->dev_endpoints = val;
27650 + return retval;
27651 +}
27652 +
27653 +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
27654 +{
27655 + return core_if->core_params->dev_endpoints;
27656 +}
27657 +
27658 +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
27659 +{
27660 + int retval = 0;
27661 + int valid = 0;
27662 +
27663 + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
27664 + DWC_WARN("Wrong value for phy_type\n");
27665 + DWC_WARN("phy_type must be 0,1 or 2\n");
27666 + return -DWC_E_INVALID;
27667 + }
27668 +#ifndef NO_FS_PHY_HW_CHECKS
27669 + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
27670 + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
27671 + (core_if->hwcfg2.b.hs_phy_type == 3))) {
27672 + valid = 1;
27673 + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
27674 + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
27675 + (core_if->hwcfg2.b.hs_phy_type == 3))) {
27676 + valid = 1;
27677 + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
27678 + (core_if->hwcfg2.b.fs_phy_type == 1)) {
27679 + valid = 1;
27680 + }
27681 + if (!valid) {
27682 + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
27683 + DWC_ERROR
27684 + ("%d invalid for phy_type. Check HW configurations.\n",
27685 + val);
27686 + }
27687 + if (core_if->hwcfg2.b.hs_phy_type) {
27688 + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
27689 + (core_if->hwcfg2.b.hs_phy_type == 1)) {
27690 + val = DWC_PHY_TYPE_PARAM_UTMI;
27691 + } else {
27692 + val = DWC_PHY_TYPE_PARAM_ULPI;
27693 + }
27694 + }
27695 + retval = -DWC_E_INVALID;
27696 + }
27697 +#endif
27698 + core_if->core_params->phy_type = val;
27699 + return retval;
27700 +}
27701 +
27702 +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
27703 +{
27704 + return core_if->core_params->phy_type;
27705 +}
27706 +
27707 +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
27708 +{
27709 + int retval = 0;
27710 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27711 + DWC_WARN("Wrong value for speed parameter\n");
27712 + DWC_WARN("max_speed parameter must be 0 or 1\n");
27713 + return -DWC_E_INVALID;
27714 + }
27715 + if ((val == 0)
27716 + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
27717 + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
27718 + DWC_ERROR
27719 + ("%d invalid for speed paremter. Check HW configuration.\n",
27720 + val);
27721 + }
27722 + val =
27723 + (dwc_otg_get_param_phy_type(core_if) ==
27724 + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
27725 + retval = -DWC_E_INVALID;
27726 + }
27727 + core_if->core_params->speed = val;
27728 + return retval;
27729 +}
27730 +
27731 +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
27732 +{
27733 + return core_if->core_params->speed;
27734 +}
27735 +
27736 +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
27737 + int32_t val)
27738 +{
27739 + int retval = 0;
27740 +
27741 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27742 + DWC_WARN
27743 + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
27744 + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
27745 + return -DWC_E_INVALID;
27746 + }
27747 +
27748 + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
27749 + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
27750 + if (dwc_otg_param_initialized
27751 + (core_if->core_params->host_ls_low_power_phy_clk)) {
27752 + DWC_ERROR
27753 + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
27754 + val);
27755 + }
27756 + val =
27757 + (dwc_otg_get_param_phy_type(core_if) ==
27758 + DWC_PHY_TYPE_PARAM_FS) ?
27759 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
27760 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
27761 + retval = -DWC_E_INVALID;
27762 + }
27763 +
27764 + core_if->core_params->host_ls_low_power_phy_clk = val;
27765 + return retval;
27766 +}
27767 +
27768 +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
27769 +{
27770 + return core_if->core_params->host_ls_low_power_phy_clk;
27771 +}
27772 +
27773 +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
27774 +{
27775 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27776 + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
27777 + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
27778 + return -DWC_E_INVALID;
27779 + }
27780 +
27781 + core_if->core_params->phy_ulpi_ddr = val;
27782 + return 0;
27783 +}
27784 +
27785 +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
27786 +{
27787 + return core_if->core_params->phy_ulpi_ddr;
27788 +}
27789 +
27790 +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
27791 + int32_t val)
27792 +{
27793 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27794 + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
27795 + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
27796 + return -DWC_E_INVALID;
27797 + }
27798 +
27799 + core_if->core_params->phy_ulpi_ext_vbus = val;
27800 + return 0;
27801 +}
27802 +
27803 +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
27804 +{
27805 + return core_if->core_params->phy_ulpi_ext_vbus;
27806 +}
27807 +
27808 +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
27809 +{
27810 + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
27811 + DWC_WARN("Wrong valaue for phy_utmi_width\n");
27812 + DWC_WARN("phy_utmi_width must be 8 or 16\n");
27813 + return -DWC_E_INVALID;
27814 + }
27815 +
27816 + core_if->core_params->phy_utmi_width = val;
27817 + return 0;
27818 +}
27819 +
27820 +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
27821 +{
27822 + return core_if->core_params->phy_utmi_width;
27823 +}
27824 +
27825 +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
27826 +{
27827 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27828 + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
27829 + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
27830 + return -DWC_E_INVALID;
27831 + }
27832 +
27833 + core_if->core_params->ulpi_fs_ls = val;
27834 + return 0;
27835 +}
27836 +
27837 +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
27838 +{
27839 + return core_if->core_params->ulpi_fs_ls;
27840 +}
27841 +
27842 +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
27843 +{
27844 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27845 + DWC_WARN("Wrong valaue for ts_dline\n");
27846 + DWC_WARN("ts_dline must be 0 or 1\n");
27847 + return -DWC_E_INVALID;
27848 + }
27849 +
27850 + core_if->core_params->ts_dline = val;
27851 + return 0;
27852 +}
27853 +
27854 +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
27855 +{
27856 + return core_if->core_params->ts_dline;
27857 +}
27858 +
27859 +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
27860 +{
27861 + int retval = 0;
27862 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27863 + DWC_WARN("Wrong valaue for i2c_enable\n");
27864 + DWC_WARN("i2c_enable must be 0 or 1\n");
27865 + return -DWC_E_INVALID;
27866 + }
27867 +#ifndef NO_FS_PHY_HW_CHECK
27868 + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
27869 + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
27870 + DWC_ERROR
27871 + ("%d invalid for i2c_enable. Check HW configuration.\n",
27872 + val);
27873 + }
27874 + val = 0;
27875 + retval = -DWC_E_INVALID;
27876 + }
27877 +#endif
27878 +
27879 + core_if->core_params->i2c_enable = val;
27880 + return retval;
27881 +}
27882 +
27883 +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
27884 +{
27885 + return core_if->core_params->i2c_enable;
27886 +}
27887 +
27888 +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27889 + int32_t val, int fifo_num)
27890 +{
27891 + int retval = 0;
27892 +
27893 + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
27894 + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
27895 + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
27896 + return -DWC_E_INVALID;
27897 + }
27898 +
27899 + if (val >
27900 + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
27901 + if (dwc_otg_param_initialized
27902 + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
27903 + DWC_ERROR
27904 + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
27905 + val, fifo_num);
27906 + }
27907 + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
27908 + retval = -DWC_E_INVALID;
27909 + }
27910 +
27911 + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
27912 + return retval;
27913 +}
27914 +
27915 +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27916 + int fifo_num)
27917 +{
27918 + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
27919 +}
27920 +
27921 +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
27922 + int32_t val)
27923 +{
27924 + int retval = 0;
27925 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27926 + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
27927 + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
27928 + return -DWC_E_INVALID;
27929 + }
27930 +
27931 + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
27932 + if (dwc_otg_param_initialized
27933 + (core_if->core_params->en_multiple_tx_fifo)) {
27934 + DWC_ERROR
27935 + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
27936 + val);
27937 + }
27938 + val = 0;
27939 + retval = -DWC_E_INVALID;
27940 + }
27941 +
27942 + core_if->core_params->en_multiple_tx_fifo = val;
27943 + return retval;
27944 +}
27945 +
27946 +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
27947 +{
27948 + return core_if->core_params->en_multiple_tx_fifo;
27949 +}
27950 +
27951 +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
27952 + int fifo_num)
27953 +{
27954 + int retval = 0;
27955 +
27956 + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
27957 + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
27958 + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
27959 + return -DWC_E_INVALID;
27960 + }
27961 +
27962 + if (val >
27963 + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
27964 + if (dwc_otg_param_initialized
27965 + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
27966 + DWC_ERROR
27967 + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
27968 + val, fifo_num);
27969 + }
27970 + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
27971 + retval = -DWC_E_INVALID;
27972 + }
27973 +
27974 + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
27975 + return retval;
27976 +}
27977 +
27978 +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
27979 + int fifo_num)
27980 +{
27981 + return core_if->core_params->dev_tx_fifo_size[fifo_num];
27982 +}
27983 +
27984 +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
27985 +{
27986 + int retval = 0;
27987 +
27988 + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
27989 + DWC_WARN("Wrong value for thr_ctl\n");
27990 + DWC_WARN("thr_ctl must be 0-7\n");
27991 + return -DWC_E_INVALID;
27992 + }
27993 +
27994 + if ((val != 0) &&
27995 + (!dwc_otg_get_param_dma_enable(core_if) ||
27996 + !core_if->hwcfg4.b.ded_fifo_en)) {
27997 + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
27998 + DWC_ERROR
27999 + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
28000 + val);
28001 + }
28002 + val = 0;
28003 + retval = -DWC_E_INVALID;
28004 + }
28005 +
28006 + core_if->core_params->thr_ctl = val;
28007 + return retval;
28008 +}
28009 +
28010 +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
28011 +{
28012 + return core_if->core_params->thr_ctl;
28013 +}
28014 +
28015 +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
28016 +{
28017 + int retval = 0;
28018 +
28019 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28020 + DWC_WARN("Wrong value for lpm_enable\n");
28021 + DWC_WARN("lpm_enable must be 0 or 1\n");
28022 + return -DWC_E_INVALID;
28023 + }
28024 +
28025 + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
28026 + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
28027 + DWC_ERROR
28028 + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
28029 + val);
28030 + }
28031 + val = 0;
28032 + retval = -DWC_E_INVALID;
28033 + }
28034 +
28035 + core_if->core_params->lpm_enable = val;
28036 + return retval;
28037 +}
28038 +
28039 +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
28040 +{
28041 + return core_if->core_params->lpm_enable;
28042 +}
28043 +
28044 +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
28045 +{
28046 + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
28047 + DWC_WARN("Wrong valaue for tx_thr_length\n");
28048 + DWC_WARN("tx_thr_length must be 8 - 128\n");
28049 + return -DWC_E_INVALID;
28050 + }
28051 +
28052 + core_if->core_params->tx_thr_length = val;
28053 + return 0;
28054 +}
28055 +
28056 +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
28057 +{
28058 + return core_if->core_params->tx_thr_length;
28059 +}
28060 +
28061 +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
28062 +{
28063 + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
28064 + DWC_WARN("Wrong valaue for rx_thr_length\n");
28065 + DWC_WARN("rx_thr_length must be 8 - 128\n");
28066 + return -DWC_E_INVALID;
28067 + }
28068 +
28069 + core_if->core_params->rx_thr_length = val;
28070 + return 0;
28071 +}
28072 +
28073 +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
28074 +{
28075 + return core_if->core_params->rx_thr_length;
28076 +}
28077 +
28078 +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
28079 +{
28080 + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
28081 + DWC_OTG_PARAM_TEST(val, 4, 4) &&
28082 + DWC_OTG_PARAM_TEST(val, 8, 8) &&
28083 + DWC_OTG_PARAM_TEST(val, 16, 16) &&
28084 + DWC_OTG_PARAM_TEST(val, 32, 32) &&
28085 + DWC_OTG_PARAM_TEST(val, 64, 64) &&
28086 + DWC_OTG_PARAM_TEST(val, 128, 128) &&
28087 + DWC_OTG_PARAM_TEST(val, 256, 256)) {
28088 + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
28089 + return -DWC_E_INVALID;
28090 + }
28091 + core_if->core_params->dma_burst_size = val;
28092 + return 0;
28093 +}
28094 +
28095 +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
28096 +{
28097 + return core_if->core_params->dma_burst_size;
28098 +}
28099 +
28100 +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
28101 +{
28102 + int retval = 0;
28103 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28104 + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
28105 + return -DWC_E_INVALID;
28106 + }
28107 + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
28108 + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
28109 + DWC_ERROR
28110 + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
28111 + val);
28112 + }
28113 + retval = -DWC_E_INVALID;
28114 + val = 0;
28115 + }
28116 + core_if->core_params->pti_enable = val;
28117 + return retval;
28118 +}
28119 +
28120 +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
28121 +{
28122 + return core_if->core_params->pti_enable;
28123 +}
28124 +
28125 +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
28126 +{
28127 + int retval = 0;
28128 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28129 + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
28130 + return -DWC_E_INVALID;
28131 + }
28132 + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
28133 + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
28134 + DWC_ERROR
28135 + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
28136 + val);
28137 + }
28138 + retval = -DWC_E_INVALID;
28139 + val = 0;
28140 + }
28141 + core_if->core_params->mpi_enable = val;
28142 + return retval;
28143 +}
28144 +
28145 +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
28146 +{
28147 + return core_if->core_params->mpi_enable;
28148 +}
28149 +
28150 +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
28151 +{
28152 + int retval = 0;
28153 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28154 + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
28155 + return -DWC_E_INVALID;
28156 + }
28157 + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
28158 + if (dwc_otg_param_initialized
28159 + (core_if->core_params->adp_supp_enable)) {
28160 + DWC_ERROR
28161 + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
28162 + val);
28163 + }
28164 + retval = -DWC_E_INVALID;
28165 + val = 0;
28166 + }
28167 + core_if->core_params->adp_supp_enable = val;
28168 + /*Set OTG version 2.0 in case of enabling ADP*/
28169 + if (val)
28170 + dwc_otg_set_param_otg_ver(core_if, 1);
28171 +
28172 + return retval;
28173 +}
28174 +
28175 +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
28176 +{
28177 + return core_if->core_params->adp_supp_enable;
28178 +}
28179 +
28180 +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
28181 +{
28182 + int retval = 0;
28183 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28184 + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
28185 + DWC_WARN("ic_usb_cap must be 0 or 1\n");
28186 + return -DWC_E_INVALID;
28187 + }
28188 +
28189 + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
28190 + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
28191 + DWC_ERROR
28192 + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
28193 + val);
28194 + }
28195 + retval = -DWC_E_INVALID;
28196 + val = 0;
28197 + }
28198 + core_if->core_params->ic_usb_cap = val;
28199 + return retval;
28200 +}
28201 +
28202 +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
28203 +{
28204 + return core_if->core_params->ic_usb_cap;
28205 +}
28206 +
28207 +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
28208 +{
28209 + int retval = 0;
28210 + int valid = 1;
28211 +
28212 + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
28213 + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
28214 + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
28215 + return -DWC_E_INVALID;
28216 + }
28217 +
28218 + if (val
28219 + && (core_if->snpsid < OTG_CORE_REV_2_81a
28220 + || !dwc_otg_get_param_thr_ctl(core_if))) {
28221 + valid = 0;
28222 + } else if (val
28223 + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
28224 + 4)) {
28225 + valid = 0;
28226 + }
28227 + if (valid == 0) {
28228 + if (dwc_otg_param_initialized
28229 + (core_if->core_params->ahb_thr_ratio)) {
28230 + DWC_ERROR
28231 + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
28232 + val);
28233 + }
28234 + retval = -DWC_E_INVALID;
28235 + val = 0;
28236 + }
28237 +
28238 + core_if->core_params->ahb_thr_ratio = val;
28239 + return retval;
28240 +}
28241 +
28242 +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
28243 +{
28244 + return core_if->core_params->ahb_thr_ratio;
28245 +}
28246 +
28247 +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
28248 +{
28249 + int retval = 0;
28250 + int valid = 1;
28251 + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
28252 + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
28253 +
28254 + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
28255 + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
28256 + DWC_WARN("power_down must be 0 - 2\n");
28257 + return -DWC_E_INVALID;
28258 + }
28259 +
28260 + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
28261 + valid = 0;
28262 + }
28263 + if ((val == 3)
28264 + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
28265 + || (hwcfg4.b.xhiber == 0))) {
28266 + valid = 0;
28267 + }
28268 + if (valid == 0) {
28269 + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
28270 + DWC_ERROR
28271 + ("%d invalid for parameter power_down. Check HW configuration.\n",
28272 + val);
28273 + }
28274 + retval = -DWC_E_INVALID;
28275 + val = 0;
28276 + }
28277 + core_if->core_params->power_down = val;
28278 + return retval;
28279 +}
28280 +
28281 +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
28282 +{
28283 + return core_if->core_params->power_down;
28284 +}
28285 +
28286 +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
28287 +{
28288 + int retval = 0;
28289 + int valid = 1;
28290 +
28291 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28292 + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
28293 + DWC_WARN("reload_ctl must be 0 or 1\n");
28294 + return -DWC_E_INVALID;
28295 + }
28296 +
28297 + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
28298 + valid = 0;
28299 + }
28300 + if (valid == 0) {
28301 + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
28302 + DWC_ERROR("%d invalid for parameter reload_ctl."
28303 + "Check HW configuration.\n", val);
28304 + }
28305 + retval = -DWC_E_INVALID;
28306 + val = 0;
28307 + }
28308 + core_if->core_params->reload_ctl = val;
28309 + return retval;
28310 +}
28311 +
28312 +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
28313 +{
28314 + return core_if->core_params->reload_ctl;
28315 +}
28316 +
28317 +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
28318 +{
28319 + int retval = 0;
28320 + int valid = 1;
28321 +
28322 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28323 + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
28324 + DWC_WARN("dev_out_nak must be 0 or 1\n");
28325 + return -DWC_E_INVALID;
28326 + }
28327 +
28328 + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
28329 + !(core_if->core_params->dma_desc_enable))) {
28330 + valid = 0;
28331 + }
28332 + if (valid == 0) {
28333 + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
28334 + DWC_ERROR("%d invalid for parameter dev_out_nak."
28335 + "Check HW configuration.\n", val);
28336 + }
28337 + retval = -DWC_E_INVALID;
28338 + val = 0;
28339 + }
28340 + core_if->core_params->dev_out_nak = val;
28341 + return retval;
28342 +}
28343 +
28344 +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
28345 +{
28346 + return core_if->core_params->dev_out_nak;
28347 +}
28348 +
28349 +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
28350 +{
28351 + int retval = 0;
28352 + int valid = 1;
28353 +
28354 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28355 + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
28356 + DWC_WARN("cont_on_bna must be 0 or 1\n");
28357 + return -DWC_E_INVALID;
28358 + }
28359 +
28360 + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
28361 + !(core_if->core_params->dma_desc_enable))) {
28362 + valid = 0;
28363 + }
28364 + if (valid == 0) {
28365 + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
28366 + DWC_ERROR("%d invalid for parameter cont_on_bna."
28367 + "Check HW configuration.\n", val);
28368 + }
28369 + retval = -DWC_E_INVALID;
28370 + val = 0;
28371 + }
28372 + core_if->core_params->cont_on_bna = val;
28373 + return retval;
28374 +}
28375 +
28376 +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
28377 +{
28378 + return core_if->core_params->cont_on_bna;
28379 +}
28380 +
28381 +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
28382 +{
28383 + int retval = 0;
28384 + int valid = 1;
28385 +
28386 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28387 + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
28388 + DWC_WARN("ahb_single must be 0 or 1\n");
28389 + return -DWC_E_INVALID;
28390 + }
28391 +
28392 + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
28393 + valid = 0;
28394 + }
28395 + if (valid == 0) {
28396 + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
28397 + DWC_ERROR("%d invalid for parameter ahb_single."
28398 + "Check HW configuration.\n", val);
28399 + }
28400 + retval = -DWC_E_INVALID;
28401 + val = 0;
28402 + }
28403 + core_if->core_params->ahb_single = val;
28404 + return retval;
28405 +}
28406 +
28407 +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
28408 +{
28409 + return core_if->core_params->ahb_single;
28410 +}
28411 +
28412 +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
28413 +{
28414 + int retval = 0;
28415 +
28416 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28417 + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
28418 + DWC_WARN
28419 + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
28420 + return -DWC_E_INVALID;
28421 + }
28422 +
28423 + core_if->core_params->otg_ver = val;
28424 + return retval;
28425 +}
28426 +
28427 +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
28428 +{
28429 + return core_if->core_params->otg_ver;
28430 +}
28431 +
28432 +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
28433 +{
28434 + gotgctl_data_t otgctl;
28435 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28436 + return otgctl.b.hstnegscs;
28437 +}
28438 +
28439 +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
28440 +{
28441 + gotgctl_data_t otgctl;
28442 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28443 + return otgctl.b.sesreqscs;
28444 +}
28445 +
28446 +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
28447 +{
28448 + if(core_if->otg_ver == 0) {
28449 + gotgctl_data_t otgctl;
28450 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28451 + otgctl.b.hnpreq = val;
28452 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
28453 + } else {
28454 + core_if->otg_sts = val;
28455 + }
28456 +}
28457 +
28458 +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
28459 +{
28460 + return core_if->snpsid;
28461 +}
28462 +
28463 +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
28464 +{
28465 + gintsts_data_t gintsts;
28466 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
28467 + return gintsts.b.curmode;
28468 +}
28469 +
28470 +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
28471 +{
28472 + gusbcfg_data_t usbcfg;
28473 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28474 + return usbcfg.b.hnpcap;
28475 +}
28476 +
28477 +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
28478 +{
28479 + gusbcfg_data_t usbcfg;
28480 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28481 + usbcfg.b.hnpcap = val;
28482 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
28483 +}
28484 +
28485 +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
28486 +{
28487 + gusbcfg_data_t usbcfg;
28488 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28489 + return usbcfg.b.srpcap;
28490 +}
28491 +
28492 +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
28493 +{
28494 + gusbcfg_data_t usbcfg;
28495 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28496 + usbcfg.b.srpcap = val;
28497 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
28498 +}
28499 +
28500 +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
28501 +{
28502 + dcfg_data_t dcfg;
28503 + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
28504 +
28505 + dcfg.d32 = -1; //GRAYG
28506 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
28507 + if (NULL == core_if)
28508 + DWC_ERROR("reg request with NULL core_if\n");
28509 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
28510 + core_if, core_if->dev_if);
28511 + if (NULL == core_if->dev_if)
28512 + DWC_ERROR("reg request with NULL dev_if\n");
28513 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
28514 + "dev_global_regs(%p)\n", __func__,
28515 + core_if, core_if->dev_if,
28516 + core_if->dev_if->dev_global_regs);
28517 + if (NULL == core_if->dev_if->dev_global_regs)
28518 + DWC_ERROR("reg request with NULL dev_global_regs\n");
28519 + else {
28520 + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
28521 + "dev_global_regs(%p)->dcfg = %p\n", __func__,
28522 + core_if, core_if->dev_if,
28523 + core_if->dev_if->dev_global_regs,
28524 + &core_if->dev_if->dev_global_regs->dcfg);
28525 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
28526 + }
28527 + return dcfg.b.devspd;
28528 +}
28529 +
28530 +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
28531 +{
28532 + dcfg_data_t dcfg;
28533 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
28534 + dcfg.b.devspd = val;
28535 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
28536 +}
28537 +
28538 +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
28539 +{
28540 + hprt0_data_t hprt0;
28541 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28542 + return hprt0.b.prtconnsts;
28543 +}
28544 +
28545 +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
28546 +{
28547 + dsts_data_t dsts;
28548 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
28549 + return dsts.b.enumspd;
28550 +}
28551 +
28552 +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
28553 +{
28554 + hprt0_data_t hprt0;
28555 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28556 + return hprt0.b.prtpwr;
28557 +
28558 +}
28559 +
28560 +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
28561 +{
28562 + return core_if->hibernation_suspend;
28563 +}
28564 +
28565 +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
28566 +{
28567 + hprt0_data_t hprt0;
28568 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28569 + hprt0.b.prtpwr = val;
28570 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28571 +}
28572 +
28573 +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
28574 +{
28575 + hprt0_data_t hprt0;
28576 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28577 + return hprt0.b.prtsusp;
28578 +
28579 +}
28580 +
28581 +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
28582 +{
28583 + hprt0_data_t hprt0;
28584 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28585 + hprt0.b.prtsusp = val;
28586 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28587 +}
28588 +
28589 +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
28590 +{
28591 + hfir_data_t hfir;
28592 + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
28593 + return hfir.b.frint;
28594 +
28595 +}
28596 +
28597 +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
28598 +{
28599 + hfir_data_t hfir;
28600 + uint32_t fram_int;
28601 + fram_int = calc_frame_interval(core_if);
28602 + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
28603 + if (!core_if->core_params->reload_ctl) {
28604 + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
28605 + "not set to 1.\nShould load driver with reload_ctl=1"
28606 + " module parameter\n");
28607 + return;
28608 + }
28609 + switch (fram_int) {
28610 + case 3750:
28611 + if ((val < 3350) || (val > 4150)) {
28612 + DWC_WARN("HFIR interval for HS core and 30 MHz"
28613 + "clock freq should be from 3350 to 4150\n");
28614 + return;
28615 + }
28616 + break;
28617 + case 30000:
28618 + if ((val < 26820) || (val > 33180)) {
28619 + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
28620 + "clock freq should be from 26820 to 33180\n");
28621 + return;
28622 + }
28623 + break;
28624 + case 6000:
28625 + if ((val < 5360) || (val > 6640)) {
28626 + DWC_WARN("HFIR interval for HS core and 48 MHz"
28627 + "clock freq should be from 5360 to 6640\n");
28628 + return;
28629 + }
28630 + break;
28631 + case 48000:
28632 + if ((val < 42912) || (val > 53088)) {
28633 + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
28634 + "clock freq should be from 42912 to 53088\n");
28635 + return;
28636 + }
28637 + break;
28638 + case 7500:
28639 + if ((val < 6700) || (val > 8300)) {
28640 + DWC_WARN("HFIR interval for HS core and 60 MHz"
28641 + "clock freq should be from 6700 to 8300\n");
28642 + return;
28643 + }
28644 + break;
28645 + case 60000:
28646 + if ((val < 53640) || (val > 65536)) {
28647 + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
28648 + "clock freq should be from 53640 to 65536\n");
28649 + return;
28650 + }
28651 + break;
28652 + default:
28653 + DWC_WARN("Unknown frame interval\n");
28654 + return;
28655 + break;
28656 +
28657 + }
28658 + hfir.b.frint = val;
28659 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
28660 +}
28661 +
28662 +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
28663 +{
28664 + hcfg_data_t hcfg;
28665 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
28666 + return hcfg.b.modechtimen;
28667 +
28668 +}
28669 +
28670 +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
28671 +{
28672 + hcfg_data_t hcfg;
28673 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
28674 + hcfg.b.modechtimen = val;
28675 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
28676 +}
28677 +
28678 +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
28679 +{
28680 + hprt0_data_t hprt0;
28681 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28682 + hprt0.b.prtres = val;
28683 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28684 +}
28685 +
28686 +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
28687 +{
28688 + dctl_data_t dctl;
28689 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
28690 + return dctl.b.rmtwkupsig;
28691 +}
28692 +
28693 +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
28694 +{
28695 + glpmcfg_data_t lpmcfg;
28696 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28697 +
28698 + DWC_ASSERT(!
28699 + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
28700 + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
28701 + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
28702 +
28703 + return lpmcfg.b.prt_sleep_sts;
28704 +}
28705 +
28706 +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
28707 +{
28708 + glpmcfg_data_t lpmcfg;
28709 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28710 + return lpmcfg.b.rem_wkup_en;
28711 +}
28712 +
28713 +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
28714 +{
28715 + glpmcfg_data_t lpmcfg;
28716 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28717 + return lpmcfg.b.appl_resp;
28718 +}
28719 +
28720 +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
28721 +{
28722 + glpmcfg_data_t lpmcfg;
28723 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28724 + lpmcfg.b.appl_resp = val;
28725 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28726 +}
28727 +
28728 +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
28729 +{
28730 + glpmcfg_data_t lpmcfg;
28731 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28732 + return lpmcfg.b.hsic_connect;
28733 +}
28734 +
28735 +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
28736 +{
28737 + glpmcfg_data_t lpmcfg;
28738 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28739 + lpmcfg.b.hsic_connect = val;
28740 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28741 +}
28742 +
28743 +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
28744 +{
28745 + glpmcfg_data_t lpmcfg;
28746 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28747 + return lpmcfg.b.inv_sel_hsic;
28748 +
28749 +}
28750 +
28751 +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
28752 +{
28753 + glpmcfg_data_t lpmcfg;
28754 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28755 + lpmcfg.b.inv_sel_hsic = val;
28756 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28757 +}
28758 +
28759 +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
28760 +{
28761 + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28762 +}
28763 +
28764 +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
28765 +{
28766 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
28767 +}
28768 +
28769 +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
28770 +{
28771 + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28772 +}
28773 +
28774 +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
28775 +{
28776 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
28777 +}
28778 +
28779 +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
28780 +{
28781 + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
28782 +}
28783 +
28784 +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
28785 +{
28786 + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
28787 +}
28788 +
28789 +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
28790 +{
28791 + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
28792 +}
28793 +
28794 +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
28795 +{
28796 + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
28797 +}
28798 +
28799 +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
28800 +{
28801 + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
28802 +}
28803 +
28804 +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
28805 +{
28806 + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
28807 +}
28808 +
28809 +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
28810 +{
28811 + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
28812 +}
28813 +
28814 +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
28815 +{
28816 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
28817 +}
28818 +
28819 +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
28820 +{
28821 + return DWC_READ_REG32(core_if->host_if->hprt0);
28822 +
28823 +}
28824 +
28825 +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
28826 +{
28827 + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
28828 +}
28829 +
28830 +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
28831 +{
28832 + return DWC_READ_REG32(&core_if->core_global_regs->guid);
28833 +}
28834 +
28835 +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
28836 +{
28837 + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
28838 +}
28839 +
28840 +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
28841 +{
28842 + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
28843 +}
28844 +
28845 +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
28846 +{
28847 + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
28848 +}
28849 +
28850 +/**
28851 + * Start the SRP timer to detect when the SRP does not complete within
28852 + * 6 seconds.
28853 + *
28854 + * @param core_if the pointer to core_if strucure.
28855 + */
28856 +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
28857 +{
28858 + core_if->srp_timer_started = 1;
28859 + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
28860 +}
28861 +
28862 +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
28863 +{
28864 + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
28865 + gotgctl_data_t mem;
28866 + gotgctl_data_t val;
28867 +
28868 + val.d32 = DWC_READ_REG32(addr);
28869 + if (val.b.sesreq) {
28870 + DWC_ERROR("Session Request Already active!\n");
28871 + return;
28872 + }
28873 +
28874 + DWC_INFO("Session Request Initated\n"); //NOTICE
28875 + mem.d32 = DWC_READ_REG32(addr);
28876 + mem.b.sesreq = 1;
28877 + DWC_WRITE_REG32(addr, mem.d32);
28878 +
28879 + /* Start the SRP timer */
28880 + dwc_otg_pcd_start_srp_timer(core_if);
28881 + return;
28882 +}
28883 --- /dev/null
28884 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.h
28885 @@ -0,0 +1,1464 @@
28886 +/* ==========================================================================
28887 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
28888 + * $Revision: #123 $
28889 + * $Date: 2012/08/10 $
28890 + * $Change: 2047372 $
28891 + *
28892 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
28893 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
28894 + * otherwise expressly agreed to in writing between Synopsys and you.
28895 + *
28896 + * The Software IS NOT an item of Licensed Software or Licensed Product under
28897 + * any End User Software License Agreement or Agreement for Licensed Product
28898 + * with Synopsys or any supplement thereto. You are permitted to use and
28899 + * redistribute this Software in source and binary forms, with or without
28900 + * modification, provided that redistributions of source code must retain this
28901 + * notice. You may not view, use, disclose, copy or distribute this file or
28902 + * any information contained herein except pursuant to this license grant from
28903 + * Synopsys. If you do not agree with this notice, including the disclaimer
28904 + * below, then you are not authorized to use the Software.
28905 + *
28906 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
28907 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28908 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28909 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
28910 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28911 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28912 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28913 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28914 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28915 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28916 + * DAMAGE.
28917 + * ========================================================================== */
28918 +
28919 +#if !defined(__DWC_CIL_H__)
28920 +#define __DWC_CIL_H__
28921 +
28922 +#include "dwc_list.h"
28923 +#include "dwc_otg_dbg.h"
28924 +#include "dwc_otg_regs.h"
28925 +
28926 +#include "dwc_otg_core_if.h"
28927 +#include "dwc_otg_adp.h"
28928 +
28929 +/**
28930 + * @file
28931 + * This file contains the interface to the Core Interface Layer.
28932 + */
28933 +
28934 +#ifdef DWC_UTE_CFI
28935 +
28936 +#define MAX_DMA_DESCS_PER_EP 256
28937 +
28938 +/**
28939 + * Enumeration for the data buffer mode
28940 + */
28941 +typedef enum _data_buffer_mode {
28942 + BM_STANDARD = 0, /* data buffer is in normal mode */
28943 + BM_SG = 1, /* data buffer uses the scatter/gather mode */
28944 + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
28945 + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
28946 + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
28947 +} data_buffer_mode_e;
28948 +#endif //DWC_UTE_CFI
28949 +
28950 +/** Macros defined for DWC OTG HW Release version */
28951 +
28952 +#define OTG_CORE_REV_2_60a 0x4F54260A
28953 +#define OTG_CORE_REV_2_71a 0x4F54271A
28954 +#define OTG_CORE_REV_2_72a 0x4F54272A
28955 +#define OTG_CORE_REV_2_80a 0x4F54280A
28956 +#define OTG_CORE_REV_2_81a 0x4F54281A
28957 +#define OTG_CORE_REV_2_90a 0x4F54290A
28958 +#define OTG_CORE_REV_2_91a 0x4F54291A
28959 +#define OTG_CORE_REV_2_92a 0x4F54292A
28960 +#define OTG_CORE_REV_2_93a 0x4F54293A
28961 +#define OTG_CORE_REV_2_94a 0x4F54294A
28962 +#define OTG_CORE_REV_3_00a 0x4F54300A
28963 +
28964 +/**
28965 + * Information for each ISOC packet.
28966 + */
28967 +typedef struct iso_pkt_info {
28968 + uint32_t offset;
28969 + uint32_t length;
28970 + int32_t status;
28971 +} iso_pkt_info_t;
28972 +
28973 +/**
28974 + * The <code>dwc_ep</code> structure represents the state of a single
28975 + * endpoint when acting in device mode. It contains the data items
28976 + * needed for an endpoint to be activated and transfer packets.
28977 + */
28978 +typedef struct dwc_ep {
28979 + /** EP number used for register address lookup */
28980 + uint8_t num;
28981 + /** EP direction 0 = OUT */
28982 + unsigned is_in:1;
28983 + /** EP active. */
28984 + unsigned active:1;
28985 +
28986 + /**
28987 + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
28988 + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
28989 + unsigned tx_fifo_num:4;
28990 + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
28991 + unsigned type:2;
28992 +#define DWC_OTG_EP_TYPE_CONTROL 0
28993 +#define DWC_OTG_EP_TYPE_ISOC 1
28994 +#define DWC_OTG_EP_TYPE_BULK 2
28995 +#define DWC_OTG_EP_TYPE_INTR 3
28996 +
28997 + /** DATA start PID for INTR and BULK EP */
28998 + unsigned data_pid_start:1;
28999 + /** Frame (even/odd) for ISOC EP */
29000 + unsigned even_odd_frame:1;
29001 + /** Max Packet bytes */
29002 + unsigned maxpacket:11;
29003 +
29004 + /** Max Transfer size */
29005 + uint32_t maxxfer;
29006 +
29007 + /** @name Transfer state */
29008 + /** @{ */
29009 +
29010 + /**
29011 + * Pointer to the beginning of the transfer buffer -- do not modify
29012 + * during transfer.
29013 + */
29014 +
29015 + dwc_dma_t dma_addr;
29016 +
29017 + dwc_dma_t dma_desc_addr;
29018 + dwc_otg_dev_dma_desc_t *desc_addr;
29019 +
29020 + uint8_t *start_xfer_buff;
29021 + /** pointer to the transfer buffer */
29022 + uint8_t *xfer_buff;
29023 + /** Number of bytes to transfer */
29024 + unsigned xfer_len:19;
29025 + /** Number of bytes transferred. */
29026 + unsigned xfer_count:19;
29027 + /** Sent ZLP */
29028 + unsigned sent_zlp:1;
29029 + /** Total len for control transfer */
29030 + unsigned total_len:19;
29031 +
29032 + /** stall clear flag */
29033 + unsigned stall_clear_flag:1;
29034 +
29035 + /** SETUP pkt cnt rollover flag for EP0 out*/
29036 + unsigned stp_rollover;
29037 +
29038 +#ifdef DWC_UTE_CFI
29039 + /* The buffer mode */
29040 + data_buffer_mode_e buff_mode;
29041 +
29042 + /* The chain of DMA descriptors.
29043 + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
29044 + */
29045 + dwc_otg_dma_desc_t *descs;
29046 +
29047 + /* The DMA address of the descriptors chain start */
29048 + dma_addr_t descs_dma_addr;
29049 + /** This variable stores the length of the last enqueued request */
29050 + uint32_t cfi_req_len;
29051 +#endif //DWC_UTE_CFI
29052 +
29053 +/** Max DMA Descriptor count for any EP */
29054 +#define MAX_DMA_DESC_CNT 256
29055 + /** Allocated DMA Desc count */
29056 + uint32_t desc_cnt;
29057 +
29058 + /** bInterval */
29059 + uint32_t bInterval;
29060 + /** Next frame num to setup next ISOC transfer */
29061 + uint32_t frame_num;
29062 + /** Indicates SOF number overrun in DSTS */
29063 + uint8_t frm_overrun;
29064 +
29065 +#ifdef DWC_UTE_PER_IO
29066 + /** Next frame num for which will be setup DMA Desc */
29067 + uint32_t xiso_frame_num;
29068 + /** bInterval */
29069 + uint32_t xiso_bInterval;
29070 + /** Count of currently active transfers - shall be either 0 or 1 */
29071 + int xiso_active_xfers;
29072 + int xiso_queued_xfers;
29073 +#endif
29074 +#ifdef DWC_EN_ISOC
29075 + /**
29076 + * Variables specific for ISOC EPs
29077 + *
29078 + */
29079 + /** DMA addresses of ISOC buffers */
29080 + dwc_dma_t dma_addr0;
29081 + dwc_dma_t dma_addr1;
29082 +
29083 + dwc_dma_t iso_dma_desc_addr;
29084 + dwc_otg_dev_dma_desc_t *iso_desc_addr;
29085 +
29086 + /** pointer to the transfer buffers */
29087 + uint8_t *xfer_buff0;
29088 + uint8_t *xfer_buff1;
29089 +
29090 + /** number of ISOC Buffer is processing */
29091 + uint32_t proc_buf_num;
29092 + /** Interval of ISOC Buffer processing */
29093 + uint32_t buf_proc_intrvl;
29094 + /** Data size for regular frame */
29095 + uint32_t data_per_frame;
29096 +
29097 + /* todo - pattern data support is to be implemented in the future */
29098 + /** Data size for pattern frame */
29099 + uint32_t data_pattern_frame;
29100 + /** Frame number of pattern data */
29101 + uint32_t sync_frame;
29102 +
29103 + /** bInterval */
29104 + uint32_t bInterval;
29105 + /** ISO Packet number per frame */
29106 + uint32_t pkt_per_frm;
29107 + /** Next frame num for which will be setup DMA Desc */
29108 + uint32_t next_frame;
29109 + /** Number of packets per buffer processing */
29110 + uint32_t pkt_cnt;
29111 + /** Info for all isoc packets */
29112 + iso_pkt_info_t *pkt_info;
29113 + /** current pkt number */
29114 + uint32_t cur_pkt;
29115 + /** current pkt number */
29116 + uint8_t *cur_pkt_addr;
29117 + /** current pkt number */
29118 + uint32_t cur_pkt_dma_addr;
29119 +#endif /* DWC_EN_ISOC */
29120 +
29121 +/** @} */
29122 +} dwc_ep_t;
29123 +
29124 +/*
29125 + * Reasons for halting a host channel.
29126 + */
29127 +typedef enum dwc_otg_halt_status {
29128 + DWC_OTG_HC_XFER_NO_HALT_STATUS,
29129 + DWC_OTG_HC_XFER_COMPLETE,
29130 + DWC_OTG_HC_XFER_URB_COMPLETE,
29131 + DWC_OTG_HC_XFER_ACK,
29132 + DWC_OTG_HC_XFER_NAK,
29133 + DWC_OTG_HC_XFER_NYET,
29134 + DWC_OTG_HC_XFER_STALL,
29135 + DWC_OTG_HC_XFER_XACT_ERR,
29136 + DWC_OTG_HC_XFER_FRAME_OVERRUN,
29137 + DWC_OTG_HC_XFER_BABBLE_ERR,
29138 + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
29139 + DWC_OTG_HC_XFER_AHB_ERR,
29140 + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
29141 + DWC_OTG_HC_XFER_URB_DEQUEUE
29142 +} dwc_otg_halt_status_e;
29143 +
29144 +/**
29145 + * Host channel descriptor. This structure represents the state of a single
29146 + * host channel when acting in host mode. It contains the data items needed to
29147 + * transfer packets to an endpoint via a host channel.
29148 + */
29149 +typedef struct dwc_hc {
29150 + /** Host channel number used for register address lookup */
29151 + uint8_t hc_num;
29152 +
29153 + /** Device to access */
29154 + unsigned dev_addr:7;
29155 +
29156 + /** EP to access */
29157 + unsigned ep_num:4;
29158 +
29159 + /** EP direction. 0: OUT, 1: IN */
29160 + unsigned ep_is_in:1;
29161 +
29162 + /**
29163 + * EP speed.
29164 + * One of the following values:
29165 + * - DWC_OTG_EP_SPEED_LOW
29166 + * - DWC_OTG_EP_SPEED_FULL
29167 + * - DWC_OTG_EP_SPEED_HIGH
29168 + */
29169 + unsigned speed:2;
29170 +#define DWC_OTG_EP_SPEED_LOW 0
29171 +#define DWC_OTG_EP_SPEED_FULL 1
29172 +#define DWC_OTG_EP_SPEED_HIGH 2
29173 +
29174 + /**
29175 + * Endpoint type.
29176 + * One of the following values:
29177 + * - DWC_OTG_EP_TYPE_CONTROL: 0
29178 + * - DWC_OTG_EP_TYPE_ISOC: 1
29179 + * - DWC_OTG_EP_TYPE_BULK: 2
29180 + * - DWC_OTG_EP_TYPE_INTR: 3
29181 + */
29182 + unsigned ep_type:2;
29183 +
29184 + /** Max packet size in bytes */
29185 + unsigned max_packet:11;
29186 +
29187 + /**
29188 + * PID for initial transaction.
29189 + * 0: DATA0,<br>
29190 + * 1: DATA2,<br>
29191 + * 2: DATA1,<br>
29192 + * 3: MDATA (non-Control EP),
29193 + * SETUP (Control EP)
29194 + */
29195 + unsigned data_pid_start:2;
29196 +#define DWC_OTG_HC_PID_DATA0 0
29197 +#define DWC_OTG_HC_PID_DATA2 1
29198 +#define DWC_OTG_HC_PID_DATA1 2
29199 +#define DWC_OTG_HC_PID_MDATA 3
29200 +#define DWC_OTG_HC_PID_SETUP 3
29201 +
29202 + /** Number of periodic transactions per (micro)frame */
29203 + unsigned multi_count:2;
29204 +
29205 + /** @name Transfer State */
29206 + /** @{ */
29207 +
29208 + /** Pointer to the current transfer buffer position. */
29209 + uint8_t *xfer_buff;
29210 + /**
29211 + * In Buffer DMA mode this buffer will be used
29212 + * if xfer_buff is not DWORD aligned.
29213 + */
29214 + dwc_dma_t align_buff;
29215 + /** Total number of bytes to transfer. */
29216 + uint32_t xfer_len;
29217 + /** Number of bytes transferred so far. */
29218 + uint32_t xfer_count;
29219 + /** Packet count at start of transfer.*/
29220 + uint16_t start_pkt_count;
29221 +
29222 + /**
29223 + * Flag to indicate whether the transfer has been started. Set to 1 if
29224 + * it has been started, 0 otherwise.
29225 + */
29226 + uint8_t xfer_started;
29227 +
29228 + /**
29229 + * Set to 1 to indicate that a PING request should be issued on this
29230 + * channel. If 0, process normally.
29231 + */
29232 + uint8_t do_ping;
29233 +
29234 + /**
29235 + * Set to 1 to indicate that the error count for this transaction is
29236 + * non-zero. Set to 0 if the error count is 0.
29237 + */
29238 + uint8_t error_state;
29239 +
29240 + /**
29241 + * Set to 1 to indicate that this channel should be halted the next
29242 + * time a request is queued for the channel. This is necessary in
29243 + * slave mode if no request queue space is available when an attempt
29244 + * is made to halt the channel.
29245 + */
29246 + uint8_t halt_on_queue;
29247 +
29248 + /**
29249 + * Set to 1 if the host channel has been halted, but the core is not
29250 + * finished flushing queued requests. Otherwise 0.
29251 + */
29252 + uint8_t halt_pending;
29253 +
29254 + /**
29255 + * Reason for halting the host channel.
29256 + */
29257 + dwc_otg_halt_status_e halt_status;
29258 +
29259 + /*
29260 + * Split settings for the host channel
29261 + */
29262 + uint8_t do_split; /**< Enable split for the channel */
29263 + uint8_t complete_split; /**< Enable complete split */
29264 + uint8_t hub_addr; /**< Address of high speed hub */
29265 +
29266 + uint8_t port_addr; /**< Port of the low/full speed device */
29267 + /** Split transaction position
29268 + * One of the following values:
29269 + * - DWC_HCSPLIT_XACTPOS_MID
29270 + * - DWC_HCSPLIT_XACTPOS_BEGIN
29271 + * - DWC_HCSPLIT_XACTPOS_END
29272 + * - DWC_HCSPLIT_XACTPOS_ALL */
29273 + uint8_t xact_pos;
29274 +
29275 + /** Set when the host channel does a short read. */
29276 + uint8_t short_read;
29277 +
29278 + /**
29279 + * Number of requests issued for this channel since it was assigned to
29280 + * the current transfer (not counting PINGs).
29281 + */
29282 + uint8_t requests;
29283 +
29284 + /**
29285 + * Queue Head for the transfer being processed by this channel.
29286 + */
29287 + struct dwc_otg_qh *qh;
29288 +
29289 + /** @} */
29290 +
29291 + /** Entry in list of host channels. */
29292 + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
29293 +
29294 + /** @name Descriptor DMA support */
29295 + /** @{ */
29296 +
29297 + /** Number of Transfer Descriptors */
29298 + uint16_t ntd;
29299 +
29300 + /** Descriptor List DMA address */
29301 + dwc_dma_t desc_list_addr;
29302 +
29303 + /** Scheduling micro-frame bitmap. */
29304 + uint8_t schinfo;
29305 +
29306 + /** @} */
29307 +} dwc_hc_t;
29308 +
29309 +/**
29310 + * The following parameters may be specified when starting the module. These
29311 + * parameters define how the DWC_otg controller should be configured.
29312 + */
29313 +typedef struct dwc_otg_core_params {
29314 + int32_t opt;
29315 +
29316 + /**
29317 + * Specifies the OTG capabilities. The driver will automatically
29318 + * detect the value for this parameter if none is specified.
29319 + * 0 - HNP and SRP capable (default)
29320 + * 1 - SRP Only capable
29321 + * 2 - No HNP/SRP capable
29322 + */
29323 + int32_t otg_cap;
29324 +
29325 + /**
29326 + * Specifies whether to use slave or DMA mode for accessing the data
29327 + * FIFOs. The driver will automatically detect the value for this
29328 + * parameter if none is specified.
29329 + * 0 - Slave
29330 + * 1 - DMA (default, if available)
29331 + */
29332 + int32_t dma_enable;
29333 +
29334 + /**
29335 + * When DMA mode is enabled specifies whether to use address DMA or DMA
29336 + * Descriptor mode for accessing the data FIFOs in device mode. The driver
29337 + * will automatically detect the value for this if none is specified.
29338 + * 0 - address DMA
29339 + * 1 - DMA Descriptor(default, if available)
29340 + */
29341 + int32_t dma_desc_enable;
29342 + /** The DMA Burst size (applicable only for External DMA
29343 + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
29344 + */
29345 + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
29346 +
29347 + /**
29348 + * Specifies the maximum speed of operation in host and device mode.
29349 + * The actual speed depends on the speed of the attached device and
29350 + * the value of phy_type. The actual speed depends on the speed of the
29351 + * attached device.
29352 + * 0 - High Speed (default)
29353 + * 1 - Full Speed
29354 + */
29355 + int32_t speed;
29356 + /** Specifies whether low power mode is supported when attached
29357 + * to a Full Speed or Low Speed device in host mode.
29358 + * 0 - Don't support low power mode (default)
29359 + * 1 - Support low power mode
29360 + */
29361 + int32_t host_support_fs_ls_low_power;
29362 +
29363 + /** Specifies the PHY clock rate in low power mode when connected to a
29364 + * Low Speed device in host mode. This parameter is applicable only if
29365 + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
29366 + * then defaults to 6 MHZ otherwise 48 MHZ.
29367 + *
29368 + * 0 - 48 MHz
29369 + * 1 - 6 MHz
29370 + */
29371 + int32_t host_ls_low_power_phy_clk;
29372 +
29373 + /**
29374 + * 0 - Use cC FIFO size parameters
29375 + * 1 - Allow dynamic FIFO sizing (default)
29376 + */
29377 + int32_t enable_dynamic_fifo;
29378 +
29379 + /** Total number of 4-byte words in the data FIFO memory. This
29380 + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
29381 + * Tx FIFOs.
29382 + * 32 to 32768 (default 8192)
29383 + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
29384 + */
29385 + int32_t data_fifo_size;
29386 +
29387 + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
29388 + * FIFO sizing is enabled.
29389 + * 16 to 32768 (default 1064)
29390 + */
29391 + int32_t dev_rx_fifo_size;
29392 +
29393 + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
29394 + * when dynamic FIFO sizing is enabled.
29395 + * 16 to 32768 (default 1024)
29396 + */
29397 + int32_t dev_nperio_tx_fifo_size;
29398 +
29399 + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
29400 + * mode when dynamic FIFO sizing is enabled.
29401 + * 4 to 768 (default 256)
29402 + */
29403 + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
29404 +
29405 + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
29406 + * FIFO sizing is enabled.
29407 + * 16 to 32768 (default 1024)
29408 + */
29409 + int32_t host_rx_fifo_size;
29410 +
29411 + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
29412 + * when Dynamic FIFO sizing is enabled in the core.
29413 + * 16 to 32768 (default 1024)
29414 + */
29415 + int32_t host_nperio_tx_fifo_size;
29416 +
29417 + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
29418 + * FIFO sizing is enabled.
29419 + * 16 to 32768 (default 1024)
29420 + */
29421 + int32_t host_perio_tx_fifo_size;
29422 +
29423 + /** The maximum transfer size supported in bytes.
29424 + * 2047 to 65,535 (default 65,535)
29425 + */
29426 + int32_t max_transfer_size;
29427 +
29428 + /** The maximum number of packets in a transfer.
29429 + * 15 to 511 (default 511)
29430 + */
29431 + int32_t max_packet_count;
29432 +
29433 + /** The number of host channel registers to use.
29434 + * 1 to 16 (default 12)
29435 + * Note: The FPGA configuration supports a maximum of 12 host channels.
29436 + */
29437 + int32_t host_channels;
29438 +
29439 + /** The number of endpoints in addition to EP0 available for device
29440 + * mode operations.
29441 + * 1 to 15 (default 6 IN and OUT)
29442 + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
29443 + * endpoints in addition to EP0.
29444 + */
29445 + int32_t dev_endpoints;
29446 +
29447 + /**
29448 + * Specifies the type of PHY interface to use. By default, the driver
29449 + * will automatically detect the phy_type.
29450 + *
29451 + * 0 - Full Speed PHY
29452 + * 1 - UTMI+ (default)
29453 + * 2 - ULPI
29454 + */
29455 + int32_t phy_type;
29456 +
29457 + /**
29458 + * Specifies the UTMI+ Data Width. This parameter is
29459 + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
29460 + * PHY_TYPE, this parameter indicates the data width between
29461 + * the MAC and the ULPI Wrapper.) Also, this parameter is
29462 + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
29463 + * to "8 and 16 bits", meaning that the core has been
29464 + * configured to work at either data path width.
29465 + *
29466 + * 8 or 16 bits (default 16)
29467 + */
29468 + int32_t phy_utmi_width;
29469 +
29470 + /**
29471 + * Specifies whether the ULPI operates at double or single
29472 + * data rate. This parameter is only applicable if PHY_TYPE is
29473 + * ULPI.
29474 + *
29475 + * 0 - single data rate ULPI interface with 8 bit wide data
29476 + * bus (default)
29477 + * 1 - double data rate ULPI interface with 4 bit wide data
29478 + * bus
29479 + */
29480 + int32_t phy_ulpi_ddr;
29481 +
29482 + /**
29483 + * Specifies whether to use the internal or external supply to
29484 + * drive the vbus with a ULPI phy.
29485 + */
29486 + int32_t phy_ulpi_ext_vbus;
29487 +
29488 + /**
29489 + * Specifies whether to use the I2Cinterface for full speed PHY. This
29490 + * parameter is only applicable if PHY_TYPE is FS.
29491 + * 0 - No (default)
29492 + * 1 - Yes
29493 + */
29494 + int32_t i2c_enable;
29495 +
29496 + int32_t ulpi_fs_ls;
29497 +
29498 + int32_t ts_dline;
29499 +
29500 + /**
29501 + * Specifies whether dedicated transmit FIFOs are
29502 + * enabled for non periodic IN endpoints in device mode
29503 + * 0 - No
29504 + * 1 - Yes
29505 + */
29506 + int32_t en_multiple_tx_fifo;
29507 +
29508 + /** Number of 4-byte words in each of the Tx FIFOs in device
29509 + * mode when dynamic FIFO sizing is enabled.
29510 + * 4 to 768 (default 256)
29511 + */
29512 + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
29513 +
29514 + /** Thresholding enable flag-
29515 + * bit 0 - enable non-ISO Tx thresholding
29516 + * bit 1 - enable ISO Tx thresholding
29517 + * bit 2 - enable Rx thresholding
29518 + */
29519 + uint32_t thr_ctl;
29520 +
29521 + /** Thresholding length for Tx
29522 + * FIFOs in 32 bit DWORDs
29523 + */
29524 + uint32_t tx_thr_length;
29525 +
29526 + /** Thresholding length for Rx
29527 + * FIFOs in 32 bit DWORDs
29528 + */
29529 + uint32_t rx_thr_length;
29530 +
29531 + /**
29532 + * Specifies whether LPM (Link Power Management) support is enabled
29533 + */
29534 + int32_t lpm_enable;
29535 +
29536 + /** Per Transfer Interrupt
29537 + * mode enable flag
29538 + * 1 - Enabled
29539 + * 0 - Disabled
29540 + */
29541 + int32_t pti_enable;
29542 +
29543 + /** Multi Processor Interrupt
29544 + * mode enable flag
29545 + * 1 - Enabled
29546 + * 0 - Disabled
29547 + */
29548 + int32_t mpi_enable;
29549 +
29550 + /** IS_USB Capability
29551 + * 1 - Enabled
29552 + * 0 - Disabled
29553 + */
29554 + int32_t ic_usb_cap;
29555 +
29556 + /** AHB Threshold Ratio
29557 + * 2'b00 AHB Threshold = MAC Threshold
29558 + * 2'b01 AHB Threshold = 1/2 MAC Threshold
29559 + * 2'b10 AHB Threshold = 1/4 MAC Threshold
29560 + * 2'b11 AHB Threshold = 1/8 MAC Threshold
29561 + */
29562 + int32_t ahb_thr_ratio;
29563 +
29564 + /** ADP Support
29565 + * 1 - Enabled
29566 + * 0 - Disabled
29567 + */
29568 + int32_t adp_supp_enable;
29569 +
29570 + /** HFIR Reload Control
29571 + * 0 - The HFIR cannot be reloaded dynamically.
29572 + * 1 - Allow dynamic reloading of the HFIR register during runtime.
29573 + */
29574 + int32_t reload_ctl;
29575 +
29576 + /** DCFG: Enable device Out NAK
29577 + * 0 - The core does not set NAK after Bulk Out transfer complete.
29578 + * 1 - The core sets NAK after Bulk OUT transfer complete.
29579 + */
29580 + int32_t dev_out_nak;
29581 +
29582 + /** DCFG: Enable Continue on BNA
29583 + * After receiving BNA interrupt the core disables the endpoint,when the
29584 + * endpoint is re-enabled by the application the core starts processing
29585 + * 0 - from the DOEPDMA descriptor
29586 + * 1 - from the descriptor which received the BNA.
29587 + */
29588 + int32_t cont_on_bna;
29589 +
29590 + /** GAHBCFG: AHB Single Support
29591 + * This bit when programmed supports SINGLE transfers for remainder
29592 + * data in a transfer for DMA mode of operation.
29593 + * 0 - in this case the remainder data will be sent using INCR burst size.
29594 + * 1 - in this case the remainder data will be sent using SINGLE burst size.
29595 + */
29596 + int32_t ahb_single;
29597 +
29598 + /** Core Power down mode
29599 + * 0 - No Power Down is enabled
29600 + * 1 - Reserved
29601 + * 2 - Complete Power Down (Hibernation)
29602 + */
29603 + int32_t power_down;
29604 +
29605 + /** OTG revision supported
29606 + * 0 - OTG 1.3 revision
29607 + * 1 - OTG 2.0 revision
29608 + */
29609 + int32_t otg_ver;
29610 +
29611 +} dwc_otg_core_params_t;
29612 +
29613 +#ifdef DEBUG
29614 +struct dwc_otg_core_if;
29615 +typedef struct hc_xfer_info {
29616 + struct dwc_otg_core_if *core_if;
29617 + dwc_hc_t *hc;
29618 +} hc_xfer_info_t;
29619 +#endif
29620 +
29621 +typedef struct ep_xfer_info {
29622 + struct dwc_otg_core_if *core_if;
29623 + dwc_ep_t *ep;
29624 + uint8_t state;
29625 +} ep_xfer_info_t;
29626 +/*
29627 + * Device States
29628 + */
29629 +typedef enum dwc_otg_lx_state {
29630 + /** On state */
29631 + DWC_OTG_L0,
29632 + /** LPM sleep state*/
29633 + DWC_OTG_L1,
29634 + /** USB suspend state*/
29635 + DWC_OTG_L2,
29636 + /** Off state*/
29637 + DWC_OTG_L3
29638 +} dwc_otg_lx_state_e;
29639 +
29640 +struct dwc_otg_global_regs_backup {
29641 + uint32_t gotgctl_local;
29642 + uint32_t gintmsk_local;
29643 + uint32_t gahbcfg_local;
29644 + uint32_t gusbcfg_local;
29645 + uint32_t grxfsiz_local;
29646 + uint32_t gnptxfsiz_local;
29647 +#ifdef CONFIG_USB_DWC_OTG_LPM
29648 + uint32_t glpmcfg_local;
29649 +#endif
29650 + uint32_t gi2cctl_local;
29651 + uint32_t hptxfsiz_local;
29652 + uint32_t pcgcctl_local;
29653 + uint32_t gdfifocfg_local;
29654 + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
29655 + uint32_t gpwrdn_local;
29656 + uint32_t xhib_pcgcctl;
29657 + uint32_t xhib_gpwrdn;
29658 +};
29659 +
29660 +struct dwc_otg_host_regs_backup {
29661 + uint32_t hcfg_local;
29662 + uint32_t haintmsk_local;
29663 + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
29664 + uint32_t hprt0_local;
29665 + uint32_t hfir_local;
29666 +};
29667 +
29668 +struct dwc_otg_dev_regs_backup {
29669 + uint32_t dcfg;
29670 + uint32_t dctl;
29671 + uint32_t daintmsk;
29672 + uint32_t diepmsk;
29673 + uint32_t doepmsk;
29674 + uint32_t diepctl[MAX_EPS_CHANNELS];
29675 + uint32_t dieptsiz[MAX_EPS_CHANNELS];
29676 + uint32_t diepdma[MAX_EPS_CHANNELS];
29677 +};
29678 +/**
29679 + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
29680 + * the DWC_otg controller acting in either host or device mode. It
29681 + * represents the programming view of the controller as a whole.
29682 + */
29683 +struct dwc_otg_core_if {
29684 + /** Parameters that define how the core should be configured.*/
29685 + dwc_otg_core_params_t *core_params;
29686 +
29687 + /** Core Global registers starting at offset 000h. */
29688 + dwc_otg_core_global_regs_t *core_global_regs;
29689 +
29690 + /** Device-specific information */
29691 + dwc_otg_dev_if_t *dev_if;
29692 + /** Host-specific information */
29693 + dwc_otg_host_if_t *host_if;
29694 +
29695 + /** Value from SNPSID register */
29696 + uint32_t snpsid;
29697 +
29698 + /*
29699 + * Set to 1 if the core PHY interface bits in USBCFG have been
29700 + * initialized.
29701 + */
29702 + uint8_t phy_init_done;
29703 +
29704 + /*
29705 + * SRP Success flag, set by srp success interrupt in FS I2C mode
29706 + */
29707 + uint8_t srp_success;
29708 + uint8_t srp_timer_started;
29709 + /** Timer for SRP. If it expires before SRP is successful
29710 + * clear the SRP. */
29711 + dwc_timer_t *srp_timer;
29712 +
29713 +#ifdef DWC_DEV_SRPCAP
29714 + /* This timer is needed to power on the hibernated host core if SRP is not
29715 + * initiated on connected SRP capable device for limited period of time
29716 + */
29717 + uint8_t pwron_timer_started;
29718 + dwc_timer_t *pwron_timer;
29719 +#endif
29720 + /* Common configuration information */
29721 + /** Power and Clock Gating Control Register */
29722 + volatile uint32_t *pcgcctl;
29723 +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
29724 +
29725 + /** Push/pop addresses for endpoints or host channels.*/
29726 + uint32_t *data_fifo[MAX_EPS_CHANNELS];
29727 +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
29728 +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
29729 +
29730 + /** Total RAM for FIFOs (Bytes) */
29731 + uint16_t total_fifo_size;
29732 + /** Size of Rx FIFO (Bytes) */
29733 + uint16_t rx_fifo_size;
29734 + /** Size of Non-periodic Tx FIFO (Bytes) */
29735 + uint16_t nperio_tx_fifo_size;
29736 +
29737 + /** 1 if DMA is enabled, 0 otherwise. */
29738 + uint8_t dma_enable;
29739 +
29740 + /** 1 if DMA descriptor is enabled, 0 otherwise. */
29741 + uint8_t dma_desc_enable;
29742 +
29743 + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
29744 + uint8_t pti_enh_enable;
29745 +
29746 + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
29747 + uint8_t multiproc_int_enable;
29748 +
29749 + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
29750 + uint8_t en_multiple_tx_fifo;
29751 +
29752 + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
29753 + * process of being queued */
29754 + uint8_t queuing_high_bandwidth;
29755 +
29756 + /** Hardware Configuration -- stored here for convenience.*/
29757 + hwcfg1_data_t hwcfg1;
29758 + hwcfg2_data_t hwcfg2;
29759 + hwcfg3_data_t hwcfg3;
29760 + hwcfg4_data_t hwcfg4;
29761 + fifosize_data_t hptxfsiz;
29762 +
29763 + /** Host and Device Configuration -- stored here for convenience.*/
29764 + hcfg_data_t hcfg;
29765 + dcfg_data_t dcfg;
29766 +
29767 + /** The operational State, during transations
29768 + * (a_host>>a_peripherial and b_device=>b_host) this may not
29769 + * match the core but allows the software to determine
29770 + * transitions.
29771 + */
29772 + uint8_t op_state;
29773 +
29774 + /**
29775 + * Set to 1 if the HCD needs to be restarted on a session request
29776 + * interrupt. This is required if no connector ID status change has
29777 + * occurred since the HCD was last disconnected.
29778 + */
29779 + uint8_t restart_hcd_on_session_req;
29780 +
29781 + /** HCD callbacks */
29782 + /** A-Device is a_host */
29783 +#define A_HOST (1)
29784 + /** A-Device is a_suspend */
29785 +#define A_SUSPEND (2)
29786 + /** A-Device is a_peripherial */
29787 +#define A_PERIPHERAL (3)
29788 + /** B-Device is operating as a Peripheral. */
29789 +#define B_PERIPHERAL (4)
29790 + /** B-Device is operating as a Host. */
29791 +#define B_HOST (5)
29792 +
29793 + /** HCD callbacks */
29794 + struct dwc_otg_cil_callbacks *hcd_cb;
29795 + /** PCD callbacks */
29796 + struct dwc_otg_cil_callbacks *pcd_cb;
29797 +
29798 + /** Device mode Periodic Tx FIFO Mask */
29799 + uint32_t p_tx_msk;
29800 + /** Device mode Periodic Tx FIFO Mask */
29801 + uint32_t tx_msk;
29802 +
29803 + /** Workqueue object used for handling several interrupts */
29804 + dwc_workq_t *wq_otg;
29805 +
29806 + /** Timer object used for handling "Wakeup Detected" Interrupt */
29807 + dwc_timer_t *wkp_timer;
29808 + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
29809 + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
29810 + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
29811 + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
29812 +#ifdef DEBUG
29813 + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
29814 +
29815 + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
29816 + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
29817 +
29818 + uint32_t hfnum_7_samples;
29819 + uint64_t hfnum_7_frrem_accum;
29820 + uint32_t hfnum_0_samples;
29821 + uint64_t hfnum_0_frrem_accum;
29822 + uint32_t hfnum_other_samples;
29823 + uint64_t hfnum_other_frrem_accum;
29824 +#endif
29825 +
29826 +#ifdef DWC_UTE_CFI
29827 + uint16_t pwron_rxfsiz;
29828 + uint16_t pwron_gnptxfsiz;
29829 + uint16_t pwron_txfsiz[15];
29830 +
29831 + uint16_t init_rxfsiz;
29832 + uint16_t init_gnptxfsiz;
29833 + uint16_t init_txfsiz[15];
29834 +#endif
29835 +
29836 + /** Lx state of device */
29837 + dwc_otg_lx_state_e lx_state;
29838 +
29839 + /** Saved Core Global registers */
29840 + struct dwc_otg_global_regs_backup *gr_backup;
29841 + /** Saved Host registers */
29842 + struct dwc_otg_host_regs_backup *hr_backup;
29843 + /** Saved Device registers */
29844 + struct dwc_otg_dev_regs_backup *dr_backup;
29845 +
29846 + /** Power Down Enable */
29847 + uint32_t power_down;
29848 +
29849 + /** ADP support Enable */
29850 + uint32_t adp_enable;
29851 +
29852 + /** ADP structure object */
29853 + dwc_otg_adp_t adp;
29854 +
29855 + /** hibernation/suspend flag */
29856 + int hibernation_suspend;
29857 +
29858 + /** Device mode extended hibernation flag */
29859 + int xhib;
29860 +
29861 + /** OTG revision supported */
29862 + uint32_t otg_ver;
29863 +
29864 + /** OTG status flag used for HNP polling */
29865 + uint8_t otg_sts;
29866 +
29867 + /** Pointer to either hcd->lock or pcd->lock */
29868 + dwc_spinlock_t *lock;
29869 +
29870 + /** Start predict NextEP based on Learning Queue if equal 1,
29871 + * also used as counter of disabled NP IN EP's */
29872 + uint8_t start_predict;
29873 +
29874 + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
29875 + * active, 0xff otherwise */
29876 + uint8_t nextep_seq[MAX_EPS_CHANNELS];
29877 +
29878 + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
29879 + uint8_t first_in_nextep_seq;
29880 +
29881 + /** Frame number while entering to ISR - needed for ISOCs **/
29882 + uint32_t frame_num;
29883 +
29884 +};
29885 +
29886 +#ifdef DEBUG
29887 +/*
29888 + * This function is called when transfer is timed out.
29889 + */
29890 +extern void hc_xfer_timeout(void *ptr);
29891 +#endif
29892 +
29893 +/*
29894 + * This function is called when transfer is timed out on endpoint.
29895 + */
29896 +extern void ep_xfer_timeout(void *ptr);
29897 +
29898 +/*
29899 + * The following functions are functions for works
29900 + * using during handling some interrupts
29901 + */
29902 +extern void w_conn_id_status_change(void *p);
29903 +
29904 +extern void w_wakeup_detected(void *p);
29905 +
29906 +/** Saves global register values into system memory. */
29907 +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
29908 +/** Saves device register values into system memory. */
29909 +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
29910 +/** Saves host register values into system memory. */
29911 +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
29912 +/** Restore global register values. */
29913 +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
29914 +/** Restore host register values. */
29915 +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
29916 +/** Restore device register values. */
29917 +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
29918 + int rem_wakeup);
29919 +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
29920 +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
29921 + int is_host);
29922 +
29923 +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
29924 + int restore_mode, int reset);
29925 +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
29926 + int rem_wakeup, int reset);
29927 +
29928 +/*
29929 + * The following functions support initialization of the CIL driver component
29930 + * and the DWC_otg controller.
29931 + */
29932 +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
29933 +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
29934 +
29935 +/** @name Device CIL Functions
29936 + * The following functions support managing the DWC_otg controller in device
29937 + * mode.
29938 + */
29939 +/**@{*/
29940 +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
29941 +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
29942 + uint32_t * _dest);
29943 +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
29944 +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29945 +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29946 +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29947 +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
29948 + dwc_ep_t * _ep);
29949 +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
29950 + dwc_ep_t * _ep);
29951 +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
29952 + dwc_ep_t * _ep);
29953 +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
29954 + dwc_ep_t * _ep);
29955 +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
29956 + dwc_ep_t * _ep, int _dma);
29957 +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29958 +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
29959 + dwc_ep_t * _ep);
29960 +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
29961 +
29962 +#ifdef DWC_EN_ISOC
29963 +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
29964 + dwc_ep_t * ep);
29965 +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
29966 + dwc_ep_t * ep);
29967 +#endif /* DWC_EN_ISOC */
29968 +/**@}*/
29969 +
29970 +/** @name Host CIL Functions
29971 + * The following functions support managing the DWC_otg controller in host
29972 + * mode.
29973 + */
29974 +/**@{*/
29975 +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29976 +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
29977 + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
29978 +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29979 +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
29980 + dwc_hc_t * _hc);
29981 +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
29982 + dwc_hc_t * _hc);
29983 +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29984 +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
29985 + dwc_hc_t * _hc);
29986 +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
29987 +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
29988 +
29989 +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
29990 + dwc_hc_t * hc);
29991 +
29992 +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
29993 +
29994 +/* Macro used to clear one channel interrupt */
29995 +#define clear_hc_int(_hc_regs_, _intr_) \
29996 +do { \
29997 + hcint_data_t hcint_clear = {.d32 = 0}; \
29998 + hcint_clear.b._intr_ = 1; \
29999 + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
30000 +} while (0)
30001 +
30002 +/*
30003 + * Macro used to disable one channel interrupt. Channel interrupts are
30004 + * disabled when the channel is halted or released by the interrupt handler.
30005 + * There is no need to handle further interrupts of that type until the
30006 + * channel is re-assigned. In fact, subsequent handling may cause crashes
30007 + * because the channel structures are cleaned up when the channel is released.
30008 + */
30009 +#define disable_hc_int(_hc_regs_, _intr_) \
30010 +do { \
30011 + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
30012 + hcintmsk.b._intr_ = 1; \
30013 + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
30014 +} while (0)
30015 +
30016 +/**
30017 + * This function Reads HPRT0 in preparation to modify. It keeps the
30018 + * WC bits 0 so that if they are read as 1, they won't clear when you
30019 + * write it back
30020 + */
30021 +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
30022 +{
30023 + hprt0_data_t hprt0;
30024 + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
30025 + hprt0.b.prtena = 0;
30026 + hprt0.b.prtconndet = 0;
30027 + hprt0.b.prtenchng = 0;
30028 + hprt0.b.prtovrcurrchng = 0;
30029 + return hprt0.d32;
30030 +}
30031 +
30032 +/**@}*/
30033 +
30034 +/** @name Common CIL Functions
30035 + * The following functions support managing the DWC_otg controller in either
30036 + * device or host mode.
30037 + */
30038 +/**@{*/
30039 +
30040 +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
30041 + uint8_t * dest, uint16_t bytes);
30042 +
30043 +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
30044 +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
30045 +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
30046 +
30047 +/**
30048 + * This function returns the Core Interrupt register.
30049 + */
30050 +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
30051 +{
30052 + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
30053 + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
30054 +}
30055 +
30056 +/**
30057 + * This function returns the OTG Interrupt register.
30058 + */
30059 +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
30060 +{
30061 + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
30062 +}
30063 +
30064 +/**
30065 + * This function reads the Device All Endpoints Interrupt register and
30066 + * returns the IN endpoint interrupt bits.
30067 + */
30068 +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
30069 + core_if)
30070 +{
30071 +
30072 + uint32_t v;
30073 +
30074 + if (core_if->multiproc_int_enable) {
30075 + v = DWC_READ_REG32(&core_if->dev_if->
30076 + dev_global_regs->deachint) &
30077 + DWC_READ_REG32(&core_if->
30078 + dev_if->dev_global_regs->deachintmsk);
30079 + } else {
30080 + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
30081 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
30082 + }
30083 + return (v & 0xffff);
30084 +}
30085 +
30086 +/**
30087 + * This function reads the Device All Endpoints Interrupt register and
30088 + * returns the OUT endpoint interrupt bits.
30089 + */
30090 +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
30091 + core_if)
30092 +{
30093 + uint32_t v;
30094 +
30095 + if (core_if->multiproc_int_enable) {
30096 + v = DWC_READ_REG32(&core_if->dev_if->
30097 + dev_global_regs->deachint) &
30098 + DWC_READ_REG32(&core_if->
30099 + dev_if->dev_global_regs->deachintmsk);
30100 + } else {
30101 + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
30102 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
30103 + }
30104 +
30105 + return ((v & 0xffff0000) >> 16);
30106 +}
30107 +
30108 +/**
30109 + * This function returns the Device IN EP Interrupt register
30110 + */
30111 +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
30112 + dwc_ep_t * ep)
30113 +{
30114 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
30115 + uint32_t v, msk, emp;
30116 +
30117 + if (core_if->multiproc_int_enable) {
30118 + msk =
30119 + DWC_READ_REG32(&dev_if->
30120 + dev_global_regs->diepeachintmsk[ep->num]);
30121 + emp =
30122 + DWC_READ_REG32(&dev_if->
30123 + dev_global_regs->dtknqr4_fifoemptymsk);
30124 + msk |= ((emp >> ep->num) & 0x1) << 7;
30125 + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
30126 + } else {
30127 + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
30128 + emp =
30129 + DWC_READ_REG32(&dev_if->
30130 + dev_global_regs->dtknqr4_fifoemptymsk);
30131 + msk |= ((emp >> ep->num) & 0x1) << 7;
30132 + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
30133 + }
30134 +
30135 + return v;
30136 +}
30137 +
30138 +/**
30139 + * This function returns the Device OUT EP Interrupt register
30140 + */
30141 +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
30142 + _core_if, dwc_ep_t * _ep)
30143 +{
30144 + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
30145 + uint32_t v;
30146 + doepmsk_data_t msk = {.d32 = 0 };
30147 +
30148 + if (_core_if->multiproc_int_enable) {
30149 + msk.d32 =
30150 + DWC_READ_REG32(&dev_if->
30151 + dev_global_regs->doepeachintmsk[_ep->num]);
30152 + if (_core_if->pti_enh_enable) {
30153 + msk.b.pktdrpsts = 1;
30154 + }
30155 + v = DWC_READ_REG32(&dev_if->
30156 + out_ep_regs[_ep->num]->doepint) & msk.d32;
30157 + } else {
30158 + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
30159 + if (_core_if->pti_enh_enable) {
30160 + msk.b.pktdrpsts = 1;
30161 + }
30162 + v = DWC_READ_REG32(&dev_if->
30163 + out_ep_regs[_ep->num]->doepint) & msk.d32;
30164 + }
30165 + return v;
30166 +}
30167 +
30168 +/**
30169 + * This function returns the Host All Channel Interrupt register
30170 + */
30171 +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
30172 + _core_if)
30173 +{
30174 + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
30175 +}
30176 +
30177 +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
30178 + _core_if, dwc_hc_t * _hc)
30179 +{
30180 + return (DWC_READ_REG32
30181 + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
30182 +}
30183 +
30184 +/**
30185 + * This function returns the mode of the operation, host or device.
30186 + *
30187 + * @return 0 - Device Mode, 1 - Host Mode
30188 + */
30189 +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
30190 +{
30191 + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
30192 +}
30193 +
30194 +/**@}*/
30195 +
30196 +/**
30197 + * DWC_otg CIL callback structure. This structure allows the HCD and
30198 + * PCD to register functions used for starting and stopping the PCD
30199 + * and HCD for role change on for a DRD.
30200 + */
30201 +typedef struct dwc_otg_cil_callbacks {
30202 + /** Start function for role change */
30203 + int (*start) (void *_p);
30204 + /** Stop Function for role change */
30205 + int (*stop) (void *_p);
30206 + /** Disconnect Function for role change */
30207 + int (*disconnect) (void *_p);
30208 + /** Resume/Remote wakeup Function */
30209 + int (*resume_wakeup) (void *_p);
30210 + /** Suspend function */
30211 + int (*suspend) (void *_p);
30212 + /** Session Start (SRP) */
30213 + int (*session_start) (void *_p);
30214 +#ifdef CONFIG_USB_DWC_OTG_LPM
30215 + /** Sleep (switch to L0 state) */
30216 + int (*sleep) (void *_p);
30217 +#endif
30218 + /** Pointer passed to start() and stop() */
30219 + void *p;
30220 +} dwc_otg_cil_callbacks_t;
30221 +
30222 +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
30223 + dwc_otg_cil_callbacks_t * _cb,
30224 + void *_p);
30225 +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
30226 + dwc_otg_cil_callbacks_t * _cb,
30227 + void *_p);
30228 +
30229 +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
30230 +
30231 +//////////////////////////////////////////////////////////////////////
30232 +/** Start the HCD. Helper function for using the HCD callbacks.
30233 + *
30234 + * @param core_if Programming view of DWC_otg controller.
30235 + */
30236 +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
30237 +{
30238 + if (core_if->hcd_cb && core_if->hcd_cb->start) {
30239 + core_if->hcd_cb->start(core_if->hcd_cb->p);
30240 + }
30241 +}
30242 +
30243 +/** Stop the HCD. Helper function for using the HCD callbacks.
30244 + *
30245 + * @param core_if Programming view of DWC_otg controller.
30246 + */
30247 +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
30248 +{
30249 + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
30250 + core_if->hcd_cb->stop(core_if->hcd_cb->p);
30251 + }
30252 +}
30253 +
30254 +/** Disconnect the HCD. Helper function for using the HCD callbacks.
30255 + *
30256 + * @param core_if Programming view of DWC_otg controller.
30257 + */
30258 +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
30259 +{
30260 + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
30261 + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
30262 + }
30263 +}
30264 +
30265 +/** Inform the HCD the a New Session has begun. Helper function for
30266 + * using the HCD callbacks.
30267 + *
30268 + * @param core_if Programming view of DWC_otg controller.
30269 + */
30270 +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
30271 +{
30272 + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
30273 + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
30274 + }
30275 +}
30276 +
30277 +#ifdef CONFIG_USB_DWC_OTG_LPM
30278 +/**
30279 + * Inform the HCD about LPM sleep.
30280 + * Helper function for using the HCD callbacks.
30281 + *
30282 + * @param core_if Programming view of DWC_otg controller.
30283 + */
30284 +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
30285 +{
30286 + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
30287 + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
30288 + }
30289 +}
30290 +#endif
30291 +
30292 +/** Resume the HCD. Helper function for using the HCD callbacks.
30293 + *
30294 + * @param core_if Programming view of DWC_otg controller.
30295 + */
30296 +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
30297 +{
30298 + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
30299 + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
30300 + }
30301 +}
30302 +
30303 +/** Start the PCD. Helper function for using the PCD callbacks.
30304 + *
30305 + * @param core_if Programming view of DWC_otg controller.
30306 + */
30307 +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
30308 +{
30309 + if (core_if->pcd_cb && core_if->pcd_cb->start) {
30310 + core_if->pcd_cb->start(core_if->pcd_cb->p);
30311 + }
30312 +}
30313 +
30314 +/** Stop the PCD. Helper function for using the PCD callbacks.
30315 + *
30316 + * @param core_if Programming view of DWC_otg controller.
30317 + */
30318 +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
30319 +{
30320 + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
30321 + core_if->pcd_cb->stop(core_if->pcd_cb->p);
30322 + }
30323 +}
30324 +
30325 +/** Suspend the PCD. Helper function for using the PCD callbacks.
30326 + *
30327 + * @param core_if Programming view of DWC_otg controller.
30328 + */
30329 +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
30330 +{
30331 + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
30332 + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
30333 + }
30334 +}
30335 +
30336 +/** Resume the PCD. Helper function for using the PCD callbacks.
30337 + *
30338 + * @param core_if Programming view of DWC_otg controller.
30339 + */
30340 +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
30341 +{
30342 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
30343 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
30344 + }
30345 +}
30346 +
30347 +//////////////////////////////////////////////////////////////////////
30348 +
30349 +#endif
30350 --- /dev/null
30351 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
30352 @@ -0,0 +1,1596 @@
30353 +/* ==========================================================================
30354 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
30355 + * $Revision: #32 $
30356 + * $Date: 2012/08/10 $
30357 + * $Change: 2047372 $
30358 + *
30359 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
30360 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
30361 + * otherwise expressly agreed to in writing between Synopsys and you.
30362 + *
30363 + * The Software IS NOT an item of Licensed Software or Licensed Product under
30364 + * any End User Software License Agreement or Agreement for Licensed Product
30365 + * with Synopsys or any supplement thereto. You are permitted to use and
30366 + * redistribute this Software in source and binary forms, with or without
30367 + * modification, provided that redistributions of source code must retain this
30368 + * notice. You may not view, use, disclose, copy or distribute this file or
30369 + * any information contained herein except pursuant to this license grant from
30370 + * Synopsys. If you do not agree with this notice, including the disclaimer
30371 + * below, then you are not authorized to use the Software.
30372 + *
30373 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
30374 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30375 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30376 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
30377 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30378 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30379 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30380 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30381 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30382 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30383 + * DAMAGE.
30384 + * ========================================================================== */
30385 +
30386 +/** @file
30387 + *
30388 + * The Core Interface Layer provides basic services for accessing and
30389 + * managing the DWC_otg hardware. These services are used by both the
30390 + * Host Controller Driver and the Peripheral Controller Driver.
30391 + *
30392 + * This file contains the Common Interrupt handlers.
30393 + */
30394 +#include "dwc_os.h"
30395 +#include "dwc_otg_regs.h"
30396 +#include "dwc_otg_cil.h"
30397 +#include "dwc_otg_driver.h"
30398 +#include "dwc_otg_pcd.h"
30399 +#include "dwc_otg_hcd.h"
30400 +
30401 +#ifdef DEBUG
30402 +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
30403 +{
30404 + return (core_if->op_state == A_HOST ? "a_host" :
30405 + (core_if->op_state == A_SUSPEND ? "a_suspend" :
30406 + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
30407 + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
30408 + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
30409 +}
30410 +#endif
30411 +
30412 +/** This function will log a debug message
30413 + *
30414 + * @param core_if Programming view of DWC_otg controller.
30415 + */
30416 +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
30417 +{
30418 + gintsts_data_t gintsts;
30419 + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
30420 + dwc_otg_mode(core_if) ? "Host" : "Device");
30421 +
30422 + /* Clear interrupt */
30423 + gintsts.d32 = 0;
30424 + gintsts.b.modemismatch = 1;
30425 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30426 + return 1;
30427 +}
30428 +
30429 +/**
30430 + * This function handles the OTG Interrupts. It reads the OTG
30431 + * Interrupt Register (GOTGINT) to determine what interrupt has
30432 + * occurred.
30433 + *
30434 + * @param core_if Programming view of DWC_otg controller.
30435 + */
30436 +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
30437 +{
30438 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
30439 + gotgint_data_t gotgint;
30440 + gotgctl_data_t gotgctl;
30441 + gintmsk_data_t gintmsk;
30442 + gpwrdn_data_t gpwrdn;
30443 +
30444 + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
30445 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30446 + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
30447 + op_state_str(core_if));
30448 +
30449 + if (gotgint.b.sesenddet) {
30450 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30451 + "Session End Detected++ (%s)\n",
30452 + op_state_str(core_if));
30453 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30454 +
30455 + if (core_if->op_state == B_HOST) {
30456 + cil_pcd_start(core_if);
30457 + core_if->op_state = B_PERIPHERAL;
30458 + } else {
30459 + /* If not B_HOST and Device HNP still set. HNP
30460 + * Did not succeed!*/
30461 + if (gotgctl.b.devhnpen) {
30462 + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
30463 + __DWC_ERROR("Device Not Connected/Responding!\n");
30464 + }
30465 +
30466 + /* If Session End Detected the B-Cable has
30467 + * been disconnected. */
30468 + /* Reset PCD and Gadget driver to a
30469 + * clean state. */
30470 + core_if->lx_state = DWC_OTG_L0;
30471 + DWC_SPINUNLOCK(core_if->lock);
30472 + cil_pcd_stop(core_if);
30473 + DWC_SPINLOCK(core_if->lock);
30474 +
30475 + if (core_if->adp_enable) {
30476 + if (core_if->power_down == 2) {
30477 + gpwrdn.d32 = 0;
30478 + gpwrdn.b.pwrdnswtch = 1;
30479 + DWC_MODIFY_REG32(&core_if->
30480 + core_global_regs->
30481 + gpwrdn, gpwrdn.d32, 0);
30482 + }
30483 +
30484 + gpwrdn.d32 = 0;
30485 + gpwrdn.b.pmuintsel = 1;
30486 + gpwrdn.b.pmuactv = 1;
30487 + DWC_MODIFY_REG32(&core_if->core_global_regs->
30488 + gpwrdn, 0, gpwrdn.d32);
30489 +
30490 + dwc_otg_adp_sense_start(core_if);
30491 + }
30492 + }
30493 +
30494 + gotgctl.d32 = 0;
30495 + gotgctl.b.devhnpen = 1;
30496 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
30497 + }
30498 + if (gotgint.b.sesreqsucstschng) {
30499 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30500 + "Session Reqeust Success Status Change++\n");
30501 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30502 + if (gotgctl.b.sesreqscs) {
30503 +
30504 + if ((core_if->core_params->phy_type ==
30505 + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
30506 + core_if->srp_success = 1;
30507 + } else {
30508 + DWC_SPINUNLOCK(core_if->lock);
30509 + cil_pcd_resume(core_if);
30510 + DWC_SPINLOCK(core_if->lock);
30511 + /* Clear Session Request */
30512 + gotgctl.d32 = 0;
30513 + gotgctl.b.sesreq = 1;
30514 + DWC_MODIFY_REG32(&global_regs->gotgctl,
30515 + gotgctl.d32, 0);
30516 + }
30517 + }
30518 + }
30519 + if (gotgint.b.hstnegsucstschng) {
30520 + /* Print statements during the HNP interrupt handling
30521 + * can cause it to fail.*/
30522 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30523 + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
30524 + * this does not help*/
30525 + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
30526 + dwc_udelay(100);
30527 + if (gotgctl.b.hstnegscs) {
30528 + if (dwc_otg_is_host_mode(core_if)) {
30529 + core_if->op_state = B_HOST;
30530 + /*
30531 + * Need to disable SOF interrupt immediately.
30532 + * When switching from device to host, the PCD
30533 + * interrupt handler won't handle the
30534 + * interrupt if host mode is already set. The
30535 + * HCD interrupt handler won't get called if
30536 + * the HCD state is HALT. This means that the
30537 + * interrupt does not get handled and Linux
30538 + * complains loudly.
30539 + */
30540 + gintmsk.d32 = 0;
30541 + gintmsk.b.sofintr = 1;
30542 + DWC_MODIFY_REG32(&global_regs->gintmsk,
30543 + gintmsk.d32, 0);
30544 + /* Call callback function with spin lock released */
30545 + DWC_SPINUNLOCK(core_if->lock);
30546 + cil_pcd_stop(core_if);
30547 + /*
30548 + * Initialize the Core for Host mode.
30549 + */
30550 + cil_hcd_start(core_if);
30551 + DWC_SPINLOCK(core_if->lock);
30552 + core_if->op_state = B_HOST;
30553 + }
30554 + } else {
30555 + gotgctl.d32 = 0;
30556 + gotgctl.b.hnpreq = 1;
30557 + gotgctl.b.devhnpen = 1;
30558 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
30559 + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
30560 + __DWC_ERROR("Device Not Connected/Responding\n");
30561 + }
30562 + }
30563 + if (gotgint.b.hstnegdet) {
30564 + /* The disconnect interrupt is set at the same time as
30565 + * Host Negotiation Detected. During the mode
30566 + * switch all interrupts are cleared so the disconnect
30567 + * interrupt handler will not get executed.
30568 + */
30569 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30570 + "Host Negotiation Detected++ (%s)\n",
30571 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30572 + "Device"));
30573 + if (dwc_otg_is_device_mode(core_if)) {
30574 + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
30575 + core_if->op_state);
30576 + DWC_SPINUNLOCK(core_if->lock);
30577 + cil_hcd_disconnect(core_if);
30578 + cil_pcd_start(core_if);
30579 + DWC_SPINLOCK(core_if->lock);
30580 + core_if->op_state = A_PERIPHERAL;
30581 + } else {
30582 + /*
30583 + * Need to disable SOF interrupt immediately. When
30584 + * switching from device to host, the PCD interrupt
30585 + * handler won't handle the interrupt if host mode is
30586 + * already set. The HCD interrupt handler won't get
30587 + * called if the HCD state is HALT. This means that
30588 + * the interrupt does not get handled and Linux
30589 + * complains loudly.
30590 + */
30591 + gintmsk.d32 = 0;
30592 + gintmsk.b.sofintr = 1;
30593 + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
30594 + DWC_SPINUNLOCK(core_if->lock);
30595 + cil_pcd_stop(core_if);
30596 + cil_hcd_start(core_if);
30597 + DWC_SPINLOCK(core_if->lock);
30598 + core_if->op_state = A_HOST;
30599 + }
30600 + }
30601 + if (gotgint.b.adevtoutchng) {
30602 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30603 + "A-Device Timeout Change++\n");
30604 + }
30605 + if (gotgint.b.debdone) {
30606 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
30607 + }
30608 +
30609 + /* Clear GOTGINT */
30610 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
30611 +
30612 + return 1;
30613 +}
30614 +
30615 +void w_conn_id_status_change(void *p)
30616 +{
30617 + dwc_otg_core_if_t *core_if = p;
30618 + uint32_t count = 0;
30619 + gotgctl_data_t gotgctl = {.d32 = 0 };
30620 +
30621 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
30622 + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
30623 + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
30624 +
30625 + /* B-Device connector (Device Mode) */
30626 + if (gotgctl.b.conidsts) {
30627 + /* Wait for switch to device mode. */
30628 + while (!dwc_otg_is_device_mode(core_if)) {
30629 + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
30630 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30631 + "Peripheral"));
30632 + dwc_mdelay(100);
30633 + if (++count > 10000)
30634 + break;
30635 + }
30636 + DWC_ASSERT(++count < 10000,
30637 + "Connection id status change timed out");
30638 + core_if->op_state = B_PERIPHERAL;
30639 + dwc_otg_core_init(core_if);
30640 + dwc_otg_enable_global_interrupts(core_if);
30641 + cil_pcd_start(core_if);
30642 + } else {
30643 + /* A-Device connector (Host Mode) */
30644 + while (!dwc_otg_is_host_mode(core_if)) {
30645 + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
30646 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30647 + "Peripheral"));
30648 + dwc_mdelay(100);
30649 + if (++count > 10000)
30650 + break;
30651 + }
30652 + DWC_ASSERT(++count < 10000,
30653 + "Connection id status change timed out");
30654 + core_if->op_state = A_HOST;
30655 + /*
30656 + * Initialize the Core for Host mode.
30657 + */
30658 + dwc_otg_core_init(core_if);
30659 + dwc_otg_enable_global_interrupts(core_if);
30660 + cil_hcd_start(core_if);
30661 + }
30662 +}
30663 +
30664 +/**
30665 + * This function handles the Connector ID Status Change Interrupt. It
30666 + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
30667 + * is a Device to Host Mode transition or a Host Mode to Device
30668 + * Transition.
30669 + *
30670 + * This only occurs when the cable is connected/removed from the PHY
30671 + * connector.
30672 + *
30673 + * @param core_if Programming view of DWC_otg controller.
30674 + */
30675 +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
30676 +{
30677 +
30678 + /*
30679 + * Need to disable SOF interrupt immediately. If switching from device
30680 + * to host, the PCD interrupt handler won't handle the interrupt if
30681 + * host mode is already set. The HCD interrupt handler won't get
30682 + * called if the HCD state is HALT. This means that the interrupt does
30683 + * not get handled and Linux complains loudly.
30684 + */
30685 + gintmsk_data_t gintmsk = {.d32 = 0 };
30686 + gintsts_data_t gintsts = {.d32 = 0 };
30687 +
30688 + gintmsk.b.sofintr = 1;
30689 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
30690 +
30691 + DWC_DEBUGPL(DBG_CIL,
30692 + " ++Connector ID Status Change Interrupt++ (%s)\n",
30693 + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
30694 +
30695 + DWC_SPINUNLOCK(core_if->lock);
30696 +
30697 + /*
30698 + * Need to schedule a work, as there are possible DELAY function calls
30699 + * Release lock before scheduling workq as it holds spinlock during scheduling
30700 + */
30701 +
30702 + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
30703 + core_if, "connection id status change");
30704 + DWC_SPINLOCK(core_if->lock);
30705 +
30706 + /* Set flag and clear interrupt */
30707 + gintsts.b.conidstschng = 1;
30708 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30709 +
30710 + return 1;
30711 +}
30712 +
30713 +/**
30714 + * This interrupt indicates that a device is initiating the Session
30715 + * Request Protocol to request the host to turn on bus power so a new
30716 + * session can begin. The handler responds by turning on bus power. If
30717 + * the DWC_otg controller is in low power mode, the handler brings the
30718 + * controller out of low power mode before turning on bus power.
30719 + *
30720 + * @param core_if Programming view of DWC_otg controller.
30721 + */
30722 +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
30723 +{
30724 + gintsts_data_t gintsts;
30725 +
30726 +#ifndef DWC_HOST_ONLY
30727 + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
30728 +
30729 + if (dwc_otg_is_device_mode(core_if)) {
30730 + DWC_PRINTF("SRP: Device mode\n");
30731 + } else {
30732 + hprt0_data_t hprt0;
30733 + DWC_PRINTF("SRP: Host mode\n");
30734 +
30735 + /* Turn on the port power bit. */
30736 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
30737 + hprt0.b.prtpwr = 1;
30738 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
30739 +
30740 + /* Start the Connection timer. So a message can be displayed
30741 + * if connect does not occur within 10 seconds. */
30742 + cil_hcd_session_start(core_if);
30743 + }
30744 +#endif
30745 +
30746 + /* Clear interrupt */
30747 + gintsts.d32 = 0;
30748 + gintsts.b.sessreqintr = 1;
30749 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30750 +
30751 + return 1;
30752 +}
30753 +
30754 +void w_wakeup_detected(void *p)
30755 +{
30756 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
30757 + /*
30758 + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
30759 + * so that OPT tests pass with all PHYs).
30760 + */
30761 + hprt0_data_t hprt0 = {.d32 = 0 };
30762 +#if 0
30763 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
30764 + /* Restart the Phy Clock */
30765 + pcgcctl.b.stoppclk = 1;
30766 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
30767 + dwc_udelay(10);
30768 +#endif //0
30769 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
30770 + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
30771 +// dwc_mdelay(70);
30772 + hprt0.b.prtres = 0; /* Resume */
30773 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
30774 + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
30775 + DWC_READ_REG32(core_if->host_if->hprt0));
30776 +
30777 + cil_hcd_resume(core_if);
30778 +
30779 + /** Change to L0 state*/
30780 + core_if->lx_state = DWC_OTG_L0;
30781 +}
30782 +
30783 +/**
30784 + * This interrupt indicates that the DWC_otg controller has detected a
30785 + * resume or remote wakeup sequence. If the DWC_otg controller is in
30786 + * low power mode, the handler must brings the controller out of low
30787 + * power mode. The controller automatically begins resume
30788 + * signaling. The handler schedules a time to stop resume signaling.
30789 + */
30790 +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
30791 +{
30792 + gintsts_data_t gintsts;
30793 +
30794 + DWC_DEBUGPL(DBG_ANY,
30795 + "++Resume and Remote Wakeup Detected Interrupt++\n");
30796 +
30797 + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
30798 +
30799 + if (dwc_otg_is_device_mode(core_if)) {
30800 + dctl_data_t dctl = {.d32 = 0 };
30801 + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
30802 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
30803 + dsts));
30804 + if (core_if->lx_state == DWC_OTG_L2) {
30805 +#ifdef PARTIAL_POWER_DOWN
30806 + if (core_if->hwcfg4.b.power_optimiz) {
30807 + pcgcctl_data_t power = {.d32 = 0 };
30808 +
30809 + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
30810 + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
30811 + power.d32);
30812 +
30813 + power.b.stoppclk = 0;
30814 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30815 +
30816 + power.b.pwrclmp = 0;
30817 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30818 +
30819 + power.b.rstpdwnmodule = 0;
30820 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30821 + }
30822 +#endif
30823 + /* Clear the Remote Wakeup Signaling */
30824 + dctl.b.rmtwkupsig = 1;
30825 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
30826 + dctl, dctl.d32, 0);
30827 +
30828 + DWC_SPINUNLOCK(core_if->lock);
30829 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
30830 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
30831 + }
30832 + DWC_SPINLOCK(core_if->lock);
30833 + } else {
30834 + glpmcfg_data_t lpmcfg;
30835 + lpmcfg.d32 =
30836 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
30837 + lpmcfg.b.hird_thres &= (~(1 << 4));
30838 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
30839 + lpmcfg.d32);
30840 + }
30841 + /** Change to L0 state*/
30842 + core_if->lx_state = DWC_OTG_L0;
30843 + } else {
30844 + if (core_if->lx_state != DWC_OTG_L1) {
30845 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
30846 +
30847 + /* Restart the Phy Clock */
30848 + pcgcctl.b.stoppclk = 1;
30849 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
30850 + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
30851 + } else {
30852 + /** Change to L0 state*/
30853 + core_if->lx_state = DWC_OTG_L0;
30854 + }
30855 + }
30856 +
30857 + /* Clear interrupt */
30858 + gintsts.d32 = 0;
30859 + gintsts.b.wkupintr = 1;
30860 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30861 +
30862 + return 1;
30863 +}
30864 +
30865 +/**
30866 + * This interrupt indicates that the Wakeup Logic has detected a
30867 + * Device disconnect.
30868 + */
30869 +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
30870 +{
30871 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
30872 + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
30873 + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30874 +
30875 + DWC_PRINTF("%s called\n", __FUNCTION__);
30876 +
30877 + if (!core_if->hibernation_suspend) {
30878 + DWC_PRINTF("Already exited from Hibernation\n");
30879 + return 1;
30880 + }
30881 +
30882 + /* Switch on the voltage to the core */
30883 + gpwrdn.b.pwrdnswtch = 1;
30884 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30885 + dwc_udelay(10);
30886 +
30887 + /* Reset the core */
30888 + gpwrdn.d32 = 0;
30889 + gpwrdn.b.pwrdnrstn = 1;
30890 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30891 + dwc_udelay(10);
30892 +
30893 + /* Disable power clamps*/
30894 + gpwrdn.d32 = 0;
30895 + gpwrdn.b.pwrdnclmp = 1;
30896 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30897 +
30898 + /* Remove reset the core signal */
30899 + gpwrdn.d32 = 0;
30900 + gpwrdn.b.pwrdnrstn = 1;
30901 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30902 + dwc_udelay(10);
30903 +
30904 + /* Disable PMU interrupt */
30905 + gpwrdn.d32 = 0;
30906 + gpwrdn.b.pmuintsel = 1;
30907 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30908 +
30909 + core_if->hibernation_suspend = 0;
30910 +
30911 + /* Disable PMU */
30912 + gpwrdn.d32 = 0;
30913 + gpwrdn.b.pmuactv = 1;
30914 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30915 + dwc_udelay(10);
30916 +
30917 + if (gpwrdn_temp.b.idsts) {
30918 + core_if->op_state = B_PERIPHERAL;
30919 + dwc_otg_core_init(core_if);
30920 + dwc_otg_enable_global_interrupts(core_if);
30921 + cil_pcd_start(core_if);
30922 + } else {
30923 + core_if->op_state = A_HOST;
30924 + dwc_otg_core_init(core_if);
30925 + dwc_otg_enable_global_interrupts(core_if);
30926 + cil_hcd_start(core_if);
30927 + }
30928 +
30929 + return 1;
30930 +}
30931 +
30932 +/**
30933 + * This interrupt indicates that the Wakeup Logic has detected a
30934 + * remote wakeup sequence.
30935 + */
30936 +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
30937 +{
30938 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30939 + DWC_DEBUGPL(DBG_ANY,
30940 + "++Powerdown Remote Wakeup Detected Interrupt++\n");
30941 +
30942 + if (!core_if->hibernation_suspend) {
30943 + DWC_PRINTF("Already exited from Hibernation\n");
30944 + return 1;
30945 + }
30946 +
30947 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30948 + if (gpwrdn.b.idsts) { // Device Mode
30949 + if ((core_if->power_down == 2)
30950 + && (core_if->hibernation_suspend == 1)) {
30951 + dwc_otg_device_hibernation_restore(core_if, 0, 0);
30952 + }
30953 + } else {
30954 + if ((core_if->power_down == 2)
30955 + && (core_if->hibernation_suspend == 1)) {
30956 + dwc_otg_host_hibernation_restore(core_if, 1, 0);
30957 + }
30958 + }
30959 + return 1;
30960 +}
30961 +
30962 +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
30963 +{
30964 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30965 + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
30966 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
30967 +
30968 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
30969 + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30970 + if (core_if->power_down == 2) {
30971 + if (!core_if->hibernation_suspend) {
30972 + DWC_PRINTF("Already exited from Hibernation\n");
30973 + return 1;
30974 + }
30975 + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
30976 + /* Switch on the voltage to the core */
30977 + gpwrdn.b.pwrdnswtch = 1;
30978 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30979 + dwc_udelay(10);
30980 +
30981 + /* Reset the core */
30982 + gpwrdn.d32 = 0;
30983 + gpwrdn.b.pwrdnrstn = 1;
30984 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30985 + dwc_udelay(10);
30986 +
30987 + /* Disable power clamps */
30988 + gpwrdn.d32 = 0;
30989 + gpwrdn.b.pwrdnclmp = 1;
30990 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30991 +
30992 + /* Remove reset the core signal */
30993 + gpwrdn.d32 = 0;
30994 + gpwrdn.b.pwrdnrstn = 1;
30995 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30996 + dwc_udelay(10);
30997 +
30998 + /* Disable PMU interrupt */
30999 + gpwrdn.d32 = 0;
31000 + gpwrdn.b.pmuintsel = 1;
31001 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31002 +
31003 + /*Indicates that we are exiting from hibernation */
31004 + core_if->hibernation_suspend = 0;
31005 +
31006 + /* Disable PMU */
31007 + gpwrdn.d32 = 0;
31008 + gpwrdn.b.pmuactv = 1;
31009 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31010 + dwc_udelay(10);
31011 +
31012 + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
31013 + if (gpwrdn.b.dis_vbus == 1) {
31014 + gpwrdn.d32 = 0;
31015 + gpwrdn.b.dis_vbus = 1;
31016 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31017 + }
31018 +
31019 + if (gpwrdn_temp.b.idsts) {
31020 + core_if->op_state = B_PERIPHERAL;
31021 + dwc_otg_core_init(core_if);
31022 + dwc_otg_enable_global_interrupts(core_if);
31023 + cil_pcd_start(core_if);
31024 + } else {
31025 + core_if->op_state = A_HOST;
31026 + dwc_otg_core_init(core_if);
31027 + dwc_otg_enable_global_interrupts(core_if);
31028 + cil_hcd_start(core_if);
31029 + }
31030 + }
31031 +
31032 + if (core_if->adp_enable) {
31033 + uint8_t is_host = 0;
31034 + DWC_SPINUNLOCK(core_if->lock);
31035 + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
31036 +#ifndef DWC_HOST_ONLY
31037 + if (gpwrdn_temp.b.idsts)
31038 + core_if->lock = otg_dev->pcd->lock;
31039 +#endif
31040 +#ifndef DWC_DEVICE_ONLY
31041 + if (!gpwrdn_temp.b.idsts) {
31042 + core_if->lock = otg_dev->hcd->lock;
31043 + is_host = 1;
31044 + }
31045 +#endif
31046 + DWC_PRINTF("RESTART ADP\n");
31047 + if (core_if->adp.probe_enabled)
31048 + dwc_otg_adp_probe_stop(core_if);
31049 + if (core_if->adp.sense_enabled)
31050 + dwc_otg_adp_sense_stop(core_if);
31051 + if (core_if->adp.sense_timer_started)
31052 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
31053 + if (core_if->adp.vbuson_timer_started)
31054 + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
31055 + core_if->adp.probe_timer_values[0] = -1;
31056 + core_if->adp.probe_timer_values[1] = -1;
31057 + core_if->adp.sense_timer_started = 0;
31058 + core_if->adp.vbuson_timer_started = 0;
31059 + core_if->adp.probe_counter = 0;
31060 + core_if->adp.gpwrdn = 0;
31061 +
31062 + /* Disable PMU and restart ADP */
31063 + gpwrdn_temp.d32 = 0;
31064 + gpwrdn_temp.b.pmuactv = 1;
31065 + gpwrdn_temp.b.pmuintsel = 1;
31066 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31067 + DWC_PRINTF("Check point 1\n");
31068 + dwc_mdelay(110);
31069 + dwc_otg_adp_start(core_if, is_host);
31070 + DWC_SPINLOCK(core_if->lock);
31071 + }
31072 +
31073 +
31074 + return 1;
31075 +}
31076 +
31077 +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
31078 +{
31079 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31080 + int32_t otg_cap_param = core_if->core_params->otg_cap;
31081 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
31082 +
31083 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31084 + if (core_if->power_down == 2) {
31085 + if (!core_if->hibernation_suspend) {
31086 + DWC_PRINTF("Already exited from Hibernation\n");
31087 + return 1;
31088 + }
31089 +
31090 + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
31091 + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
31092 + gpwrdn.b.bsessvld == 0) {
31093 + /* Save gpwrdn register for further usage if stschng interrupt */
31094 + core_if->gr_backup->gpwrdn_local =
31095 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31096 + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
31097 + return 1;
31098 + }
31099 +
31100 + /* Switch on the voltage to the core */
31101 + gpwrdn.d32 = 0;
31102 + gpwrdn.b.pwrdnswtch = 1;
31103 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31104 + dwc_udelay(10);
31105 +
31106 + /* Reset the core */
31107 + gpwrdn.d32 = 0;
31108 + gpwrdn.b.pwrdnrstn = 1;
31109 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31110 + dwc_udelay(10);
31111 +
31112 + /* Disable power clamps */
31113 + gpwrdn.d32 = 0;
31114 + gpwrdn.b.pwrdnclmp = 1;
31115 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31116 +
31117 + /* Remove reset the core signal */
31118 + gpwrdn.d32 = 0;
31119 + gpwrdn.b.pwrdnrstn = 1;
31120 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
31121 + dwc_udelay(10);
31122 +
31123 + /* Disable PMU interrupt */
31124 + gpwrdn.d32 = 0;
31125 + gpwrdn.b.pmuintsel = 1;
31126 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31127 + dwc_udelay(10);
31128 +
31129 + /*Indicates that we are exiting from hibernation */
31130 + core_if->hibernation_suspend = 0;
31131 +
31132 + /* Disable PMU */
31133 + gpwrdn.d32 = 0;
31134 + gpwrdn.b.pmuactv = 1;
31135 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31136 + dwc_udelay(10);
31137 +
31138 + core_if->op_state = B_PERIPHERAL;
31139 + dwc_otg_core_init(core_if);
31140 + dwc_otg_enable_global_interrupts(core_if);
31141 + cil_pcd_start(core_if);
31142 +
31143 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
31144 + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
31145 + /*
31146 + * Initiate SRP after initial ADP probe.
31147 + */
31148 + dwc_otg_initiate_srp(core_if);
31149 + }
31150 + }
31151 +
31152 + return 1;
31153 +}
31154 +/**
31155 + * This interrupt indicates that the Wakeup Logic has detected a
31156 + * status change either on IDDIG or BSessVld.
31157 + */
31158 +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
31159 +{
31160 + int retval;
31161 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31162 + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
31163 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
31164 +
31165 + DWC_PRINTF("%s called\n", __FUNCTION__);
31166 +
31167 + if (core_if->power_down == 2) {
31168 + if (core_if->hibernation_suspend <= 0) {
31169 + DWC_PRINTF("Already exited from Hibernation\n");
31170 + return 1;
31171 + } else
31172 + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
31173 +
31174 + } else {
31175 + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
31176 + }
31177 +
31178 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31179 +
31180 + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
31181 + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
31182 + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
31183 + retval = dwc_otg_handle_pwrdn_session_change(core_if);
31184 + }
31185 +
31186 + return retval;
31187 +}
31188 +
31189 +/**
31190 + * This interrupt indicates that the Wakeup Logic has detected a
31191 + * SRP.
31192 + */
31193 +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
31194 +{
31195 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31196 +
31197 + DWC_PRINTF("%s called\n", __FUNCTION__);
31198 +
31199 + if (!core_if->hibernation_suspend) {
31200 + DWC_PRINTF("Already exited from Hibernation\n");
31201 + return 1;
31202 + }
31203 +#ifdef DWC_DEV_SRPCAP
31204 + if (core_if->pwron_timer_started) {
31205 + core_if->pwron_timer_started = 0;
31206 + DWC_TIMER_CANCEL(core_if->pwron_timer);
31207 + }
31208 +#endif
31209 +
31210 + /* Switch on the voltage to the core */
31211 + gpwrdn.b.pwrdnswtch = 1;
31212 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31213 + dwc_udelay(10);
31214 +
31215 + /* Reset the core */
31216 + gpwrdn.d32 = 0;
31217 + gpwrdn.b.pwrdnrstn = 1;
31218 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31219 + dwc_udelay(10);
31220 +
31221 + /* Disable power clamps */
31222 + gpwrdn.d32 = 0;
31223 + gpwrdn.b.pwrdnclmp = 1;
31224 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31225 +
31226 + /* Remove reset the core signal */
31227 + gpwrdn.d32 = 0;
31228 + gpwrdn.b.pwrdnrstn = 1;
31229 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
31230 + dwc_udelay(10);
31231 +
31232 + /* Disable PMU interrupt */
31233 + gpwrdn.d32 = 0;
31234 + gpwrdn.b.pmuintsel = 1;
31235 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31236 +
31237 + /* Indicates that we are exiting from hibernation */
31238 + core_if->hibernation_suspend = 0;
31239 +
31240 + /* Disable PMU */
31241 + gpwrdn.d32 = 0;
31242 + gpwrdn.b.pmuactv = 1;
31243 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31244 + dwc_udelay(10);
31245 +
31246 + /* Programm Disable VBUS to 0 */
31247 + gpwrdn.d32 = 0;
31248 + gpwrdn.b.dis_vbus = 1;
31249 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31250 +
31251 + /*Initialize the core as Host */
31252 + core_if->op_state = A_HOST;
31253 + dwc_otg_core_init(core_if);
31254 + dwc_otg_enable_global_interrupts(core_if);
31255 + cil_hcd_start(core_if);
31256 +
31257 + return 1;
31258 +}
31259 +
31260 +/** This interrupt indicates that restore command after Hibernation
31261 + * was completed by the core. */
31262 +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
31263 +{
31264 + pcgcctl_data_t pcgcctl;
31265 + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
31266 +
31267 + //TODO De-assert restore signal. 8.a
31268 + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
31269 + if (pcgcctl.b.restoremode == 1) {
31270 + gintmsk_data_t gintmsk = {.d32 = 0 };
31271 + /*
31272 + * If restore mode is Remote Wakeup,
31273 + * unmask Remote Wakeup interrupt.
31274 + */
31275 + gintmsk.b.wkupintr = 1;
31276 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
31277 + 0, gintmsk.d32);
31278 + }
31279 +
31280 + return 1;
31281 +}
31282 +
31283 +/**
31284 + * This interrupt indicates that a device has been disconnected from
31285 + * the root port.
31286 + */
31287 +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
31288 +{
31289 + gintsts_data_t gintsts;
31290 +
31291 + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
31292 + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
31293 + op_state_str(core_if));
31294 +
31295 +/** @todo Consolidate this if statement. */
31296 +#ifndef DWC_HOST_ONLY
31297 + if (core_if->op_state == B_HOST) {
31298 + /* If in device mode Disconnect and stop the HCD, then
31299 + * start the PCD. */
31300 + DWC_SPINUNLOCK(core_if->lock);
31301 + cil_hcd_disconnect(core_if);
31302 + cil_pcd_start(core_if);
31303 + DWC_SPINLOCK(core_if->lock);
31304 + core_if->op_state = B_PERIPHERAL;
31305 + } else if (dwc_otg_is_device_mode(core_if)) {
31306 + gotgctl_data_t gotgctl = {.d32 = 0 };
31307 + gotgctl.d32 =
31308 + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
31309 + if (gotgctl.b.hstsethnpen == 1) {
31310 + /* Do nothing, if HNP in process the OTG
31311 + * interrupt "Host Negotiation Detected"
31312 + * interrupt will do the mode switch.
31313 + */
31314 + } else if (gotgctl.b.devhnpen == 0) {
31315 + /* If in device mode Disconnect and stop the HCD, then
31316 + * start the PCD. */
31317 + DWC_SPINUNLOCK(core_if->lock);
31318 + cil_hcd_disconnect(core_if);
31319 + cil_pcd_start(core_if);
31320 + DWC_SPINLOCK(core_if->lock);
31321 + core_if->op_state = B_PERIPHERAL;
31322 + } else {
31323 + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
31324 + }
31325 + } else {
31326 + if (core_if->op_state == A_HOST) {
31327 + /* A-Cable still connected but device disconnected. */
31328 + DWC_SPINUNLOCK(core_if->lock);
31329 + cil_hcd_disconnect(core_if);
31330 + DWC_SPINLOCK(core_if->lock);
31331 + if (core_if->adp_enable) {
31332 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
31333 + cil_hcd_stop(core_if);
31334 + /* Enable Power Down Logic */
31335 + gpwrdn.b.pmuintsel = 1;
31336 + gpwrdn.b.pmuactv = 1;
31337 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31338 + gpwrdn, 0, gpwrdn.d32);
31339 + dwc_otg_adp_probe_start(core_if);
31340 +
31341 + /* Power off the core */
31342 + if (core_if->power_down == 2) {
31343 + gpwrdn.d32 = 0;
31344 + gpwrdn.b.pwrdnswtch = 1;
31345 + DWC_MODIFY_REG32
31346 + (&core_if->core_global_regs->gpwrdn,
31347 + gpwrdn.d32, 0);
31348 + }
31349 + }
31350 + }
31351 + }
31352 +#endif
31353 + /* Change to L3(OFF) state */
31354 + core_if->lx_state = DWC_OTG_L3;
31355 +
31356 + gintsts.d32 = 0;
31357 + gintsts.b.disconnect = 1;
31358 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31359 + return 1;
31360 +}
31361 +
31362 +/**
31363 + * This interrupt indicates that SUSPEND state has been detected on
31364 + * the USB.
31365 + *
31366 + * For HNP the USB Suspend interrupt signals the change from
31367 + * "a_peripheral" to "a_host".
31368 + *
31369 + * When power management is enabled the core will be put in low power
31370 + * mode.
31371 + */
31372 +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
31373 +{
31374 + dsts_data_t dsts;
31375 + gintsts_data_t gintsts;
31376 + dcfg_data_t dcfg;
31377 +
31378 + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
31379 +
31380 + if (dwc_otg_is_device_mode(core_if)) {
31381 + /* Check the Device status register to determine if the Suspend
31382 + * state is active. */
31383 + dsts.d32 =
31384 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
31385 + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
31386 + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
31387 + "HWCFG4.power Optimize=%d\n",
31388 + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
31389 +
31390 +#ifdef PARTIAL_POWER_DOWN
31391 +/** @todo Add a module parameter for power management. */
31392 +
31393 + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
31394 + pcgcctl_data_t power = {.d32 = 0 };
31395 + DWC_DEBUGPL(DBG_CIL, "suspend\n");
31396 +
31397 + power.b.pwrclmp = 1;
31398 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
31399 +
31400 + power.b.rstpdwnmodule = 1;
31401 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
31402 +
31403 + power.b.stoppclk = 1;
31404 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
31405 +
31406 + } else {
31407 + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
31408 + }
31409 +#endif
31410 + /* PCD callback for suspend. Release the lock inside of callback function */
31411 + cil_pcd_suspend(core_if);
31412 + if (core_if->power_down == 2)
31413 + {
31414 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
31415 + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
31416 + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
31417 +
31418 + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
31419 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31420 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31421 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
31422 +
31423 + /* Change to L2(suspend) state */
31424 + core_if->lx_state = DWC_OTG_L2;
31425 +
31426 + /* Clear interrupt in gintsts */
31427 + gintsts.d32 = 0;
31428 + gintsts.b.usbsuspend = 1;
31429 + DWC_WRITE_REG32(&core_if->core_global_regs->
31430 + gintsts, gintsts.d32);
31431 + DWC_PRINTF("Start of hibernation completed\n");
31432 + dwc_otg_save_global_regs(core_if);
31433 + dwc_otg_save_dev_regs(core_if);
31434 +
31435 + gusbcfg.d32 =
31436 + DWC_READ_REG32(&core_if->core_global_regs->
31437 + gusbcfg);
31438 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
31439 + /* ULPI interface */
31440 + /* Suspend the Phy Clock */
31441 + pcgcctl.d32 = 0;
31442 + pcgcctl.b.stoppclk = 1;
31443 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
31444 + pcgcctl.d32);
31445 + dwc_udelay(10);
31446 + gpwrdn.b.pmuactv = 1;
31447 + DWC_MODIFY_REG32(&core_if->
31448 + core_global_regs->
31449 + gpwrdn, 0, gpwrdn.d32);
31450 + } else {
31451 + /* UTMI+ Interface */
31452 + gpwrdn.b.pmuactv = 1;
31453 + DWC_MODIFY_REG32(&core_if->
31454 + core_global_regs->
31455 + gpwrdn, 0, gpwrdn.d32);
31456 + dwc_udelay(10);
31457 + pcgcctl.b.stoppclk = 1;
31458 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
31459 + pcgcctl.d32);
31460 + dwc_udelay(10);
31461 + }
31462 +
31463 + /* Set flag to indicate that we are in hibernation */
31464 + core_if->hibernation_suspend = 1;
31465 + /* Enable interrupts from wake up logic */
31466 + gpwrdn.d32 = 0;
31467 + gpwrdn.b.pmuintsel = 1;
31468 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31469 + gpwrdn, 0, gpwrdn.d32);
31470 + dwc_udelay(10);
31471 +
31472 + /* Unmask device mode interrupts in GPWRDN */
31473 + gpwrdn.d32 = 0;
31474 + gpwrdn.b.rst_det_msk = 1;
31475 + gpwrdn.b.lnstchng_msk = 1;
31476 + gpwrdn.b.sts_chngint_msk = 1;
31477 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31478 + gpwrdn, 0, gpwrdn.d32);
31479 + dwc_udelay(10);
31480 +
31481 + /* Enable Power Down Clamp */
31482 + gpwrdn.d32 = 0;
31483 + gpwrdn.b.pwrdnclmp = 1;
31484 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31485 + gpwrdn, 0, gpwrdn.d32);
31486 + dwc_udelay(10);
31487 +
31488 + /* Switch off VDD */
31489 + gpwrdn.d32 = 0;
31490 + gpwrdn.b.pwrdnswtch = 1;
31491 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31492 + gpwrdn, 0, gpwrdn.d32);
31493 +
31494 + /* Save gpwrdn register for further usage if stschng interrupt */
31495 + core_if->gr_backup->gpwrdn_local =
31496 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31497 + DWC_PRINTF("Hibernation completed\n");
31498 +
31499 + return 1;
31500 + }
31501 + } else if (core_if->power_down == 3) {
31502 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31503 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
31504 + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
31505 + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
31506 +
31507 + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
31508 + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
31509 + core_if->xhib = 1;
31510 +
31511 + /* Clear interrupt in gintsts */
31512 + gintsts.d32 = 0;
31513 + gintsts.b.usbsuspend = 1;
31514 + DWC_WRITE_REG32(&core_if->core_global_regs->
31515 + gintsts, gintsts.d32);
31516 +
31517 + dwc_otg_save_global_regs(core_if);
31518 + dwc_otg_save_dev_regs(core_if);
31519 +
31520 + /* Wait for 10 PHY clocks */
31521 + dwc_udelay(10);
31522 +
31523 + /* Program GPIO register while entering to xHib */
31524 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
31525 +
31526 + pcgcctl.b.enbl_extnd_hiber = 1;
31527 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31528 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31529 +
31530 + pcgcctl.d32 = 0;
31531 + pcgcctl.b.extnd_hiber_pwrclmp = 1;
31532 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31533 +
31534 + pcgcctl.d32 = 0;
31535 + pcgcctl.b.extnd_hiber_switch = 1;
31536 + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31537 + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
31538 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31539 +
31540 + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
31541 +
31542 + return 1;
31543 + }
31544 + }
31545 + } else {
31546 + if (core_if->op_state == A_PERIPHERAL) {
31547 + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
31548 + /* Clear the a_peripheral flag, back to a_host. */
31549 + DWC_SPINUNLOCK(core_if->lock);
31550 + cil_pcd_stop(core_if);
31551 + cil_hcd_start(core_if);
31552 + DWC_SPINLOCK(core_if->lock);
31553 + core_if->op_state = A_HOST;
31554 + }
31555 + }
31556 +
31557 + /* Change to L2(suspend) state */
31558 + core_if->lx_state = DWC_OTG_L2;
31559 +
31560 + /* Clear interrupt */
31561 + gintsts.d32 = 0;
31562 + gintsts.b.usbsuspend = 1;
31563 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31564 +
31565 + return 1;
31566 +}
31567 +
31568 +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
31569 +{
31570 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31571 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31572 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
31573 +
31574 + dwc_udelay(10);
31575 +
31576 + /* Program GPIO register while entering to xHib */
31577 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
31578 +
31579 + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
31580 + pcgcctl.b.extnd_hiber_pwrclmp = 0;
31581 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31582 + dwc_udelay(10);
31583 +
31584 + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
31585 + gpwrdn.b.restore = 1;
31586 + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
31587 + dwc_udelay(10);
31588 +
31589 + restore_lpm_i2c_regs(core_if);
31590 +
31591 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31592 + pcgcctl.b.max_xcvrselect = 1;
31593 + pcgcctl.b.ess_reg_restored = 0;
31594 + pcgcctl.b.extnd_hiber_switch = 0;
31595 + pcgcctl.b.extnd_hiber_pwrclmp = 0;
31596 + pcgcctl.b.enbl_extnd_hiber = 1;
31597 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31598 +
31599 + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
31600 + gahbcfg.b.glblintrmsk = 1;
31601 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
31602 +
31603 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
31604 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
31605 +
31606 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
31607 + core_if->gr_backup->gusbcfg_local);
31608 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
31609 + core_if->dr_backup->dcfg);
31610 +
31611 + pcgcctl.d32 = 0;
31612 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31613 + pcgcctl.b.max_xcvrselect = 1;
31614 + pcgcctl.d32 |= 0x608;
31615 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31616 + dwc_udelay(10);
31617 +
31618 + pcgcctl.d32 = 0;
31619 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31620 + pcgcctl.b.max_xcvrselect = 1;
31621 + pcgcctl.b.ess_reg_restored = 1;
31622 + pcgcctl.b.enbl_extnd_hiber = 1;
31623 + pcgcctl.b.rstpdwnmodule = 1;
31624 + pcgcctl.b.restoremode = 1;
31625 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31626 +
31627 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
31628 +
31629 + return 1;
31630 +}
31631 +
31632 +#ifdef CONFIG_USB_DWC_OTG_LPM
31633 +/**
31634 + * This function hadles LPM transaction received interrupt.
31635 + */
31636 +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
31637 +{
31638 + glpmcfg_data_t lpmcfg;
31639 + gintsts_data_t gintsts;
31640 +
31641 + if (!core_if->core_params->lpm_enable) {
31642 + DWC_PRINTF("Unexpected LPM interrupt\n");
31643 + }
31644 +
31645 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
31646 + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
31647 +
31648 + if (dwc_otg_is_host_mode(core_if)) {
31649 + cil_hcd_sleep(core_if);
31650 + } else {
31651 + lpmcfg.b.hird_thres |= (1 << 4);
31652 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
31653 + lpmcfg.d32);
31654 + }
31655 +
31656 + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
31657 + dwc_udelay(10);
31658 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
31659 + if (lpmcfg.b.prt_sleep_sts) {
31660 + /* Save the current state */
31661 + core_if->lx_state = DWC_OTG_L1;
31662 + }
31663 +
31664 + /* Clear interrupt */
31665 + gintsts.d32 = 0;
31666 + gintsts.b.lpmtranrcvd = 1;
31667 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31668 + return 1;
31669 +}
31670 +#endif /* CONFIG_USB_DWC_OTG_LPM */
31671 +
31672 +/**
31673 + * This function returns the Core Interrupt register.
31674 + */
31675 +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)
31676 +{
31677 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
31678 + gintsts_data_t gintsts;
31679 + gintmsk_data_t gintmsk;
31680 + gintmsk_data_t gintmsk_common = {.d32 = 0 };
31681 + gintmsk_common.b.wkupintr = 1;
31682 + gintmsk_common.b.sessreqintr = 1;
31683 + gintmsk_common.b.conidstschng = 1;
31684 + gintmsk_common.b.otgintr = 1;
31685 + gintmsk_common.b.modemismatch = 1;
31686 + gintmsk_common.b.disconnect = 1;
31687 + gintmsk_common.b.usbsuspend = 1;
31688 +#ifdef CONFIG_USB_DWC_OTG_LPM
31689 + gintmsk_common.b.lpmtranrcvd = 1;
31690 +#endif
31691 + gintmsk_common.b.restoredone = 1;
31692 + if(dwc_otg_is_device_mode(core_if))
31693 + {
31694 + /** @todo: The port interrupt occurs while in device
31695 + * mode. Added code to CIL to clear the interrupt for now!
31696 + */
31697 + gintmsk_common.b.portintr = 1;
31698 + }
31699 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
31700 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
31701 + if(fiq_enable) {
31702 + local_fiq_disable();
31703 + /* Pull in the interrupts that the FIQ has masked */
31704 + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
31705 + gintmsk.d32 |= gintmsk_common.d32;
31706 + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
31707 + reenable_gintmsk->d32 = gintmsk.d32;
31708 + local_fiq_enable();
31709 + }
31710 +
31711 + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
31712 +
31713 +#ifdef DEBUG
31714 + /* if any common interrupts set */
31715 + if (gintsts.d32 & gintmsk_common.d32) {
31716 + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
31717 + gintsts.d32, gintmsk.d32);
31718 + }
31719 +#endif
31720 + if (!fiq_enable){
31721 + if (gahbcfg.b.glblintrmsk)
31722 + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
31723 + else
31724 + return 0;
31725 + } else {
31726 + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
31727 + * Can't trust the global interrupt mask bit in this case.
31728 + */
31729 + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
31730 + }
31731 +
31732 +}
31733 +
31734 +/* MACRO for clearing interupt bits in GPWRDN register */
31735 +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
31736 +do { \
31737 + gpwrdn_data_t gpwrdn = {.d32=0}; \
31738 + gpwrdn.b.__intr = 1; \
31739 + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
31740 + 0, gpwrdn.d32); \
31741 +} while (0)
31742 +
31743 +/**
31744 + * Common interrupt handler.
31745 + *
31746 + * The common interrupts are those that occur in both Host and Device mode.
31747 + * This handler handles the following interrupts:
31748 + * - Mode Mismatch Interrupt
31749 + * - Disconnect Interrupt
31750 + * - OTG Interrupt
31751 + * - Connector ID Status Change Interrupt
31752 + * - Session Request Interrupt.
31753 + * - Resume / Remote Wakeup Detected Interrupt.
31754 + * - LPM Transaction Received Interrupt
31755 + * - ADP Transaction Received Interrupt
31756 + *
31757 + */
31758 +int32_t dwc_otg_handle_common_intr(void *dev)
31759 +{
31760 + int retval = 0;
31761 + gintsts_data_t gintsts;
31762 + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
31763 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31764 + dwc_otg_device_t *otg_dev = dev;
31765 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
31766 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31767 + if (dwc_otg_is_device_mode(core_if))
31768 + core_if->frame_num = dwc_otg_get_frame_number(core_if);
31769 +
31770 + if (core_if->lock)
31771 + DWC_SPINLOCK(core_if->lock);
31772 +
31773 + if (core_if->power_down == 3 && core_if->xhib == 1) {
31774 + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
31775 + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
31776 + core_if->xhib = 2;
31777 + if (core_if->lock)
31778 + DWC_SPINUNLOCK(core_if->lock);
31779 +
31780 + return retval;
31781 + }
31782 +
31783 + if (core_if->hibernation_suspend <= 0) {
31784 + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
31785 + * of this handler - god only knows why it's done like this
31786 + */
31787 + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
31788 +
31789 + if (gintsts.b.modemismatch) {
31790 + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
31791 + }
31792 + if (gintsts.b.otgintr) {
31793 + retval |= dwc_otg_handle_otg_intr(core_if);
31794 + }
31795 + if (gintsts.b.conidstschng) {
31796 + retval |=
31797 + dwc_otg_handle_conn_id_status_change_intr(core_if);
31798 + }
31799 + if (gintsts.b.disconnect) {
31800 + retval |= dwc_otg_handle_disconnect_intr(core_if);
31801 + }
31802 + if (gintsts.b.sessreqintr) {
31803 + retval |= dwc_otg_handle_session_req_intr(core_if);
31804 + }
31805 + if (gintsts.b.wkupintr) {
31806 + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
31807 + }
31808 + if (gintsts.b.usbsuspend) {
31809 + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
31810 + }
31811 +#ifdef CONFIG_USB_DWC_OTG_LPM
31812 + if (gintsts.b.lpmtranrcvd) {
31813 + retval |= dwc_otg_handle_lpm_intr(core_if);
31814 + }
31815 +#endif
31816 + if (gintsts.b.restoredone) {
31817 + gintsts.d32 = 0;
31818 + if (core_if->power_down == 2)
31819 + core_if->hibernation_suspend = -1;
31820 + else if (core_if->power_down == 3 && core_if->xhib == 2) {
31821 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31822 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31823 + dctl_data_t dctl = {.d32 = 0 };
31824 +
31825 + DWC_WRITE_REG32(&core_if->core_global_regs->
31826 + gintsts, 0xFFFFFFFF);
31827 +
31828 + DWC_DEBUGPL(DBG_ANY,
31829 + "RESTORE DONE generated\n");
31830 +
31831 + gpwrdn.b.restore = 1;
31832 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31833 + dwc_udelay(10);
31834 +
31835 + pcgcctl.b.rstpdwnmodule = 1;
31836 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
31837 +
31838 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
31839 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
31840 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
31841 + dwc_udelay(50);
31842 +
31843 + dctl.b.pwronprgdone = 1;
31844 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
31845 + dwc_udelay(10);
31846 +
31847 + dwc_otg_restore_global_regs(core_if);
31848 + dwc_otg_restore_dev_regs(core_if, 0);
31849 +
31850 + dctl.d32 = 0;
31851 + dctl.b.pwronprgdone = 1;
31852 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
31853 + dwc_udelay(10);
31854 +
31855 + pcgcctl.d32 = 0;
31856 + pcgcctl.b.enbl_extnd_hiber = 1;
31857 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
31858 +
31859 + /* The core will be in ON STATE */
31860 + core_if->lx_state = DWC_OTG_L0;
31861 + core_if->xhib = 0;
31862 +
31863 + DWC_SPINUNLOCK(core_if->lock);
31864 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
31865 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
31866 + }
31867 + DWC_SPINLOCK(core_if->lock);
31868 +
31869 + }
31870 +
31871 + gintsts.b.restoredone = 1;
31872 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
31873 + DWC_PRINTF(" --Restore done interrupt received-- \n");
31874 + retval |= 1;
31875 + }
31876 + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
31877 + /* The port interrupt occurs while in device mode with HPRT0
31878 + * Port Enable/Disable.
31879 + */
31880 + gintsts.d32 = 0;
31881 + gintsts.b.portintr = 1;
31882 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
31883 + retval |= 1;
31884 + gintmsk_reenable.b.portintr = 1;
31885 +
31886 + }
31887 + /* Did we actually handle anything? if so, unmask the interrupt */
31888 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
31889 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
31890 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
31891 + if (retval && fiq_enable) {
31892 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
31893 + }
31894 +
31895 + } else {
31896 + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
31897 +
31898 + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
31899 + CLEAR_GPWRDN_INTR(core_if, disconn_det);
31900 + if (gpwrdn.b.linestate == 0) {
31901 + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
31902 + } else {
31903 + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
31904 + }
31905 +
31906 + retval |= 1;
31907 + }
31908 + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
31909 + CLEAR_GPWRDN_INTR(core_if, lnstschng);
31910 + /* remote wakeup from hibernation */
31911 + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
31912 + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
31913 + } else {
31914 + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
31915 + }
31916 + retval |= 1;
31917 + }
31918 + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
31919 + CLEAR_GPWRDN_INTR(core_if, rst_det);
31920 + if (gpwrdn.b.linestate == 0) {
31921 + DWC_PRINTF("Reset detected\n");
31922 + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
31923 + }
31924 + }
31925 + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
31926 + CLEAR_GPWRDN_INTR(core_if, srp_det);
31927 + dwc_otg_handle_pwrdn_srp_intr(core_if);
31928 + retval |= 1;
31929 + }
31930 + }
31931 + /* Handle ADP interrupt here */
31932 + if (gpwrdn.b.adp_int) {
31933 + DWC_PRINTF("ADP interrupt\n");
31934 + CLEAR_GPWRDN_INTR(core_if, adp_int);
31935 + dwc_otg_adp_handle_intr(core_if);
31936 + retval |= 1;
31937 + }
31938 + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
31939 + DWC_PRINTF("STS CHNG interrupt asserted\n");
31940 + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
31941 + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
31942 +
31943 + retval |= 1;
31944 + }
31945 + if (core_if->lock)
31946 + DWC_SPINUNLOCK(core_if->lock);
31947 + return retval;
31948 +}
31949 --- /dev/null
31950 +++ b/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
31951 @@ -0,0 +1,705 @@
31952 +/* ==========================================================================
31953 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
31954 + * $Revision: #13 $
31955 + * $Date: 2012/08/10 $
31956 + * $Change: 2047372 $
31957 + *
31958 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
31959 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
31960 + * otherwise expressly agreed to in writing between Synopsys and you.
31961 + *
31962 + * The Software IS NOT an item of Licensed Software or Licensed Product under
31963 + * any End User Software License Agreement or Agreement for Licensed Product
31964 + * with Synopsys or any supplement thereto. You are permitted to use and
31965 + * redistribute this Software in source and binary forms, with or without
31966 + * modification, provided that redistributions of source code must retain this
31967 + * notice. You may not view, use, disclose, copy or distribute this file or
31968 + * any information contained herein except pursuant to this license grant from
31969 + * Synopsys. If you do not agree with this notice, including the disclaimer
31970 + * below, then you are not authorized to use the Software.
31971 + *
31972 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
31973 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31974 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31975 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
31976 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31977 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31978 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31979 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31980 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31981 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31982 + * DAMAGE.
31983 + * ========================================================================== */
31984 +#if !defined(__DWC_CORE_IF_H__)
31985 +#define __DWC_CORE_IF_H__
31986 +
31987 +#include "dwc_os.h"
31988 +
31989 +/** @file
31990 + * This file defines DWC_OTG Core API
31991 + */
31992 +
31993 +struct dwc_otg_core_if;
31994 +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
31995 +
31996 +/** Maximum number of Periodic FIFOs */
31997 +#define MAX_PERIO_FIFOS 15
31998 +/** Maximum number of Periodic FIFOs */
31999 +#define MAX_TX_FIFOS 15
32000 +
32001 +/** Maximum number of Endpoints/HostChannels */
32002 +#define MAX_EPS_CHANNELS 16
32003 +
32004 +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
32005 +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
32006 +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
32007 +
32008 +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
32009 +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
32010 +
32011 +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
32012 +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
32013 +
32014 +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
32015 +
32016 +/** This function should be called on every hardware interrupt. */
32017 +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
32018 +
32019 +/** @name OTG Core Parameters */
32020 +/** @{ */
32021 +
32022 +/**
32023 + * Specifies the OTG capabilities. The driver will automatically
32024 + * detect the value for this parameter if none is specified.
32025 + * 0 - HNP and SRP capable (default)
32026 + * 1 - SRP Only capable
32027 + * 2 - No HNP/SRP capable
32028 + */
32029 +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
32030 +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
32031 +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
32032 +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
32033 +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
32034 +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
32035 +
32036 +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
32037 +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
32038 +#define dwc_param_opt_default 1
32039 +
32040 +/**
32041 + * Specifies whether to use slave or DMA mode for accessing the data
32042 + * FIFOs. The driver will automatically detect the value for this
32043 + * parameter if none is specified.
32044 + * 0 - Slave
32045 + * 1 - DMA (default, if available)
32046 + */
32047 +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
32048 + int32_t val);
32049 +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
32050 +#define dwc_param_dma_enable_default 1
32051 +
32052 +/**
32053 + * When DMA mode is enabled specifies whether to use
32054 + * address DMA or DMA Descritor mode for accessing the data
32055 + * FIFOs in device mode. The driver will automatically detect
32056 + * the value for this parameter if none is specified.
32057 + * 0 - address DMA
32058 + * 1 - DMA Descriptor(default, if available)
32059 + */
32060 +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
32061 + int32_t val);
32062 +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
32063 +//#define dwc_param_dma_desc_enable_default 1
32064 +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
32065 +
32066 +/** The DMA Burst size (applicable only for External DMA
32067 + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
32068 + */
32069 +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
32070 + int32_t val);
32071 +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
32072 +#define dwc_param_dma_burst_size_default 32
32073 +
32074 +/**
32075 + * Specifies the maximum speed of operation in host and device mode.
32076 + * The actual speed depends on the speed of the attached device and
32077 + * the value of phy_type. The actual speed depends on the speed of the
32078 + * attached device.
32079 + * 0 - High Speed (default)
32080 + * 1 - Full Speed
32081 + */
32082 +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
32083 +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
32084 +#define dwc_param_speed_default 0
32085 +#define DWC_SPEED_PARAM_HIGH 0
32086 +#define DWC_SPEED_PARAM_FULL 1
32087 +
32088 +/** Specifies whether low power mode is supported when attached
32089 + * to a Full Speed or Low Speed device in host mode.
32090 + * 0 - Don't support low power mode (default)
32091 + * 1 - Support low power mode
32092 + */
32093 +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
32094 + core_if, int32_t val);
32095 +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
32096 + * core_if);
32097 +#define dwc_param_host_support_fs_ls_low_power_default 0
32098 +
32099 +/** Specifies the PHY clock rate in low power mode when connected to a
32100 + * Low Speed device in host mode. This parameter is applicable only if
32101 + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
32102 + * then defaults to 6 MHZ otherwise 48 MHZ.
32103 + *
32104 + * 0 - 48 MHz
32105 + * 1 - 6 MHz
32106 + */
32107 +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
32108 + core_if, int32_t val);
32109 +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
32110 + core_if);
32111 +#define dwc_param_host_ls_low_power_phy_clk_default 0
32112 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
32113 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
32114 +
32115 +/**
32116 + * 0 - Use cC FIFO size parameters
32117 + * 1 - Allow dynamic FIFO sizing (default)
32118 + */
32119 +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
32120 + int32_t val);
32121 +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
32122 + core_if);
32123 +#define dwc_param_enable_dynamic_fifo_default 1
32124 +
32125 +/** Total number of 4-byte words in the data FIFO memory. This
32126 + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
32127 + * Tx FIFOs.
32128 + * 32 to 32768 (default 8192)
32129 + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
32130 + */
32131 +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
32132 + int32_t val);
32133 +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
32134 +//#define dwc_param_data_fifo_size_default 8192
32135 +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
32136 +
32137 +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
32138 + * FIFO sizing is enabled.
32139 + * 16 to 32768 (default 1064)
32140 + */
32141 +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
32142 + int32_t val);
32143 +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
32144 +#define dwc_param_dev_rx_fifo_size_default 1064
32145 +
32146 +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
32147 + * when dynamic FIFO sizing is enabled.
32148 + * 16 to 32768 (default 1024)
32149 + */
32150 +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
32151 + core_if, int32_t val);
32152 +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
32153 + core_if);
32154 +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
32155 +
32156 +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
32157 + * mode when dynamic FIFO sizing is enabled.
32158 + * 4 to 768 (default 256)
32159 + */
32160 +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
32161 + int32_t val, int fifo_num);
32162 +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
32163 + core_if, int fifo_num);
32164 +#define dwc_param_dev_perio_tx_fifo_size_default 256
32165 +
32166 +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
32167 + * FIFO sizing is enabled.
32168 + * 16 to 32768 (default 1024)
32169 + */
32170 +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
32171 + int32_t val);
32172 +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
32173 +//#define dwc_param_host_rx_fifo_size_default 1024
32174 +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
32175 +
32176 +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
32177 + * when Dynamic FIFO sizing is enabled in the core.
32178 + * 16 to 32768 (default 1024)
32179 + */
32180 +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
32181 + core_if, int32_t val);
32182 +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
32183 + core_if);
32184 +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
32185 +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
32186 +
32187 +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
32188 + * FIFO sizing is enabled.
32189 + * 16 to 32768 (default 1024)
32190 + */
32191 +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
32192 + core_if, int32_t val);
32193 +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
32194 + core_if);
32195 +//#define dwc_param_host_perio_tx_fifo_size_default 1024
32196 +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
32197 +
32198 +/** The maximum transfer size supported in bytes.
32199 + * 2047 to 65,535 (default 65,535)
32200 + */
32201 +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
32202 + int32_t val);
32203 +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
32204 +#define dwc_param_max_transfer_size_default 65535
32205 +
32206 +/** The maximum number of packets in a transfer.
32207 + * 15 to 511 (default 511)
32208 + */
32209 +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
32210 + int32_t val);
32211 +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
32212 +#define dwc_param_max_packet_count_default 511
32213 +
32214 +/** The number of host channel registers to use.
32215 + * 1 to 16 (default 12)
32216 + * Note: The FPGA configuration supports a maximum of 12 host channels.
32217 + */
32218 +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
32219 + int32_t val);
32220 +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
32221 +//#define dwc_param_host_channels_default 12
32222 +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
32223 +
32224 +/** The number of endpoints in addition to EP0 available for device
32225 + * mode operations.
32226 + * 1 to 15 (default 6 IN and OUT)
32227 + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
32228 + * endpoints in addition to EP0.
32229 + */
32230 +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
32231 + int32_t val);
32232 +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
32233 +#define dwc_param_dev_endpoints_default 6
32234 +
32235 +/**
32236 + * Specifies the type of PHY interface to use. By default, the driver
32237 + * will automatically detect the phy_type.
32238 + *
32239 + * 0 - Full Speed PHY
32240 + * 1 - UTMI+ (default)
32241 + * 2 - ULPI
32242 + */
32243 +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
32244 +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
32245 +#define DWC_PHY_TYPE_PARAM_FS 0
32246 +#define DWC_PHY_TYPE_PARAM_UTMI 1
32247 +#define DWC_PHY_TYPE_PARAM_ULPI 2
32248 +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
32249 +
32250 +/**
32251 + * Specifies the UTMI+ Data Width. This parameter is
32252 + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
32253 + * PHY_TYPE, this parameter indicates the data width between
32254 + * the MAC and the ULPI Wrapper.) Also, this parameter is
32255 + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
32256 + * to "8 and 16 bits", meaning that the core has been
32257 + * configured to work at either data path width.
32258 + *
32259 + * 8 or 16 bits (default 16)
32260 + */
32261 +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
32262 + int32_t val);
32263 +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
32264 +//#define dwc_param_phy_utmi_width_default 16
32265 +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
32266 +
32267 +/**
32268 + * Specifies whether the ULPI operates at double or single
32269 + * data rate. This parameter is only applicable if PHY_TYPE is
32270 + * ULPI.
32271 + *
32272 + * 0 - single data rate ULPI interface with 8 bit wide data
32273 + * bus (default)
32274 + * 1 - double data rate ULPI interface with 4 bit wide data
32275 + * bus
32276 + */
32277 +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
32278 + int32_t val);
32279 +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
32280 +#define dwc_param_phy_ulpi_ddr_default 0
32281 +
32282 +/**
32283 + * Specifies whether to use the internal or external supply to
32284 + * drive the vbus with a ULPI phy.
32285 + */
32286 +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
32287 + int32_t val);
32288 +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
32289 +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
32290 +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
32291 +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
32292 +
32293 +/**
32294 + * Specifies whether to use the I2Cinterface for full speed PHY. This
32295 + * parameter is only applicable if PHY_TYPE is FS.
32296 + * 0 - No (default)
32297 + * 1 - Yes
32298 + */
32299 +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
32300 + int32_t val);
32301 +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
32302 +#define dwc_param_i2c_enable_default 0
32303 +
32304 +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
32305 + int32_t val);
32306 +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
32307 +#define dwc_param_ulpi_fs_ls_default 0
32308 +
32309 +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
32310 +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
32311 +#define dwc_param_ts_dline_default 0
32312 +
32313 +/**
32314 + * Specifies whether dedicated transmit FIFOs are
32315 + * enabled for non periodic IN endpoints in device mode
32316 + * 0 - No
32317 + * 1 - Yes
32318 + */
32319 +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
32320 + int32_t val);
32321 +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
32322 + core_if);
32323 +#define dwc_param_en_multiple_tx_fifo_default 1
32324 +
32325 +/** Number of 4-byte words in each of the Tx FIFOs in device
32326 + * mode when dynamic FIFO sizing is enabled.
32327 + * 4 to 768 (default 256)
32328 + */
32329 +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
32330 + int fifo_num, int32_t val);
32331 +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
32332 + int fifo_num);
32333 +#define dwc_param_dev_tx_fifo_size_default 768
32334 +
32335 +/** Thresholding enable flag-
32336 + * bit 0 - enable non-ISO Tx thresholding
32337 + * bit 1 - enable ISO Tx thresholding
32338 + * bit 2 - enable Rx thresholding
32339 + */
32340 +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
32341 +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
32342 +#define dwc_param_thr_ctl_default 0
32343 +
32344 +/** Thresholding length for Tx
32345 + * FIFOs in 32 bit DWORDs
32346 + */
32347 +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
32348 + int32_t val);
32349 +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
32350 +#define dwc_param_tx_thr_length_default 64
32351 +
32352 +/** Thresholding length for Rx
32353 + * FIFOs in 32 bit DWORDs
32354 + */
32355 +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
32356 + int32_t val);
32357 +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
32358 +#define dwc_param_rx_thr_length_default 64
32359 +
32360 +/**
32361 + * Specifies whether LPM (Link Power Management) support is enabled
32362 + */
32363 +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
32364 + int32_t val);
32365 +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
32366 +#define dwc_param_lpm_enable_default 1
32367 +
32368 +/**
32369 + * Specifies whether PTI enhancement is enabled
32370 + */
32371 +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
32372 + int32_t val);
32373 +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
32374 +#define dwc_param_pti_enable_default 0
32375 +
32376 +/**
32377 + * Specifies whether MPI enhancement is enabled
32378 + */
32379 +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
32380 + int32_t val);
32381 +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
32382 +#define dwc_param_mpi_enable_default 0
32383 +
32384 +/**
32385 + * Specifies whether ADP capability is enabled
32386 + */
32387 +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
32388 + int32_t val);
32389 +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
32390 +#define dwc_param_adp_enable_default 0
32391 +
32392 +/**
32393 + * Specifies whether IC_USB capability is enabled
32394 + */
32395 +
32396 +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
32397 + int32_t val);
32398 +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
32399 +#define dwc_param_ic_usb_cap_default 0
32400 +
32401 +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
32402 + int32_t val);
32403 +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
32404 +#define dwc_param_ahb_thr_ratio_default 0
32405 +
32406 +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
32407 + int32_t val);
32408 +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
32409 +#define dwc_param_power_down_default 0
32410 +
32411 +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
32412 + int32_t val);
32413 +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
32414 +#define dwc_param_reload_ctl_default 0
32415 +
32416 +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
32417 + int32_t val);
32418 +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
32419 +#define dwc_param_dev_out_nak_default 0
32420 +
32421 +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
32422 + int32_t val);
32423 +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
32424 +#define dwc_param_cont_on_bna_default 0
32425 +
32426 +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
32427 + int32_t val);
32428 +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
32429 +#define dwc_param_ahb_single_default 0
32430 +
32431 +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
32432 +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
32433 +#define dwc_param_otg_ver_default 0
32434 +
32435 +/** @} */
32436 +
32437 +/** @name Access to registers and bit-fields */
32438 +
32439 +/**
32440 + * Dump core registers and SPRAM
32441 + */
32442 +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
32443 +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
32444 +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
32445 +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
32446 +
32447 +/**
32448 + * Get host negotiation status.
32449 + */
32450 +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
32451 +
32452 +/**
32453 + * Get srp status
32454 + */
32455 +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
32456 +
32457 +/**
32458 + * Set hnpreq bit in the GOTGCTL register.
32459 + */
32460 +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
32461 +
32462 +/**
32463 + * Get Content of SNPSID register.
32464 + */
32465 +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
32466 +
32467 +/**
32468 + * Get current mode.
32469 + * Returns 0 if in device mode, and 1 if in host mode.
32470 + */
32471 +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
32472 +
32473 +/**
32474 + * Get value of hnpcapable field in the GUSBCFG register
32475 + */
32476 +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
32477 +/**
32478 + * Set value of hnpcapable field in the GUSBCFG register
32479 + */
32480 +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
32481 +
32482 +/**
32483 + * Get value of srpcapable field in the GUSBCFG register
32484 + */
32485 +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
32486 +/**
32487 + * Set value of srpcapable field in the GUSBCFG register
32488 + */
32489 +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
32490 +
32491 +/**
32492 + * Get value of devspeed field in the DCFG register
32493 + */
32494 +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
32495 +/**
32496 + * Set value of devspeed field in the DCFG register
32497 + */
32498 +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
32499 +
32500 +/**
32501 + * Get the value of busconnected field from the HPRT0 register
32502 + */
32503 +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
32504 +
32505 +/**
32506 + * Gets the device enumeration Speed.
32507 + */
32508 +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
32509 +
32510 +/**
32511 + * Get value of prtpwr field from the HPRT0 register
32512 + */
32513 +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
32514 +
32515 +/**
32516 + * Get value of flag indicating core state - hibernated or not
32517 + */
32518 +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
32519 +
32520 +/**
32521 + * Set value of prtpwr field from the HPRT0 register
32522 + */
32523 +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
32524 +
32525 +/**
32526 + * Get value of prtsusp field from the HPRT0 regsiter
32527 + */
32528 +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
32529 +/**
32530 + * Set value of prtpwr field from the HPRT0 register
32531 + */
32532 +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
32533 +
32534 +/**
32535 + * Get value of ModeChTimEn field from the HCFG regsiter
32536 + */
32537 +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
32538 +/**
32539 + * Set value of ModeChTimEn field from the HCFG regsiter
32540 + */
32541 +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
32542 +
32543 +/**
32544 + * Get value of Fram Interval field from the HFIR regsiter
32545 + */
32546 +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
32547 +/**
32548 + * Set value of Frame Interval field from the HFIR regsiter
32549 + */
32550 +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
32551 +
32552 +/**
32553 + * Set value of prtres field from the HPRT0 register
32554 + *FIXME Remove?
32555 + */
32556 +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
32557 +
32558 +/**
32559 + * Get value of rmtwkupsig bit in DCTL register
32560 + */
32561 +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
32562 +
32563 +/**
32564 + * Get value of prt_sleep_sts field from the GLPMCFG register
32565 + */
32566 +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
32567 +
32568 +/**
32569 + * Get value of rem_wkup_en field from the GLPMCFG register
32570 + */
32571 +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
32572 +
32573 +/**
32574 + * Get value of appl_resp field from the GLPMCFG register
32575 + */
32576 +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
32577 +/**
32578 + * Set value of appl_resp field from the GLPMCFG register
32579 + */
32580 +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
32581 +
32582 +/**
32583 + * Get value of hsic_connect field from the GLPMCFG register
32584 + */
32585 +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
32586 +/**
32587 + * Set value of hsic_connect field from the GLPMCFG register
32588 + */
32589 +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
32590 +
32591 +/**
32592 + * Get value of inv_sel_hsic field from the GLPMCFG register.
32593 + */
32594 +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
32595 +/**
32596 + * Set value of inv_sel_hsic field from the GLPMFG register.
32597 + */
32598 +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
32599 +
32600 +/*
32601 + * Some functions for accessing registers
32602 + */
32603 +
32604 +/**
32605 + * GOTGCTL register
32606 + */
32607 +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
32608 +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
32609 +
32610 +/**
32611 + * GUSBCFG register
32612 + */
32613 +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
32614 +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
32615 +
32616 +/**
32617 + * GRXFSIZ register
32618 + */
32619 +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
32620 +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
32621 +
32622 +/**
32623 + * GNPTXFSIZ register
32624 + */
32625 +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
32626 +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
32627 +
32628 +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
32629 +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
32630 +
32631 +/**
32632 + * GGPIO register
32633 + */
32634 +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
32635 +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
32636 +
32637 +/**
32638 + * GUID register
32639 + */
32640 +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
32641 +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
32642 +
32643 +/**
32644 + * HPRT0 register
32645 + */
32646 +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
32647 +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
32648 +
32649 +/**
32650 + * GHPTXFSIZE
32651 + */
32652 +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
32653 +
32654 +/** @} */
32655 +
32656 +#endif /* __DWC_CORE_IF_H__ */
32657 --- /dev/null
32658 +++ b/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
32659 @@ -0,0 +1,117 @@
32660 +/* ==========================================================================
32661 + *
32662 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
32663 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
32664 + * otherwise expressly agreed to in writing between Synopsys and you.
32665 + *
32666 + * The Software IS NOT an item of Licensed Software or Licensed Product under
32667 + * any End User Software License Agreement or Agreement for Licensed Product
32668 + * with Synopsys or any supplement thereto. You are permitted to use and
32669 + * redistribute this Software in source and binary forms, with or without
32670 + * modification, provided that redistributions of source code must retain this
32671 + * notice. You may not view, use, disclose, copy or distribute this file or
32672 + * any information contained herein except pursuant to this license grant from
32673 + * Synopsys. If you do not agree with this notice, including the disclaimer
32674 + * below, then you are not authorized to use the Software.
32675 + *
32676 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
32677 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32678 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32679 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
32680 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32681 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32682 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32683 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32684 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32685 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32686 + * DAMAGE.
32687 + * ========================================================================== */
32688 +
32689 +#ifndef __DWC_OTG_DBG_H__
32690 +#define __DWC_OTG_DBG_H__
32691 +
32692 +/** @file
32693 + * This file defines debug levels.
32694 + * Debugging support vanishes in non-debug builds.
32695 + */
32696 +
32697 +/**
32698 + * The Debug Level bit-mask variable.
32699 + */
32700 +extern uint32_t g_dbg_lvl;
32701 +/**
32702 + * Set the Debug Level variable.
32703 + */
32704 +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
32705 +{
32706 + uint32_t old = g_dbg_lvl;
32707 + g_dbg_lvl = new;
32708 + return old;
32709 +}
32710 +
32711 +#define DBG_USER (0x1)
32712 +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
32713 +#define DBG_CIL (0x2)
32714 +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
32715 + * messages */
32716 +#define DBG_CILV (0x20)
32717 +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
32718 + * messages */
32719 +#define DBG_PCD (0x4)
32720 +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
32721 + * messages */
32722 +#define DBG_PCDV (0x40)
32723 +/** When debug level has the DBG_HCD bit set, display Host debug messages */
32724 +#define DBG_HCD (0x8)
32725 +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
32726 + * messages */
32727 +#define DBG_HCDV (0x80)
32728 +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
32729 + * mode. */
32730 +#define DBG_HCD_URB (0x800)
32731 +/** When debug level has the DBG_HCDI bit set, display host interrupt
32732 + * messages. */
32733 +#define DBG_HCDI (0x1000)
32734 +
32735 +/** When debug level has any bit set, display debug messages */
32736 +#define DBG_ANY (0xFF)
32737 +
32738 +/** All debug messages off */
32739 +#define DBG_OFF 0
32740 +
32741 +/** Prefix string for DWC_DEBUG print macros. */
32742 +#define USB_DWC "DWC_otg: "
32743 +
32744 +/**
32745 + * Print a debug message when the Global debug level variable contains
32746 + * the bit defined in <code>lvl</code>.
32747 + *
32748 + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
32749 + * @param[in] x - like printf
32750 + *
32751 + * Example:<p>
32752 + * <code>
32753 + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
32754 + * </code>
32755 + * <br>
32756 + * results in:<br>
32757 + * <code>
32758 + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
32759 + * </code>
32760 + */
32761 +#ifdef DEBUG
32762 +
32763 +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
32764 +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
32765 +
32766 +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
32767 +
32768 +#else
32769 +
32770 +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
32771 +# define DWC_DEBUGP(x...)
32772 +
32773 +# define CHK_DEBUG_LEVEL(level) (0)
32774 +
32775 +#endif /*DEBUG*/
32776 +#endif
32777 --- /dev/null
32778 +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.c
32779 @@ -0,0 +1,1767 @@
32780 +/* ==========================================================================
32781 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
32782 + * $Revision: #92 $
32783 + * $Date: 2012/08/10 $
32784 + * $Change: 2047372 $
32785 + *
32786 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
32787 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
32788 + * otherwise expressly agreed to in writing between Synopsys and you.
32789 + *
32790 + * The Software IS NOT an item of Licensed Software or Licensed Product under
32791 + * any End User Software License Agreement or Agreement for Licensed Product
32792 + * with Synopsys or any supplement thereto. You are permitted to use and
32793 + * redistribute this Software in source and binary forms, with or without
32794 + * modification, provided that redistributions of source code must retain this
32795 + * notice. You may not view, use, disclose, copy or distribute this file or
32796 + * any information contained herein except pursuant to this license grant from
32797 + * Synopsys. If you do not agree with this notice, including the disclaimer
32798 + * below, then you are not authorized to use the Software.
32799 + *
32800 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
32801 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32802 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32803 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
32804 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32805 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32806 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32807 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32808 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32809 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32810 + * DAMAGE.
32811 + * ========================================================================== */
32812 +
32813 +/** @file
32814 + * The dwc_otg_driver module provides the initialization and cleanup entry
32815 + * points for the DWC_otg driver. This module will be dynamically installed
32816 + * after Linux is booted using the insmod command. When the module is
32817 + * installed, the dwc_otg_driver_init function is called. When the module is
32818 + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
32819 + *
32820 + * This module also defines a data structure for the dwc_otg_driver, which is
32821 + * used in conjunction with the standard ARM lm_device structure. These
32822 + * structures allow the OTG driver to comply with the standard Linux driver
32823 + * model in which devices and drivers are registered with a bus driver. This
32824 + * has the benefit that Linux can expose attributes of the driver and device
32825 + * in its special sysfs file system. Users can then read or write files in
32826 + * this file system to perform diagnostics on the driver components or the
32827 + * device.
32828 + */
32829 +
32830 +#include "dwc_otg_os_dep.h"
32831 +#include "dwc_os.h"
32832 +#include "dwc_otg_dbg.h"
32833 +#include "dwc_otg_driver.h"
32834 +#include "dwc_otg_attr.h"
32835 +#include "dwc_otg_core_if.h"
32836 +#include "dwc_otg_pcd_if.h"
32837 +#include "dwc_otg_hcd_if.h"
32838 +#include "dwc_otg_fiq_fsm.h"
32839 +
32840 +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
32841 +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
32842 +
32843 +bool microframe_schedule=true;
32844 +
32845 +static const char dwc_driver_name[] = "dwc_otg";
32846 +
32847 +
32848 +extern int pcd_init(
32849 +#ifdef LM_INTERFACE
32850 + struct lm_device *_dev
32851 +#elif defined(PCI_INTERFACE)
32852 + struct pci_dev *_dev
32853 +#elif defined(PLATFORM_INTERFACE)
32854 + struct platform_device *dev
32855 +#endif
32856 + );
32857 +extern int hcd_init(
32858 +#ifdef LM_INTERFACE
32859 + struct lm_device *_dev
32860 +#elif defined(PCI_INTERFACE)
32861 + struct pci_dev *_dev
32862 +#elif defined(PLATFORM_INTERFACE)
32863 + struct platform_device *dev
32864 +#endif
32865 + );
32866 +
32867 +extern int pcd_remove(
32868 +#ifdef LM_INTERFACE
32869 + struct lm_device *_dev
32870 +#elif defined(PCI_INTERFACE)
32871 + struct pci_dev *_dev
32872 +#elif defined(PLATFORM_INTERFACE)
32873 + struct platform_device *_dev
32874 +#endif
32875 + );
32876 +
32877 +extern void hcd_remove(
32878 +#ifdef LM_INTERFACE
32879 + struct lm_device *_dev
32880 +#elif defined(PCI_INTERFACE)
32881 + struct pci_dev *_dev
32882 +#elif defined(PLATFORM_INTERFACE)
32883 + struct platform_device *_dev
32884 +#endif
32885 + );
32886 +
32887 +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
32888 +
32889 +/*-------------------------------------------------------------------------*/
32890 +/* Encapsulate the module parameter settings */
32891 +
32892 +struct dwc_otg_driver_module_params {
32893 + int32_t opt;
32894 + int32_t otg_cap;
32895 + int32_t dma_enable;
32896 + int32_t dma_desc_enable;
32897 + int32_t dma_burst_size;
32898 + int32_t speed;
32899 + int32_t host_support_fs_ls_low_power;
32900 + int32_t host_ls_low_power_phy_clk;
32901 + int32_t enable_dynamic_fifo;
32902 + int32_t data_fifo_size;
32903 + int32_t dev_rx_fifo_size;
32904 + int32_t dev_nperio_tx_fifo_size;
32905 + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
32906 + int32_t host_rx_fifo_size;
32907 + int32_t host_nperio_tx_fifo_size;
32908 + int32_t host_perio_tx_fifo_size;
32909 + int32_t max_transfer_size;
32910 + int32_t max_packet_count;
32911 + int32_t host_channels;
32912 + int32_t dev_endpoints;
32913 + int32_t phy_type;
32914 + int32_t phy_utmi_width;
32915 + int32_t phy_ulpi_ddr;
32916 + int32_t phy_ulpi_ext_vbus;
32917 + int32_t i2c_enable;
32918 + int32_t ulpi_fs_ls;
32919 + int32_t ts_dline;
32920 + int32_t en_multiple_tx_fifo;
32921 + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
32922 + uint32_t thr_ctl;
32923 + uint32_t tx_thr_length;
32924 + uint32_t rx_thr_length;
32925 + int32_t pti_enable;
32926 + int32_t mpi_enable;
32927 + int32_t lpm_enable;
32928 + int32_t ic_usb_cap;
32929 + int32_t ahb_thr_ratio;
32930 + int32_t power_down;
32931 + int32_t reload_ctl;
32932 + int32_t dev_out_nak;
32933 + int32_t cont_on_bna;
32934 + int32_t ahb_single;
32935 + int32_t otg_ver;
32936 + int32_t adp_enable;
32937 +};
32938 +
32939 +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
32940 + .opt = -1,
32941 + .otg_cap = -1,
32942 + .dma_enable = -1,
32943 + .dma_desc_enable = -1,
32944 + .dma_burst_size = -1,
32945 + .speed = -1,
32946 + .host_support_fs_ls_low_power = -1,
32947 + .host_ls_low_power_phy_clk = -1,
32948 + .enable_dynamic_fifo = -1,
32949 + .data_fifo_size = -1,
32950 + .dev_rx_fifo_size = -1,
32951 + .dev_nperio_tx_fifo_size = -1,
32952 + .dev_perio_tx_fifo_size = {
32953 + /* dev_perio_tx_fifo_size_1 */
32954 + -1,
32955 + -1,
32956 + -1,
32957 + -1,
32958 + -1,
32959 + -1,
32960 + -1,
32961 + -1,
32962 + -1,
32963 + -1,
32964 + -1,
32965 + -1,
32966 + -1,
32967 + -1,
32968 + -1
32969 + /* 15 */
32970 + },
32971 + .host_rx_fifo_size = -1,
32972 + .host_nperio_tx_fifo_size = -1,
32973 + .host_perio_tx_fifo_size = -1,
32974 + .max_transfer_size = -1,
32975 + .max_packet_count = -1,
32976 + .host_channels = -1,
32977 + .dev_endpoints = -1,
32978 + .phy_type = -1,
32979 + .phy_utmi_width = -1,
32980 + .phy_ulpi_ddr = -1,
32981 + .phy_ulpi_ext_vbus = -1,
32982 + .i2c_enable = -1,
32983 + .ulpi_fs_ls = -1,
32984 + .ts_dline = -1,
32985 + .en_multiple_tx_fifo = -1,
32986 + .dev_tx_fifo_size = {
32987 + /* dev_tx_fifo_size */
32988 + -1,
32989 + -1,
32990 + -1,
32991 + -1,
32992 + -1,
32993 + -1,
32994 + -1,
32995 + -1,
32996 + -1,
32997 + -1,
32998 + -1,
32999 + -1,
33000 + -1,
33001 + -1,
33002 + -1
33003 + /* 15 */
33004 + },
33005 + .thr_ctl = -1,
33006 + .tx_thr_length = -1,
33007 + .rx_thr_length = -1,
33008 + .pti_enable = -1,
33009 + .mpi_enable = -1,
33010 + .lpm_enable = 0,
33011 + .ic_usb_cap = -1,
33012 + .ahb_thr_ratio = -1,
33013 + .power_down = -1,
33014 + .reload_ctl = -1,
33015 + .dev_out_nak = -1,
33016 + .cont_on_bna = -1,
33017 + .ahb_single = -1,
33018 + .otg_ver = -1,
33019 + .adp_enable = -1,
33020 +};
33021 +
33022 +//Global variable to switch the fiq fix on or off
33023 +bool fiq_enable = 1;
33024 +// Global variable to enable the split transaction fix
33025 +bool fiq_fsm_enable = true;
33026 +//Bulk split-transaction NAK holdoff in microframes
33027 +uint16_t nak_holdoff = 8;
33028 +
33029 +//Force host mode during CIL re-init
33030 +bool cil_force_host = true;
33031 +
33032 +unsigned short fiq_fsm_mask = 0x0F;
33033 +
33034 +unsigned short int_ep_interval_min = 0;
33035 +/**
33036 + * This function shows the Driver Version.
33037 + */
33038 +static ssize_t version_show(struct device_driver *dev, char *buf)
33039 +{
33040 + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
33041 + DWC_DRIVER_VERSION);
33042 +}
33043 +
33044 +static DRIVER_ATTR_RO(version);
33045 +
33046 +/**
33047 + * Global Debug Level Mask.
33048 + */
33049 +uint32_t g_dbg_lvl = 0; /* OFF */
33050 +
33051 +/**
33052 + * This function shows the driver Debug Level.
33053 + */
33054 +static ssize_t debuglevel_show(struct device_driver *drv, char *buf)
33055 +{
33056 + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
33057 +}
33058 +
33059 +/**
33060 + * This function stores the driver Debug Level.
33061 + */
33062 +static ssize_t debuglevel_store(struct device_driver *drv, const char *buf,
33063 + size_t count)
33064 +{
33065 + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
33066 + return count;
33067 +}
33068 +
33069 +static DRIVER_ATTR_RW(debuglevel);
33070 +
33071 +/**
33072 + * This function is called during module intialization
33073 + * to pass module parameters to the DWC_OTG CORE.
33074 + */
33075 +static int set_parameters(dwc_otg_core_if_t * core_if)
33076 +{
33077 + int retval = 0;
33078 + int i;
33079 +
33080 + if (dwc_otg_module_params.otg_cap != -1) {
33081 + retval +=
33082 + dwc_otg_set_param_otg_cap(core_if,
33083 + dwc_otg_module_params.otg_cap);
33084 + }
33085 + if (dwc_otg_module_params.dma_enable != -1) {
33086 + retval +=
33087 + dwc_otg_set_param_dma_enable(core_if,
33088 + dwc_otg_module_params.
33089 + dma_enable);
33090 + }
33091 + if (dwc_otg_module_params.dma_desc_enable != -1) {
33092 + retval +=
33093 + dwc_otg_set_param_dma_desc_enable(core_if,
33094 + dwc_otg_module_params.
33095 + dma_desc_enable);
33096 + }
33097 + if (dwc_otg_module_params.opt != -1) {
33098 + retval +=
33099 + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
33100 + }
33101 + if (dwc_otg_module_params.dma_burst_size != -1) {
33102 + retval +=
33103 + dwc_otg_set_param_dma_burst_size(core_if,
33104 + dwc_otg_module_params.
33105 + dma_burst_size);
33106 + }
33107 + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
33108 + retval +=
33109 + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
33110 + dwc_otg_module_params.
33111 + host_support_fs_ls_low_power);
33112 + }
33113 + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
33114 + retval +=
33115 + dwc_otg_set_param_enable_dynamic_fifo(core_if,
33116 + dwc_otg_module_params.
33117 + enable_dynamic_fifo);
33118 + }
33119 + if (dwc_otg_module_params.data_fifo_size != -1) {
33120 + retval +=
33121 + dwc_otg_set_param_data_fifo_size(core_if,
33122 + dwc_otg_module_params.
33123 + data_fifo_size);
33124 + }
33125 + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
33126 + retval +=
33127 + dwc_otg_set_param_dev_rx_fifo_size(core_if,
33128 + dwc_otg_module_params.
33129 + dev_rx_fifo_size);
33130 + }
33131 + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
33132 + retval +=
33133 + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
33134 + dwc_otg_module_params.
33135 + dev_nperio_tx_fifo_size);
33136 + }
33137 + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
33138 + retval +=
33139 + dwc_otg_set_param_host_rx_fifo_size(core_if,
33140 + dwc_otg_module_params.host_rx_fifo_size);
33141 + }
33142 + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
33143 + retval +=
33144 + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
33145 + dwc_otg_module_params.
33146 + host_nperio_tx_fifo_size);
33147 + }
33148 + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
33149 + retval +=
33150 + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
33151 + dwc_otg_module_params.
33152 + host_perio_tx_fifo_size);
33153 + }
33154 + if (dwc_otg_module_params.max_transfer_size != -1) {
33155 + retval +=
33156 + dwc_otg_set_param_max_transfer_size(core_if,
33157 + dwc_otg_module_params.
33158 + max_transfer_size);
33159 + }
33160 + if (dwc_otg_module_params.max_packet_count != -1) {
33161 + retval +=
33162 + dwc_otg_set_param_max_packet_count(core_if,
33163 + dwc_otg_module_params.
33164 + max_packet_count);
33165 + }
33166 + if (dwc_otg_module_params.host_channels != -1) {
33167 + retval +=
33168 + dwc_otg_set_param_host_channels(core_if,
33169 + dwc_otg_module_params.
33170 + host_channels);
33171 + }
33172 + if (dwc_otg_module_params.dev_endpoints != -1) {
33173 + retval +=
33174 + dwc_otg_set_param_dev_endpoints(core_if,
33175 + dwc_otg_module_params.
33176 + dev_endpoints);
33177 + }
33178 + if (dwc_otg_module_params.phy_type != -1) {
33179 + retval +=
33180 + dwc_otg_set_param_phy_type(core_if,
33181 + dwc_otg_module_params.phy_type);
33182 + }
33183 + if (dwc_otg_module_params.speed != -1) {
33184 + retval +=
33185 + dwc_otg_set_param_speed(core_if,
33186 + dwc_otg_module_params.speed);
33187 + }
33188 + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
33189 + retval +=
33190 + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
33191 + dwc_otg_module_params.
33192 + host_ls_low_power_phy_clk);
33193 + }
33194 + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
33195 + retval +=
33196 + dwc_otg_set_param_phy_ulpi_ddr(core_if,
33197 + dwc_otg_module_params.
33198 + phy_ulpi_ddr);
33199 + }
33200 + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
33201 + retval +=
33202 + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
33203 + dwc_otg_module_params.
33204 + phy_ulpi_ext_vbus);
33205 + }
33206 + if (dwc_otg_module_params.phy_utmi_width != -1) {
33207 + retval +=
33208 + dwc_otg_set_param_phy_utmi_width(core_if,
33209 + dwc_otg_module_params.
33210 + phy_utmi_width);
33211 + }
33212 + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
33213 + retval +=
33214 + dwc_otg_set_param_ulpi_fs_ls(core_if,
33215 + dwc_otg_module_params.ulpi_fs_ls);
33216 + }
33217 + if (dwc_otg_module_params.ts_dline != -1) {
33218 + retval +=
33219 + dwc_otg_set_param_ts_dline(core_if,
33220 + dwc_otg_module_params.ts_dline);
33221 + }
33222 + if (dwc_otg_module_params.i2c_enable != -1) {
33223 + retval +=
33224 + dwc_otg_set_param_i2c_enable(core_if,
33225 + dwc_otg_module_params.
33226 + i2c_enable);
33227 + }
33228 + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
33229 + retval +=
33230 + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
33231 + dwc_otg_module_params.
33232 + en_multiple_tx_fifo);
33233 + }
33234 + for (i = 0; i < 15; i++) {
33235 + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
33236 + retval +=
33237 + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
33238 + dwc_otg_module_params.
33239 + dev_perio_tx_fifo_size
33240 + [i], i);
33241 + }
33242 + }
33243 +
33244 + for (i = 0; i < 15; i++) {
33245 + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
33246 + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
33247 + dwc_otg_module_params.
33248 + dev_tx_fifo_size
33249 + [i], i);
33250 + }
33251 + }
33252 + if (dwc_otg_module_params.thr_ctl != -1) {
33253 + retval +=
33254 + dwc_otg_set_param_thr_ctl(core_if,
33255 + dwc_otg_module_params.thr_ctl);
33256 + }
33257 + if (dwc_otg_module_params.mpi_enable != -1) {
33258 + retval +=
33259 + dwc_otg_set_param_mpi_enable(core_if,
33260 + dwc_otg_module_params.
33261 + mpi_enable);
33262 + }
33263 + if (dwc_otg_module_params.pti_enable != -1) {
33264 + retval +=
33265 + dwc_otg_set_param_pti_enable(core_if,
33266 + dwc_otg_module_params.
33267 + pti_enable);
33268 + }
33269 + if (dwc_otg_module_params.lpm_enable != -1) {
33270 + retval +=
33271 + dwc_otg_set_param_lpm_enable(core_if,
33272 + dwc_otg_module_params.
33273 + lpm_enable);
33274 + }
33275 + if (dwc_otg_module_params.ic_usb_cap != -1) {
33276 + retval +=
33277 + dwc_otg_set_param_ic_usb_cap(core_if,
33278 + dwc_otg_module_params.
33279 + ic_usb_cap);
33280 + }
33281 + if (dwc_otg_module_params.tx_thr_length != -1) {
33282 + retval +=
33283 + dwc_otg_set_param_tx_thr_length(core_if,
33284 + dwc_otg_module_params.tx_thr_length);
33285 + }
33286 + if (dwc_otg_module_params.rx_thr_length != -1) {
33287 + retval +=
33288 + dwc_otg_set_param_rx_thr_length(core_if,
33289 + dwc_otg_module_params.
33290 + rx_thr_length);
33291 + }
33292 + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
33293 + retval +=
33294 + dwc_otg_set_param_ahb_thr_ratio(core_if,
33295 + dwc_otg_module_params.ahb_thr_ratio);
33296 + }
33297 + if (dwc_otg_module_params.power_down != -1) {
33298 + retval +=
33299 + dwc_otg_set_param_power_down(core_if,
33300 + dwc_otg_module_params.power_down);
33301 + }
33302 + if (dwc_otg_module_params.reload_ctl != -1) {
33303 + retval +=
33304 + dwc_otg_set_param_reload_ctl(core_if,
33305 + dwc_otg_module_params.reload_ctl);
33306 + }
33307 +
33308 + if (dwc_otg_module_params.dev_out_nak != -1) {
33309 + retval +=
33310 + dwc_otg_set_param_dev_out_nak(core_if,
33311 + dwc_otg_module_params.dev_out_nak);
33312 + }
33313 +
33314 + if (dwc_otg_module_params.cont_on_bna != -1) {
33315 + retval +=
33316 + dwc_otg_set_param_cont_on_bna(core_if,
33317 + dwc_otg_module_params.cont_on_bna);
33318 + }
33319 +
33320 + if (dwc_otg_module_params.ahb_single != -1) {
33321 + retval +=
33322 + dwc_otg_set_param_ahb_single(core_if,
33323 + dwc_otg_module_params.ahb_single);
33324 + }
33325 +
33326 + if (dwc_otg_module_params.otg_ver != -1) {
33327 + retval +=
33328 + dwc_otg_set_param_otg_ver(core_if,
33329 + dwc_otg_module_params.otg_ver);
33330 + }
33331 + if (dwc_otg_module_params.adp_enable != -1) {
33332 + retval +=
33333 + dwc_otg_set_param_adp_enable(core_if,
33334 + dwc_otg_module_params.
33335 + adp_enable);
33336 + }
33337 + return retval;
33338 +}
33339 +
33340 +/**
33341 + * This function is the top level interrupt handler for the Common
33342 + * (Device and host modes) interrupts.
33343 + */
33344 +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
33345 +{
33346 + int32_t retval = IRQ_NONE;
33347 +
33348 + retval = dwc_otg_handle_common_intr(dev);
33349 + if (retval != 0) {
33350 + S3C2410X_CLEAR_EINTPEND();
33351 + }
33352 + return IRQ_RETVAL(retval);
33353 +}
33354 +
33355 +/**
33356 + * This function is called when a lm_device is unregistered with the
33357 + * dwc_otg_driver. This happens, for example, when the rmmod command is
33358 + * executed. The device may or may not be electrically present. If it is
33359 + * present, the driver stops device processing. Any resources used on behalf
33360 + * of this device are freed.
33361 + *
33362 + * @param _dev
33363 + */
33364 +#ifdef LM_INTERFACE
33365 +#define REM_RETVAL(n)
33366 +static void dwc_otg_driver_remove( struct lm_device *_dev )
33367 +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
33368 +#elif defined(PCI_INTERFACE)
33369 +#define REM_RETVAL(n)
33370 +static void dwc_otg_driver_remove( struct pci_dev *_dev )
33371 +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
33372 +#elif defined(PLATFORM_INTERFACE)
33373 +#define REM_RETVAL(n) n
33374 +static int dwc_otg_driver_remove( struct platform_device *_dev )
33375 +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
33376 +#endif
33377 +
33378 + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
33379 +
33380 + if (!otg_dev) {
33381 + /* Memory allocation for the dwc_otg_device failed. */
33382 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
33383 + return REM_RETVAL(-ENOMEM);
33384 + }
33385 +#ifndef DWC_DEVICE_ONLY
33386 + if (otg_dev->hcd) {
33387 + hcd_remove(_dev);
33388 + } else {
33389 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
33390 + return REM_RETVAL(-EINVAL);
33391 + }
33392 +#endif
33393 +
33394 +#ifndef DWC_HOST_ONLY
33395 + if (otg_dev->pcd) {
33396 + pcd_remove(_dev);
33397 + } else {
33398 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
33399 + return REM_RETVAL(-EINVAL);
33400 + }
33401 +#endif
33402 + /*
33403 + * Free the IRQ
33404 + */
33405 + if (otg_dev->common_irq_installed) {
33406 +#ifdef PLATFORM_INTERFACE
33407 + free_irq(platform_get_irq(_dev, 0), otg_dev);
33408 +#else
33409 + free_irq(_dev->irq, otg_dev);
33410 +#endif
33411 + } else {
33412 + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
33413 + return REM_RETVAL(-ENXIO);
33414 + }
33415 +
33416 + if (otg_dev->core_if) {
33417 + dwc_otg_cil_remove(otg_dev->core_if);
33418 + } else {
33419 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
33420 + return REM_RETVAL(-ENXIO);
33421 + }
33422 +
33423 + /*
33424 + * Remove the device attributes
33425 + */
33426 + dwc_otg_attr_remove(_dev);
33427 +
33428 + /*
33429 + * Return the memory.
33430 + */
33431 + if (otg_dev->os_dep.base) {
33432 + iounmap(otg_dev->os_dep.base);
33433 + }
33434 + DWC_FREE(otg_dev);
33435 +
33436 + /*
33437 + * Clear the drvdata pointer.
33438 + */
33439 +#ifdef LM_INTERFACE
33440 + lm_set_drvdata(_dev, 0);
33441 +#elif defined(PCI_INTERFACE)
33442 + release_mem_region(otg_dev->os_dep.rsrc_start,
33443 + otg_dev->os_dep.rsrc_len);
33444 + pci_set_drvdata(_dev, 0);
33445 +#elif defined(PLATFORM_INTERFACE)
33446 + platform_set_drvdata(_dev, 0);
33447 +#endif
33448 + return REM_RETVAL(0);
33449 +}
33450 +
33451 +/**
33452 + * This function is called when an lm_device is bound to a
33453 + * dwc_otg_driver. It creates the driver components required to
33454 + * control the device (CIL, HCD, and PCD) and it initializes the
33455 + * device. The driver components are stored in a dwc_otg_device
33456 + * structure. A reference to the dwc_otg_device is saved in the
33457 + * lm_device. This allows the driver to access the dwc_otg_device
33458 + * structure on subsequent calls to driver methods for this device.
33459 + *
33460 + * @param _dev Bus device
33461 + */
33462 +static int dwc_otg_driver_probe(
33463 +#ifdef LM_INTERFACE
33464 + struct lm_device *_dev
33465 +#elif defined(PCI_INTERFACE)
33466 + struct pci_dev *_dev,
33467 + const struct pci_device_id *id
33468 +#elif defined(PLATFORM_INTERFACE)
33469 + struct platform_device *_dev
33470 +#endif
33471 + )
33472 +{
33473 + int retval = 0;
33474 + dwc_otg_device_t *dwc_otg_device;
33475 + int devirq;
33476 +
33477 + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
33478 +#ifdef LM_INTERFACE
33479 + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
33480 +#elif defined(PCI_INTERFACE)
33481 + if (!id) {
33482 + DWC_ERROR("Invalid pci_device_id %p", id);
33483 + return -EINVAL;
33484 + }
33485 +
33486 + if (!_dev || (pci_enable_device(_dev) < 0)) {
33487 + DWC_ERROR("Invalid pci_device %p", _dev);
33488 + return -ENODEV;
33489 + }
33490 + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
33491 + /* other stuff needed as well? */
33492 +
33493 +#elif defined(PLATFORM_INTERFACE)
33494 + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
33495 + (unsigned)_dev->resource->start,
33496 + (unsigned)(_dev->resource->end - _dev->resource->start));
33497 +#endif
33498 +
33499 + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
33500 +
33501 + if (!dwc_otg_device) {
33502 + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
33503 + return -ENOMEM;
33504 + }
33505 +
33506 + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
33507 + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
33508 + dwc_otg_device->os_dep.platformdev = _dev;
33509 +
33510 + /*
33511 + * Map the DWC_otg Core memory into virtual address space.
33512 + */
33513 +#ifdef LM_INTERFACE
33514 + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
33515 +
33516 + if (!dwc_otg_device->os_dep.base) {
33517 + dev_err(&_dev->dev, "ioremap() failed\n");
33518 + DWC_FREE(dwc_otg_device);
33519 + return -ENOMEM;
33520 + }
33521 + dev_dbg(&_dev->dev, "base=0x%08x\n",
33522 + (unsigned)dwc_otg_device->os_dep.base);
33523 +#elif defined(PCI_INTERFACE)
33524 + _dev->current_state = PCI_D0;
33525 + _dev->dev.power.power_state = PMSG_ON;
33526 +
33527 + if (!_dev->irq) {
33528 + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
33529 + pci_name(_dev));
33530 + iounmap(dwc_otg_device->os_dep.base);
33531 + DWC_FREE(dwc_otg_device);
33532 + return -ENODEV;
33533 + }
33534 +
33535 + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
33536 + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
33537 + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
33538 + (unsigned)dwc_otg_device->os_dep.rsrc_start,
33539 + (unsigned)dwc_otg_device->os_dep.rsrc_len);
33540 + if (!request_mem_region
33541 + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
33542 + "dwc_otg")) {
33543 + dev_dbg(&_dev->dev, "error requesting memory\n");
33544 + iounmap(dwc_otg_device->os_dep.base);
33545 + DWC_FREE(dwc_otg_device);
33546 + return -EFAULT;
33547 + }
33548 +
33549 + dwc_otg_device->os_dep.base =
33550 + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
33551 + dwc_otg_device->os_dep.rsrc_len);
33552 + if (dwc_otg_device->os_dep.base == NULL) {
33553 + dev_dbg(&_dev->dev, "error mapping memory\n");
33554 + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
33555 + dwc_otg_device->os_dep.rsrc_len);
33556 + iounmap(dwc_otg_device->os_dep.base);
33557 + DWC_FREE(dwc_otg_device);
33558 + return -EFAULT;
33559 + }
33560 + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
33561 + dwc_otg_device->os_dep.base);
33562 + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
33563 + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
33564 + dwc_otg_device->os_dep.base);
33565 + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
33566 + (unsigned)dwc_otg_device->os_dep.rsrc_start,
33567 + dwc_otg_device->os_dep.base);
33568 +
33569 + pci_set_master(_dev);
33570 + pci_set_drvdata(_dev, dwc_otg_device);
33571 +#elif defined(PLATFORM_INTERFACE)
33572 + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
33573 + _dev->resource->start,
33574 + _dev->resource->end - _dev->resource->start + 1);
33575 +#if 1
33576 + if (!request_mem_region(_dev->resource[0].start,
33577 + _dev->resource[0].end - _dev->resource[0].start + 1,
33578 + "dwc_otg")) {
33579 + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
33580 + retval = -EFAULT;
33581 + goto fail;
33582 + }
33583 +
33584 + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
33585 + _dev->resource[0].end -
33586 + _dev->resource[0].start+1);
33587 + if (fiq_enable)
33588 + {
33589 + if (!request_mem_region(_dev->resource[1].start,
33590 + _dev->resource[1].end - _dev->resource[1].start + 1,
33591 + "dwc_otg")) {
33592 + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
33593 + retval = -EFAULT;
33594 + goto fail;
33595 + }
33596 +
33597 + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
33598 + _dev->resource[1].end -
33599 + _dev->resource[1].start + 1);
33600 + }
33601 +
33602 +#else
33603 + {
33604 + struct map_desc desc = {
33605 + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
33606 + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
33607 + .length = SZ_128K,
33608 + .type = MT_DEVICE
33609 + };
33610 + iotable_init(&desc, 1);
33611 + dwc_otg_device->os_dep.base = (void *)desc.virtual;
33612 + }
33613 +#endif
33614 + if (!dwc_otg_device->os_dep.base) {
33615 + dev_err(&_dev->dev, "ioremap() failed\n");
33616 + retval = -ENOMEM;
33617 + goto fail;
33618 + }
33619 + dev_dbg(&_dev->dev, "base=0x%08x\n",
33620 + (unsigned)dwc_otg_device->os_dep.base);
33621 +#endif
33622 +
33623 + /*
33624 + * Initialize driver data to point to the global DWC_otg
33625 + * Device structure.
33626 + */
33627 +#ifdef LM_INTERFACE
33628 + lm_set_drvdata(_dev, dwc_otg_device);
33629 +#elif defined(PLATFORM_INTERFACE)
33630 + platform_set_drvdata(_dev, dwc_otg_device);
33631 +#endif
33632 + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
33633 +
33634 + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
33635 + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
33636 + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
33637 +
33638 + if (!dwc_otg_device->core_if) {
33639 + dev_err(&_dev->dev, "CIL initialization failed!\n");
33640 + retval = -ENOMEM;
33641 + goto fail;
33642 + }
33643 +
33644 + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
33645 + /*
33646 + * Attempt to ensure this device is really a DWC_otg Controller.
33647 + * Read and verify the SNPSID register contents. The value should be
33648 + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
33649 + * as in "OTG version 2.XX" or "OTG version 3.XX".
33650 + */
33651 +
33652 + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
33653 + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
33654 + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
33655 + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
33656 + retval = -EINVAL;
33657 + goto fail;
33658 + }
33659 +
33660 + /*
33661 + * Validate parameter values.
33662 + */
33663 + dev_dbg(&_dev->dev, "Calling set_parameters\n");
33664 + if (set_parameters(dwc_otg_device->core_if)) {
33665 + retval = -EINVAL;
33666 + goto fail;
33667 + }
33668 +
33669 + /*
33670 + * Create Device Attributes in sysfs
33671 + */
33672 + dev_dbg(&_dev->dev, "Calling attr_create\n");
33673 + dwc_otg_attr_create(_dev);
33674 +
33675 + /*
33676 + * Disable the global interrupt until all the interrupt
33677 + * handlers are installed.
33678 + */
33679 + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
33680 + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
33681 +
33682 + /*
33683 + * Install the interrupt handler for the common interrupts before
33684 + * enabling common interrupts in core_init below.
33685 + */
33686 +
33687 +#if defined(PLATFORM_INTERFACE)
33688 + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
33689 +#else
33690 + devirq = _dev->irq;
33691 +#endif
33692 + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
33693 + devirq);
33694 + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
33695 + retval = request_irq(devirq, dwc_otg_common_irq,
33696 + IRQF_SHARED,
33697 + "dwc_otg", dwc_otg_device);
33698 + if (retval) {
33699 + DWC_ERROR("request of irq%d failed\n", devirq);
33700 + retval = -EBUSY;
33701 + goto fail;
33702 + } else {
33703 + dwc_otg_device->common_irq_installed = 1;
33704 + }
33705 +
33706 +#ifndef IRQF_TRIGGER_LOW
33707 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
33708 + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
33709 + set_irq_type(devirq,
33710 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
33711 + IRQT_LOW
33712 +#else
33713 + IRQ_TYPE_LEVEL_LOW
33714 +#endif
33715 + );
33716 +#endif
33717 +#endif /*IRQF_TRIGGER_LOW*/
33718 +
33719 + /*
33720 + * Initialize the DWC_otg core.
33721 + */
33722 + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
33723 + dwc_otg_core_init(dwc_otg_device->core_if);
33724 +
33725 +#ifndef DWC_HOST_ONLY
33726 + /*
33727 + * Initialize the PCD
33728 + */
33729 + dev_dbg(&_dev->dev, "Calling pcd_init\n");
33730 + retval = pcd_init(_dev);
33731 + if (retval != 0) {
33732 + DWC_ERROR("pcd_init failed\n");
33733 + dwc_otg_device->pcd = NULL;
33734 + goto fail;
33735 + }
33736 +#endif
33737 +#ifndef DWC_DEVICE_ONLY
33738 + /*
33739 + * Initialize the HCD
33740 + */
33741 + dev_dbg(&_dev->dev, "Calling hcd_init\n");
33742 + retval = hcd_init(_dev);
33743 + if (retval != 0) {
33744 + DWC_ERROR("hcd_init failed\n");
33745 + dwc_otg_device->hcd = NULL;
33746 + goto fail;
33747 + }
33748 +#endif
33749 + /* Recover from drvdata having been overwritten by hcd_init() */
33750 +#ifdef LM_INTERFACE
33751 + lm_set_drvdata(_dev, dwc_otg_device);
33752 +#elif defined(PLATFORM_INTERFACE)
33753 + platform_set_drvdata(_dev, dwc_otg_device);
33754 +#elif defined(PCI_INTERFACE)
33755 + pci_set_drvdata(_dev, dwc_otg_device);
33756 + dwc_otg_device->os_dep.pcidev = _dev;
33757 +#endif
33758 +
33759 + /*
33760 + * Enable the global interrupt after all the interrupt
33761 + * handlers are installed if there is no ADP support else
33762 + * perform initial actions required for Internal ADP logic.
33763 + */
33764 + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
33765 + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
33766 + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
33767 + dev_dbg(&_dev->dev, "Done\n");
33768 + } else
33769 + dwc_otg_adp_start(dwc_otg_device->core_if,
33770 + dwc_otg_is_host_mode(dwc_otg_device->core_if));
33771 +
33772 + return 0;
33773 +
33774 +fail:
33775 + dwc_otg_driver_remove(_dev);
33776 + return retval;
33777 +}
33778 +
33779 +/**
33780 + * This structure defines the methods to be called by a bus driver
33781 + * during the lifecycle of a device on that bus. Both drivers and
33782 + * devices are registered with a bus driver. The bus driver matches
33783 + * devices to drivers based on information in the device and driver
33784 + * structures.
33785 + *
33786 + * The probe function is called when the bus driver matches a device
33787 + * to this driver. The remove function is called when a device is
33788 + * unregistered with the bus driver.
33789 + */
33790 +#ifdef LM_INTERFACE
33791 +static struct lm_driver dwc_otg_driver = {
33792 + .drv = {.name = (char *)dwc_driver_name,},
33793 + .probe = dwc_otg_driver_probe,
33794 + .remove = dwc_otg_driver_remove,
33795 + // 'suspend' and 'resume' absent
33796 +};
33797 +#elif defined(PCI_INTERFACE)
33798 +static const struct pci_device_id pci_ids[] = { {
33799 + PCI_DEVICE(0x16c3, 0xabcd),
33800 + .driver_data =
33801 + (unsigned long)0xdeadbeef,
33802 + }, { /* end: all zeroes */ }
33803 +};
33804 +
33805 +MODULE_DEVICE_TABLE(pci, pci_ids);
33806 +
33807 +/* pci driver glue; this is a "new style" PCI driver module */
33808 +static struct pci_driver dwc_otg_driver = {
33809 + .name = "dwc_otg",
33810 + .id_table = pci_ids,
33811 +
33812 + .probe = dwc_otg_driver_probe,
33813 + .remove = dwc_otg_driver_remove,
33814 +
33815 + .driver = {
33816 + .name = (char *)dwc_driver_name,
33817 + },
33818 +};
33819 +#elif defined(PLATFORM_INTERFACE)
33820 +static struct platform_device_id platform_ids[] = {
33821 + {
33822 + .name = "bcm2708_usb",
33823 + .driver_data = (kernel_ulong_t) 0xdeadbeef,
33824 + },
33825 + { /* end: all zeroes */ }
33826 +};
33827 +MODULE_DEVICE_TABLE(platform, platform_ids);
33828 +
33829 +static const struct of_device_id dwc_otg_of_match_table[] = {
33830 + { .compatible = "brcm,bcm2708-usb", },
33831 + {},
33832 +};
33833 +MODULE_DEVICE_TABLE(of, dwc_otg_of_match_table);
33834 +
33835 +static struct platform_driver dwc_otg_driver = {
33836 + .driver = {
33837 + .name = (char *)dwc_driver_name,
33838 + .of_match_table = dwc_otg_of_match_table,
33839 + },
33840 + .id_table = platform_ids,
33841 +
33842 + .probe = dwc_otg_driver_probe,
33843 + .remove = dwc_otg_driver_remove,
33844 + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
33845 +};
33846 +#endif
33847 +
33848 +/**
33849 + * This function is called when the dwc_otg_driver is installed with the
33850 + * insmod command. It registers the dwc_otg_driver structure with the
33851 + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
33852 + * to be called. In addition, the bus driver will automatically expose
33853 + * attributes defined for the device and driver in the special sysfs file
33854 + * system.
33855 + *
33856 + * @return
33857 + */
33858 +static int __init dwc_otg_driver_init(void)
33859 +{
33860 + int retval = 0;
33861 + int error;
33862 + struct device_driver *drv;
33863 +
33864 + if(fiq_fsm_enable && !fiq_enable) {
33865 + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
33866 + fiq_enable = 1;
33867 + }
33868 +
33869 + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
33870 + DWC_DRIVER_VERSION,
33871 +#ifdef LM_INTERFACE
33872 + "logicmodule");
33873 + retval = lm_driver_register(&dwc_otg_driver);
33874 + drv = &dwc_otg_driver.drv;
33875 +#elif defined(PCI_INTERFACE)
33876 + "pci");
33877 + retval = pci_register_driver(&dwc_otg_driver);
33878 + drv = &dwc_otg_driver.driver;
33879 +#elif defined(PLATFORM_INTERFACE)
33880 + "platform");
33881 + retval = platform_driver_register(&dwc_otg_driver);
33882 + drv = &dwc_otg_driver.driver;
33883 +#endif
33884 + if (retval < 0) {
33885 + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
33886 + return retval;
33887 + }
33888 + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
33889 + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
33890 + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
33891 +
33892 + error = driver_create_file(drv, &driver_attr_version);
33893 +#ifdef DEBUG
33894 + error = driver_create_file(drv, &driver_attr_debuglevel);
33895 +#endif
33896 + return retval;
33897 +}
33898 +
33899 +module_init(dwc_otg_driver_init);
33900 +
33901 +/**
33902 + * This function is called when the driver is removed from the kernel
33903 + * with the rmmod command. The driver unregisters itself with its bus
33904 + * driver.
33905 + *
33906 + */
33907 +static void __exit dwc_otg_driver_cleanup(void)
33908 +{
33909 + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
33910 +
33911 +#ifdef LM_INTERFACE
33912 + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
33913 + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
33914 + lm_driver_unregister(&dwc_otg_driver);
33915 +#elif defined(PCI_INTERFACE)
33916 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
33917 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
33918 + pci_unregister_driver(&dwc_otg_driver);
33919 +#elif defined(PLATFORM_INTERFACE)
33920 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
33921 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
33922 + platform_driver_unregister(&dwc_otg_driver);
33923 +#endif
33924 +
33925 + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
33926 +}
33927 +
33928 +module_exit(dwc_otg_driver_cleanup);
33929 +
33930 +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
33931 +MODULE_AUTHOR("Synopsys Inc.");
33932 +MODULE_LICENSE("GPL");
33933 +
33934 +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
33935 +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
33936 +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
33937 +MODULE_PARM_DESC(opt, "OPT Mode");
33938 +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
33939 +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
33940 +
33941 +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
33942 + 0444);
33943 +MODULE_PARM_DESC(dma_desc_enable,
33944 + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
33945 +
33946 +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
33947 + 0444);
33948 +MODULE_PARM_DESC(dma_burst_size,
33949 + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
33950 +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
33951 +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
33952 +module_param_named(host_support_fs_ls_low_power,
33953 + dwc_otg_module_params.host_support_fs_ls_low_power, int,
33954 + 0444);
33955 +MODULE_PARM_DESC(host_support_fs_ls_low_power,
33956 + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
33957 +module_param_named(host_ls_low_power_phy_clk,
33958 + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
33959 +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
33960 + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
33961 +module_param_named(enable_dynamic_fifo,
33962 + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
33963 +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
33964 +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
33965 + 0444);
33966 +MODULE_PARM_DESC(data_fifo_size,
33967 + "Total number of words in the data FIFO memory 32-32768");
33968 +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
33969 + int, 0444);
33970 +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
33971 +module_param_named(dev_nperio_tx_fifo_size,
33972 + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
33973 +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
33974 + "Number of words in the non-periodic Tx FIFO 16-32768");
33975 +module_param_named(dev_perio_tx_fifo_size_1,
33976 + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
33977 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
33978 + "Number of words in the periodic Tx FIFO 4-768");
33979 +module_param_named(dev_perio_tx_fifo_size_2,
33980 + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
33981 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
33982 + "Number of words in the periodic Tx FIFO 4-768");
33983 +module_param_named(dev_perio_tx_fifo_size_3,
33984 + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
33985 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
33986 + "Number of words in the periodic Tx FIFO 4-768");
33987 +module_param_named(dev_perio_tx_fifo_size_4,
33988 + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
33989 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
33990 + "Number of words in the periodic Tx FIFO 4-768");
33991 +module_param_named(dev_perio_tx_fifo_size_5,
33992 + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
33993 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
33994 + "Number of words in the periodic Tx FIFO 4-768");
33995 +module_param_named(dev_perio_tx_fifo_size_6,
33996 + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
33997 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
33998 + "Number of words in the periodic Tx FIFO 4-768");
33999 +module_param_named(dev_perio_tx_fifo_size_7,
34000 + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
34001 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
34002 + "Number of words in the periodic Tx FIFO 4-768");
34003 +module_param_named(dev_perio_tx_fifo_size_8,
34004 + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
34005 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
34006 + "Number of words in the periodic Tx FIFO 4-768");
34007 +module_param_named(dev_perio_tx_fifo_size_9,
34008 + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
34009 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
34010 + "Number of words in the periodic Tx FIFO 4-768");
34011 +module_param_named(dev_perio_tx_fifo_size_10,
34012 + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
34013 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
34014 + "Number of words in the periodic Tx FIFO 4-768");
34015 +module_param_named(dev_perio_tx_fifo_size_11,
34016 + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
34017 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
34018 + "Number of words in the periodic Tx FIFO 4-768");
34019 +module_param_named(dev_perio_tx_fifo_size_12,
34020 + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
34021 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
34022 + "Number of words in the periodic Tx FIFO 4-768");
34023 +module_param_named(dev_perio_tx_fifo_size_13,
34024 + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
34025 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
34026 + "Number of words in the periodic Tx FIFO 4-768");
34027 +module_param_named(dev_perio_tx_fifo_size_14,
34028 + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
34029 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
34030 + "Number of words in the periodic Tx FIFO 4-768");
34031 +module_param_named(dev_perio_tx_fifo_size_15,
34032 + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
34033 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
34034 + "Number of words in the periodic Tx FIFO 4-768");
34035 +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
34036 + int, 0444);
34037 +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
34038 +module_param_named(host_nperio_tx_fifo_size,
34039 + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
34040 +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
34041 + "Number of words in the non-periodic Tx FIFO 16-32768");
34042 +module_param_named(host_perio_tx_fifo_size,
34043 + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
34044 +MODULE_PARM_DESC(host_perio_tx_fifo_size,
34045 + "Number of words in the host periodic Tx FIFO 16-32768");
34046 +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
34047 + int, 0444);
34048 +/** @todo Set the max to 512K, modify checks */
34049 +MODULE_PARM_DESC(max_transfer_size,
34050 + "The maximum transfer size supported in bytes 2047-65535");
34051 +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
34052 + int, 0444);
34053 +MODULE_PARM_DESC(max_packet_count,
34054 + "The maximum number of packets in a transfer 15-511");
34055 +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
34056 + 0444);
34057 +MODULE_PARM_DESC(host_channels,
34058 + "The number of host channel registers to use 1-16");
34059 +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
34060 + 0444);
34061 +MODULE_PARM_DESC(dev_endpoints,
34062 + "The number of endpoints in addition to EP0 available for device mode 1-15");
34063 +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
34064 +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
34065 +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
34066 + 0444);
34067 +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
34068 +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
34069 +MODULE_PARM_DESC(phy_ulpi_ddr,
34070 + "ULPI at double or single data rate 0=Single 1=Double");
34071 +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
34072 + int, 0444);
34073 +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
34074 + "ULPI PHY using internal or external vbus 0=Internal");
34075 +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
34076 +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
34077 +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
34078 +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
34079 +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
34080 +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
34081 +module_param_named(debug, g_dbg_lvl, int, 0444);
34082 +MODULE_PARM_DESC(debug, "");
34083 +
34084 +module_param_named(en_multiple_tx_fifo,
34085 + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
34086 +MODULE_PARM_DESC(en_multiple_tx_fifo,
34087 + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
34088 +module_param_named(dev_tx_fifo_size_1,
34089 + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
34090 +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
34091 +module_param_named(dev_tx_fifo_size_2,
34092 + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
34093 +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
34094 +module_param_named(dev_tx_fifo_size_3,
34095 + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
34096 +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
34097 +module_param_named(dev_tx_fifo_size_4,
34098 + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
34099 +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
34100 +module_param_named(dev_tx_fifo_size_5,
34101 + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
34102 +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
34103 +module_param_named(dev_tx_fifo_size_6,
34104 + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
34105 +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
34106 +module_param_named(dev_tx_fifo_size_7,
34107 + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
34108 +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
34109 +module_param_named(dev_tx_fifo_size_8,
34110 + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
34111 +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
34112 +module_param_named(dev_tx_fifo_size_9,
34113 + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
34114 +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
34115 +module_param_named(dev_tx_fifo_size_10,
34116 + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
34117 +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
34118 +module_param_named(dev_tx_fifo_size_11,
34119 + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
34120 +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
34121 +module_param_named(dev_tx_fifo_size_12,
34122 + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
34123 +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
34124 +module_param_named(dev_tx_fifo_size_13,
34125 + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
34126 +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
34127 +module_param_named(dev_tx_fifo_size_14,
34128 + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
34129 +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
34130 +module_param_named(dev_tx_fifo_size_15,
34131 + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
34132 +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
34133 +
34134 +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
34135 +MODULE_PARM_DESC(thr_ctl,
34136 + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
34137 +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
34138 + 0444);
34139 +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
34140 +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
34141 + 0444);
34142 +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
34143 +
34144 +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
34145 +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
34146 +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
34147 +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
34148 +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
34149 +MODULE_PARM_DESC(ic_usb_cap,
34150 + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
34151 +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
34152 + 0444);
34153 +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
34154 +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
34155 +MODULE_PARM_DESC(power_down, "Power Down Mode");
34156 +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
34157 +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
34158 +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
34159 +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
34160 +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
34161 +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
34162 +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
34163 +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
34164 +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
34165 +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
34166 +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
34167 +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
34168 +module_param(microframe_schedule, bool, 0444);
34169 +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
34170 +
34171 +module_param(fiq_enable, bool, 0444);
34172 +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
34173 +module_param(nak_holdoff, ushort, 0644);
34174 +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
34175 +module_param(fiq_fsm_enable, bool, 0444);
34176 +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
34177 +module_param(fiq_fsm_mask, ushort, 0444);
34178 +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
34179 + "Bit 0 : Non-periodic split transactions\n"
34180 + "Bit 1 : Periodic split transactions\n"
34181 + "Bit 2 : High-speed multi-transfer isochronous\n"
34182 + "All other bits should be set 0.");
34183 +module_param(int_ep_interval_min, ushort, 0644);
34184 +MODULE_PARM_DESC(int_ep_interval_min, "Clamp high-speed Interrupt endpoints to a minimum polling interval.\n"
34185 + "0..1 = Use endpoint default\n"
34186 + "2..n = Minimum interval n microframes. Use powers of 2.\n");
34187 +
34188 +module_param(cil_force_host, bool, 0644);
34189 +MODULE_PARM_DESC(cil_force_host, "On a connector-ID status change, "
34190 + "force Host Mode regardless of OTG state.");
34191 +
34192 +/** @page "Module Parameters"
34193 + *
34194 + * The following parameters may be specified when starting the module.
34195 + * These parameters define how the DWC_otg controller should be
34196 + * configured. Parameter values are passed to the CIL initialization
34197 + * function dwc_otg_cil_init
34198 + *
34199 + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
34200 + *
34201 +
34202 + <table>
34203 + <tr><td>Parameter Name</td><td>Meaning</td></tr>
34204 +
34205 + <tr>
34206 + <td>otg_cap</td>
34207 + <td>Specifies the OTG capabilities. The driver will automatically detect the
34208 + value for this parameter if none is specified.
34209 + - 0: HNP and SRP capable (default, if available)
34210 + - 1: SRP Only capable
34211 + - 2: No HNP/SRP capable
34212 + </td></tr>
34213 +
34214 + <tr>
34215 + <td>dma_enable</td>
34216 + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
34217 + The driver will automatically detect the value for this parameter if none is
34218 + specified.
34219 + - 0: Slave
34220 + - 1: DMA (default, if available)
34221 + </td></tr>
34222 +
34223 + <tr>
34224 + <td>dma_burst_size</td>
34225 + <td>The DMA Burst size (applicable only for External DMA Mode).
34226 + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
34227 + </td></tr>
34228 +
34229 + <tr>
34230 + <td>speed</td>
34231 + <td>Specifies the maximum speed of operation in host and device mode. The
34232 + actual speed depends on the speed of the attached device and the value of
34233 + phy_type.
34234 + - 0: High Speed (default)
34235 + - 1: Full Speed
34236 + </td></tr>
34237 +
34238 + <tr>
34239 + <td>host_support_fs_ls_low_power</td>
34240 + <td>Specifies whether low power mode is supported when attached to a Full
34241 + Speed or Low Speed device in host mode.
34242 + - 0: Don't support low power mode (default)
34243 + - 1: Support low power mode
34244 + </td></tr>
34245 +
34246 + <tr>
34247 + <td>host_ls_low_power_phy_clk</td>
34248 + <td>Specifies the PHY clock rate in low power mode when connected to a Low
34249 + Speed device in host mode. This parameter is applicable only if
34250 + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
34251 + - 0: 48 MHz (default)
34252 + - 1: 6 MHz
34253 + </td></tr>
34254 +
34255 + <tr>
34256 + <td>enable_dynamic_fifo</td>
34257 + <td> Specifies whether FIFOs may be resized by the driver software.
34258 + - 0: Use cC FIFO size parameters
34259 + - 1: Allow dynamic FIFO sizing (default)
34260 + </td></tr>
34261 +
34262 + <tr>
34263 + <td>data_fifo_size</td>
34264 + <td>Total number of 4-byte words in the data FIFO memory. This memory
34265 + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
34266 + - Values: 32 to 32768 (default 8192)
34267 +
34268 + Note: The total FIFO memory depth in the FPGA configuration is 8192.
34269 + </td></tr>
34270 +
34271 + <tr>
34272 + <td>dev_rx_fifo_size</td>
34273 + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
34274 + FIFO sizing is enabled.
34275 + - Values: 16 to 32768 (default 1064)
34276 + </td></tr>
34277 +
34278 + <tr>
34279 + <td>dev_nperio_tx_fifo_size</td>
34280 + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
34281 + dynamic FIFO sizing is enabled.
34282 + - Values: 16 to 32768 (default 1024)
34283 + </td></tr>
34284 +
34285 + <tr>
34286 + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
34287 + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
34288 + when dynamic FIFO sizing is enabled.
34289 + - Values: 4 to 768 (default 256)
34290 + </td></tr>
34291 +
34292 + <tr>
34293 + <td>host_rx_fifo_size</td>
34294 + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
34295 + sizing is enabled.
34296 + - Values: 16 to 32768 (default 1024)
34297 + </td></tr>
34298 +
34299 + <tr>
34300 + <td>host_nperio_tx_fifo_size</td>
34301 + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
34302 + dynamic FIFO sizing is enabled in the core.
34303 + - Values: 16 to 32768 (default 1024)
34304 + </td></tr>
34305 +
34306 + <tr>
34307 + <td>host_perio_tx_fifo_size</td>
34308 + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
34309 + sizing is enabled.
34310 + - Values: 16 to 32768 (default 1024)
34311 + </td></tr>
34312 +
34313 + <tr>
34314 + <td>max_transfer_size</td>
34315 + <td>The maximum transfer size supported in bytes.
34316 + - Values: 2047 to 65,535 (default 65,535)
34317 + </td></tr>
34318 +
34319 + <tr>
34320 + <td>max_packet_count</td>
34321 + <td>The maximum number of packets in a transfer.
34322 + - Values: 15 to 511 (default 511)
34323 + </td></tr>
34324 +
34325 + <tr>
34326 + <td>host_channels</td>
34327 + <td>The number of host channel registers to use.
34328 + - Values: 1 to 16 (default 12)
34329 +
34330 + Note: The FPGA configuration supports a maximum of 12 host channels.
34331 + </td></tr>
34332 +
34333 + <tr>
34334 + <td>dev_endpoints</td>
34335 + <td>The number of endpoints in addition to EP0 available for device mode
34336 + operations.
34337 + - Values: 1 to 15 (default 6 IN and OUT)
34338 +
34339 + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
34340 + addition to EP0.
34341 + </td></tr>
34342 +
34343 + <tr>
34344 + <td>phy_type</td>
34345 + <td>Specifies the type of PHY interface to use. By default, the driver will
34346 + automatically detect the phy_type.
34347 + - 0: Full Speed
34348 + - 1: UTMI+ (default, if available)
34349 + - 2: ULPI
34350 + </td></tr>
34351 +
34352 + <tr>
34353 + <td>phy_utmi_width</td>
34354 + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
34355 + phy_type of UTMI+. Also, this parameter is applicable only if the
34356 + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
34357 + core has been configured to work at either data path width.
34358 + - Values: 8 or 16 bits (default 16)
34359 + </td></tr>
34360 +
34361 + <tr>
34362 + <td>phy_ulpi_ddr</td>
34363 + <td>Specifies whether the ULPI operates at double or single data rate. This
34364 + parameter is only applicable if phy_type is ULPI.
34365 + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
34366 + - 1: double data rate ULPI interface with 4 bit wide data bus
34367 + </td></tr>
34368 +
34369 + <tr>
34370 + <td>i2c_enable</td>
34371 + <td>Specifies whether to use the I2C interface for full speed PHY. This
34372 + parameter is only applicable if PHY_TYPE is FS.
34373 + - 0: Disabled (default)
34374 + - 1: Enabled
34375 + </td></tr>
34376 +
34377 + <tr>
34378 + <td>ulpi_fs_ls</td>
34379 + <td>Specifies whether to use ULPI FS/LS mode only.
34380 + - 0: Disabled (default)
34381 + - 1: Enabled
34382 + </td></tr>
34383 +
34384 + <tr>
34385 + <td>ts_dline</td>
34386 + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
34387 + - 0: Disabled (default)
34388 + - 1: Enabled
34389 + </td></tr>
34390 +
34391 + <tr>
34392 + <td>en_multiple_tx_fifo</td>
34393 + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
34394 + The driver will automatically detect the value for this parameter if none is
34395 + specified.
34396 + - 0: Disabled
34397 + - 1: Enabled (default, if available)
34398 + </td></tr>
34399 +
34400 + <tr>
34401 + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
34402 + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
34403 + when dynamic FIFO sizing is enabled.
34404 + - Values: 4 to 768 (default 256)
34405 + </td></tr>
34406 +
34407 + <tr>
34408 + <td>tx_thr_length</td>
34409 + <td>Transmit Threshold length in 32 bit double words
34410 + - Values: 8 to 128 (default 64)
34411 + </td></tr>
34412 +
34413 + <tr>
34414 + <td>rx_thr_length</td>
34415 + <td>Receive Threshold length in 32 bit double words
34416 + - Values: 8 to 128 (default 64)
34417 + </td></tr>
34418 +
34419 +<tr>
34420 + <td>thr_ctl</td>
34421 + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
34422 + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
34423 + Rx transfers accordingly.
34424 + The driver will automatically detect the value for this parameter if none is
34425 + specified.
34426 + - Values: 0 to 7 (default 0)
34427 + Bit values indicate:
34428 + - 0: Thresholding disabled
34429 + - 1: Thresholding enabled
34430 + </td></tr>
34431 +
34432 +<tr>
34433 + <td>dma_desc_enable</td>
34434 + <td>Specifies whether to enable Descriptor DMA mode.
34435 + The driver will automatically detect the value for this parameter if none is
34436 + specified.
34437 + - 0: Descriptor DMA disabled
34438 + - 1: Descriptor DMA (default, if available)
34439 + </td></tr>
34440 +
34441 +<tr>
34442 + <td>mpi_enable</td>
34443 + <td>Specifies whether to enable MPI enhancement mode.
34444 + The driver will automatically detect the value for this parameter if none is
34445 + specified.
34446 + - 0: MPI disabled (default)
34447 + - 1: MPI enable
34448 + </td></tr>
34449 +
34450 +<tr>
34451 + <td>pti_enable</td>
34452 + <td>Specifies whether to enable PTI enhancement support.
34453 + The driver will automatically detect the value for this parameter if none is
34454 + specified.
34455 + - 0: PTI disabled (default)
34456 + - 1: PTI enable
34457 + </td></tr>
34458 +
34459 +<tr>
34460 + <td>lpm_enable</td>
34461 + <td>Specifies whether to enable LPM support.
34462 + The driver will automatically detect the value for this parameter if none is
34463 + specified.
34464 + - 0: LPM disabled
34465 + - 1: LPM enable (default, if available)
34466 + </td></tr>
34467 +
34468 +<tr>
34469 + <td>ic_usb_cap</td>
34470 + <td>Specifies whether to enable IC_USB capability.
34471 + The driver will automatically detect the value for this parameter if none is
34472 + specified.
34473 + - 0: IC_USB disabled (default, if available)
34474 + - 1: IC_USB enable
34475 + </td></tr>
34476 +
34477 +<tr>
34478 + <td>ahb_thr_ratio</td>
34479 + <td>Specifies AHB Threshold ratio.
34480 + - Values: 0 to 3 (default 0)
34481 + </td></tr>
34482 +
34483 +<tr>
34484 + <td>power_down</td>
34485 + <td>Specifies Power Down(Hibernation) Mode.
34486 + The driver will automatically detect the value for this parameter if none is
34487 + specified.
34488 + - 0: Power Down disabled (default)
34489 + - 2: Power Down enabled
34490 + </td></tr>
34491 +
34492 + <tr>
34493 + <td>reload_ctl</td>
34494 + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
34495 + run time. The driver will automatically detect the value for this parameter if
34496 + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
34497 + the core might misbehave.
34498 + - 0: Reload Control disabled (default)
34499 + - 1: Reload Control enabled
34500 + </td></tr>
34501 +
34502 + <tr>
34503 + <td>dev_out_nak</td>
34504 + <td>Specifies whether Device OUT NAK enhancement enabled or no.
34505 + The driver will automatically detect the value for this parameter if
34506 + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
34507 + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
34508 + - 1: The core sets NAK after Bulk OUT transfer complete
34509 + </td></tr>
34510 +
34511 + <tr>
34512 + <td>cont_on_bna</td>
34513 + <td>Specifies whether Enable Continue on BNA enabled or no.
34514 + After receiving BNA interrupt the core disables the endpoint,when the
34515 + endpoint is re-enabled by the application the
34516 + - 0: Core starts processing from the DOEPDMA descriptor (default)
34517 + - 1: Core starts processing from the descriptor which received the BNA.
34518 + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
34519 + </td></tr>
34520 +
34521 + <tr>
34522 + <td>ahb_single</td>
34523 + <td>This bit when programmed supports SINGLE transfers for remainder data
34524 + in a transfer for DMA mode of operation.
34525 + - 0: The remainder data will be sent using INCR burst size (default)
34526 + - 1: The remainder data will be sent using SINGLE burst size.
34527 + </td></tr>
34528 +
34529 +<tr>
34530 + <td>adp_enable</td>
34531 + <td>Specifies whether ADP feature is enabled.
34532 + The driver will automatically detect the value for this parameter if none is
34533 + specified.
34534 + - 0: ADP feature disabled (default)
34535 + - 1: ADP feature enabled
34536 + </td></tr>
34537 +
34538 + <tr>
34539 + <td>otg_ver</td>
34540 + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
34541 + USB OTG device.
34542 + - 0: OTG 2.0 support disabled (default)
34543 + - 1: OTG 2.0 support enabled
34544 + </td></tr>
34545 +
34546 +*/
34547 --- /dev/null
34548 +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.h
34549 @@ -0,0 +1,86 @@
34550 +/* ==========================================================================
34551 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
34552 + * $Revision: #19 $
34553 + * $Date: 2010/11/15 $
34554 + * $Change: 1627671 $
34555 + *
34556 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
34557 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
34558 + * otherwise expressly agreed to in writing between Synopsys and you.
34559 + *
34560 + * The Software IS NOT an item of Licensed Software or Licensed Product under
34561 + * any End User Software License Agreement or Agreement for Licensed Product
34562 + * with Synopsys or any supplement thereto. You are permitted to use and
34563 + * redistribute this Software in source and binary forms, with or without
34564 + * modification, provided that redistributions of source code must retain this
34565 + * notice. You may not view, use, disclose, copy or distribute this file or
34566 + * any information contained herein except pursuant to this license grant from
34567 + * Synopsys. If you do not agree with this notice, including the disclaimer
34568 + * below, then you are not authorized to use the Software.
34569 + *
34570 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
34571 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34572 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34573 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
34574 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34575 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34576 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34577 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34578 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34579 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
34580 + * DAMAGE.
34581 + * ========================================================================== */
34582 +
34583 +#ifndef __DWC_OTG_DRIVER_H__
34584 +#define __DWC_OTG_DRIVER_H__
34585 +
34586 +/** @file
34587 + * This file contains the interface to the Linux driver.
34588 + */
34589 +#include "dwc_otg_os_dep.h"
34590 +#include "dwc_otg_core_if.h"
34591 +
34592 +/* Type declarations */
34593 +struct dwc_otg_pcd;
34594 +struct dwc_otg_hcd;
34595 +
34596 +/**
34597 + * This structure is a wrapper that encapsulates the driver components used to
34598 + * manage a single DWC_otg controller.
34599 + */
34600 +typedef struct dwc_otg_device {
34601 + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
34602 + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
34603 + * require this. */
34604 + struct os_dependent os_dep;
34605 +
34606 + /** Pointer to the core interface structure. */
34607 + dwc_otg_core_if_t *core_if;
34608 +
34609 + /** Pointer to the PCD structure. */
34610 + struct dwc_otg_pcd *pcd;
34611 +
34612 + /** Pointer to the HCD structure. */
34613 + struct dwc_otg_hcd *hcd;
34614 +
34615 + /** Flag to indicate whether the common IRQ handler is installed. */
34616 + uint8_t common_irq_installed;
34617 +
34618 +} dwc_otg_device_t;
34619 +
34620 +/*We must clear S3C24XX_EINTPEND external interrupt register
34621 + * because after clearing in this register trigerred IRQ from
34622 + * H/W core in kernel interrupt can be occured again before OTG
34623 + * handlers clear all IRQ sources of Core registers because of
34624 + * timing latencies and Low Level IRQ Type.
34625 + */
34626 +#ifdef CONFIG_MACH_IPMATE
34627 +#define S3C2410X_CLEAR_EINTPEND() \
34628 +do { \
34629 + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
34630 +} while (0)
34631 +#else
34632 +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
34633 +#endif
34634 +
34635 +#endif
34636 --- /dev/null
34637 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
34638 @@ -0,0 +1,1401 @@
34639 +/*
34640 + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
34641 + *
34642 + * Copyright (c) 2013 Raspberry Pi Foundation
34643 + *
34644 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
34645 + * All rights reserved.
34646 + *
34647 + * Redistribution and use in source and binary forms, with or without
34648 + * modification, are permitted provided that the following conditions are met:
34649 + * * Redistributions of source code must retain the above copyright
34650 + * notice, this list of conditions and the following disclaimer.
34651 + * * Redistributions in binary form must reproduce the above copyright
34652 + * notice, this list of conditions and the following disclaimer in the
34653 + * documentation and/or other materials provided with the distribution.
34654 + * * Neither the name of Raspberry Pi nor the
34655 + * names of its contributors may be used to endorse or promote products
34656 + * derived from this software without specific prior written permission.
34657 + *
34658 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34659 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34660 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34661 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
34662 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34663 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34664 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34665 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34666 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34667 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34668 + *
34669 + * This FIQ implements functionality that performs split transactions on
34670 + * the dwc_otg hardware without any outside intervention. A split transaction
34671 + * is "queued" by nominating a specific host channel to perform the entirety
34672 + * of a split transaction. This FIQ will then perform the microframe-precise
34673 + * scheduling required in each phase of the transaction until completion.
34674 + *
34675 + * The FIQ functionality is glued into the Synopsys driver via the entry point
34676 + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
34677 + * for a FSM-enabled channel.
34678 + *
34679 + * NB: Large parts of this implementation have architecture-specific code.
34680 + * For porting this functionality to other ARM machines, the minimum is required:
34681 + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
34682 + * to the FIQ
34683 + * - A method of forcing a software generated interrupt from FIQ mode that then
34684 + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
34685 + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
34686 + * processor core - there is no locking between the FIQ and IRQ (aside from
34687 + * local_fiq_disable)
34688 + *
34689 + */
34690 +
34691 +#include "dwc_otg_fiq_fsm.h"
34692 +
34693 +
34694 +char buffer[1000*16];
34695 +int wptr;
34696 +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
34697 +{
34698 + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
34699 + va_list args;
34700 + char text[17];
34701 + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
34702 +
34703 + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
34704 + {
34705 + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
34706 + va_start(args, fmt);
34707 + vsnprintf(text+8, 9, fmt, args);
34708 + va_end(args);
34709 +
34710 + memcpy(buffer + wptr, text, 16);
34711 + wptr = (wptr + 16) % sizeof(buffer);
34712 + }
34713 +}
34714 +
34715 +/**
34716 + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
34717 + * Must be called with local interrupts and FIQ disabled.
34718 + */
34719 +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
34720 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
34721 +{
34722 + unsigned long tmp;
34723 + uint32_t newval;
34724 + fiq_lock_t lockval;
34725 + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
34726 + * will be sufficient. If we are on a different CPU, then the lock protects us. */
34727 + prefetchw(&lock->slock);
34728 + asm volatile (
34729 + "1: ldrex %0, [%3]\n"
34730 + " add %1, %0, %4\n"
34731 + " strex %2, %1, [%3]\n"
34732 + " teq %2, #0\n"
34733 + " bne 1b"
34734 + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
34735 + : "r" (&lock->slock), "I" (1 << 16)
34736 + : "cc");
34737 +
34738 + while (lockval.tickets.next != lockval.tickets.owner) {
34739 + wfe();
34740 + lockval.tickets.owner = READ_ONCE(lock->tickets.owner);
34741 + }
34742 + smp_mb();
34743 +}
34744 +#else
34745 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock) { }
34746 +#endif
34747 +
34748 +/**
34749 + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
34750 + */
34751 +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
34752 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
34753 +{
34754 + smp_mb();
34755 + lock->tickets.owner++;
34756 + dsb_sev();
34757 +}
34758 +#else
34759 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock) { }
34760 +#endif
34761 +
34762 +/**
34763 + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
34764 + * @channel: channel to re-enable
34765 + */
34766 +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
34767 +{
34768 + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
34769 +
34770 + hcchar.b.chen = 0;
34771 + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
34772 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
34773 + /* Hardware bug workaround: update the ssplit index */
34774 + if (st->channel[n].hcsplt_copy.b.spltena)
34775 + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
34776 +
34777 + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
34778 + }
34779 +
34780 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
34781 + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
34782 + hcchar.b.chen = 1;
34783 +
34784 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
34785 + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
34786 +}
34787 +
34788 +/**
34789 + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
34790 + * @st: Pointer to the channel's state
34791 + * @n : channel number
34792 + *
34793 + * Change host channel registers to perform a complete-split transaction. Being mindful of the
34794 + * endpoint direction, set control regs up correctly.
34795 + */
34796 +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
34797 +{
34798 + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
34799 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34800 +
34801 + hcsplt.b.compsplt = 1;
34802 + if (st->channel[n].hcchar_copy.b.epdir == 1) {
34803 + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
34804 + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
34805 + } else {
34806 + // If OUT, the CSPLIT result contains handshake only.
34807 + hctsiz.b.xfersize = 0;
34808 + }
34809 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
34810 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34811 + mb();
34812 +}
34813 +
34814 +/**
34815 + * fiq_fsm_restart_np_pending() - Restart a single non-periodic contended transfer
34816 + * @st: Pointer to the channel's state
34817 + * @num_channels: Total number of host channels
34818 + * @orig_channel: Channel index of completed transfer
34819 + *
34820 + * In the case where an IN and OUT transfer are simultaneously scheduled to the
34821 + * same device/EP, inadequate hub implementations will misbehave. Once the first
34822 + * transfer is complete, a pending non-periodic split can then be issued.
34823 + */
34824 +static void notrace fiq_fsm_restart_np_pending(struct fiq_state *st, int num_channels, int orig_channel)
34825 +{
34826 + int i;
34827 + int dev_addr = st->channel[orig_channel].hcchar_copy.b.devaddr;
34828 + int ep_num = st->channel[orig_channel].hcchar_copy.b.epnum;
34829 + for (i = 0; i < num_channels; i++) {
34830 + if (st->channel[i].fsm == FIQ_NP_SSPLIT_PENDING &&
34831 + st->channel[i].hcchar_copy.b.devaddr == dev_addr &&
34832 + st->channel[i].hcchar_copy.b.epnum == ep_num) {
34833 + st->channel[i].fsm = FIQ_NP_SSPLIT_STARTED;
34834 + fiq_fsm_restart_channel(st, i, 0);
34835 + break;
34836 + }
34837 + }
34838 +}
34839 +
34840 +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
34841 +{
34842 + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
34843 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34844 +
34845 + if (st->channel[n].hcchar_copy.b.epdir == 0) {
34846 + return st->channel[n].hctsiz_copy.b.xfersize;
34847 + } else {
34848 + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
34849 + }
34850 +
34851 +}
34852 +
34853 +
34854 +/**
34855 + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
34856 + *
34857 + * Of use only for IN periodic transfers.
34858 + */
34859 +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
34860 +{
34861 + hcdma_data_t hcdma;
34862 + int i = st->channel[n].dma_info.index;
34863 + int len;
34864 + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
34865 +
34866 + len = fiq_get_xfer_len(st, n);
34867 + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
34868 + st->channel[n].dma_info.slot_len[i] = len;
34869 + i++;
34870 + if (i > 6)
34871 + BUG();
34872 +
34873 + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
34874 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
34875 + st->channel[n].dma_info.index = i;
34876 + return 0;
34877 +}
34878 +
34879 +/**
34880 + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
34881 + */
34882 +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
34883 +{
34884 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34885 + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
34886 + hctsiz.b.pktcnt = 1;
34887 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34888 +}
34889 +
34890 +/**
34891 + * fiq_fsm_reload_hcdma() - for OUT transactions, rewind DMA pointer
34892 + */
34893 +static void notrace fiq_fsm_reload_hcdma(struct fiq_state *st, int n)
34894 +{
34895 + hcdma_data_t hcdma = st->channel[n].hcdma_copy;
34896 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
34897 +}
34898 +
34899 +/**
34900 + * fiq_iso_out_advance() - update DMA address and split position bits
34901 + * for isochronous OUT transactions.
34902 + *
34903 + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
34904 + * Split-BEGIN states are not handled - this is done when the transaction was queued.
34905 + *
34906 + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
34907 + */
34908 +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
34909 +{
34910 + hcsplt_data_t hcsplt;
34911 + hctsiz_data_t hctsiz;
34912 + hcdma_data_t hcdma;
34913 + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
34914 + int last = 0;
34915 + int i = st->channel[n].dma_info.index;
34916 +
34917 + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
34918 + i++;
34919 + if (i == 4)
34920 + last = 1;
34921 + if (st->channel[n].dma_info.slot_len[i+1] == 255)
34922 + last = 1;
34923 +
34924 + /* New DMA address - address of bounce buffer referred to in index */
34925 + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
34926 + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA);
34927 + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
34928 + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
34929 + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
34930 + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
34931 + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
34932 + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
34933 + /* Set up new packet length */
34934 + hctsiz.b.pktcnt = 1;
34935 + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
34936 + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
34937 +
34938 + st->channel[n].dma_info.index++;
34939 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
34940 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34941 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
34942 + return last;
34943 +}
34944 +
34945 +/**
34946 + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
34947 + *
34948 + * Despite the limitations of the DWC core, we can force a microframe pipeline of
34949 + * isochronous OUT start-split transactions while waiting for a corresponding other-type
34950 + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
34951 + * is very unlikely that filling the start-split FIFO will cause data loss.
34952 + * This allows much better interleaving of transactions in an order-independent way-
34953 + * there is no requirement to prioritise isochronous, just a state-space search has
34954 + * to be performed on each periodic start-split complete interrupt.
34955 + */
34956 +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
34957 +{
34958 + int hub_addr = st->channel[n].hub_addr;
34959 + int port_addr = st->channel[n].port_addr;
34960 + int i, poked = 0;
34961 + for (i = 0; i < num_channels; i++) {
34962 + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
34963 + continue;
34964 + if (st->channel[i].hub_addr == hub_addr &&
34965 + st->channel[i].port_addr == port_addr) {
34966 + switch (st->channel[i].fsm) {
34967 + case FIQ_PER_ISO_OUT_PENDING:
34968 + if (st->channel[i].nrpackets == 1) {
34969 + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
34970 + } else {
34971 + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
34972 + }
34973 + fiq_fsm_restart_channel(st, i, 0);
34974 + poked = 1;
34975 + break;
34976 +
34977 + default:
34978 + break;
34979 + }
34980 + }
34981 + if (poked)
34982 + break;
34983 + }
34984 + return poked;
34985 +}
34986 +
34987 +/**
34988 + * fiq_fsm_tt_in_use() - search for host channels using this TT
34989 + * @n: Channel to use as reference
34990 + *
34991 + */
34992 +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
34993 +{
34994 + int hub_addr = st->channel[n].hub_addr;
34995 + int port_addr = st->channel[n].port_addr;
34996 + int i, in_use = 0;
34997 + for (i = 0; i < num_channels; i++) {
34998 + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
34999 + continue;
35000 + switch (st->channel[i].fsm) {
35001 + /* TT is reserved for channels that are in the middle of a periodic
35002 + * split transaction.
35003 + */
35004 + case FIQ_PER_SSPLIT_STARTED:
35005 + case FIQ_PER_CSPLIT_WAIT:
35006 + case FIQ_PER_CSPLIT_NYET1:
35007 + //case FIQ_PER_CSPLIT_POLL:
35008 + case FIQ_PER_ISO_OUT_ACTIVE:
35009 + case FIQ_PER_ISO_OUT_LAST:
35010 + if (st->channel[i].hub_addr == hub_addr &&
35011 + st->channel[i].port_addr == port_addr) {
35012 + in_use = 1;
35013 + }
35014 + break;
35015 + default:
35016 + break;
35017 + }
35018 + if (in_use)
35019 + break;
35020 + }
35021 + return in_use;
35022 +}
35023 +
35024 +/**
35025 + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
35026 + * to be issued for this IN transaction.
35027 + *
35028 + * We cannot tell the inbound PID of a data packet due to hardware limitations.
35029 + * we need to make an educated guess as to whether we need to queue another CSPLIT
35030 + * or not. A no-brainer is when we have received enough data to fill the endpoint
35031 + * size, but for endpoints that give variable-length data then we have to resort
35032 + * to heuristics.
35033 + *
35034 + * We also return whether this is the last CSPLIT to be queued, again based on
35035 + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
35036 + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
35037 + */
35038 +
35039 +/*
35040 + * We need some way of guaranteeing if a returned periodic packet of size X
35041 + * has a DATA0 PID.
35042 + * The heuristic value of 144 bytes assumes that the received data has maximal
35043 + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
35044 + * permissible limit. If the transfer length results in a final packet size
35045 + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
35046 + * Also used to ensure that an endpoint will nominally only return a single
35047 + * complete-split worth of data.
35048 + */
35049 +#define DATA0_PID_HEURISTIC 144
35050 +
35051 +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
35052 +{
35053 +
35054 + int i;
35055 + int total_len = 0;
35056 + int more_needed = 1;
35057 + struct fiq_channel_state *st = &state->channel[n];
35058 +
35059 + for (i = 0; i < st->dma_info.index; i++) {
35060 + total_len += st->dma_info.slot_len[i];
35061 + }
35062 +
35063 + *probably_last = 0;
35064 +
35065 + if (st->hcchar_copy.b.eptype == 0x3) {
35066 + /*
35067 + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
35068 + * then this is definitely the last CSPLIT.
35069 + */
35070 + *probably_last = 1;
35071 + } else {
35072 + /* Isoc IN. This is a bit risky if we are the first transaction:
35073 + * we may have been held off slightly. */
35074 + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
35075 + more_needed = 0;
35076 + }
35077 + /* If in the next uframe we will receive enough data to fill the endpoint,
35078 + * then only issue 1 more csplit.
35079 + */
35080 + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
35081 + *probably_last = 1;
35082 + }
35083 +
35084 + if (total_len >= st->hctsiz_copy.b.xfersize ||
35085 + i == 6 || total_len == 0)
35086 + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
35087 + * a single endpoint. Accepting more would completely break our scheduling mechanism though
35088 + * - in these extreme cases we will pass through a truncated packet.
35089 + */
35090 + more_needed = 0;
35091 +
35092 + return more_needed;
35093 +}
35094 +
35095 +/**
35096 + * fiq_fsm_too_late() - Test transaction for lateness
35097 + *
35098 + * If a SSPLIT for a large IN transaction is issued too late in a frame,
35099 + * the hub will disable the port to the device and respond with ERR handshakes.
35100 + * The hub status endpoint will not reflect this change.
35101 + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
35102 + */
35103 +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
35104 +{
35105 + int uframe;
35106 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
35107 + uframe = hfnum.b.frnum & 0x7;
35108 + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
35109 + return 1;
35110 + } else {
35111 + return 0;
35112 + }
35113 +}
35114 +
35115 +
35116 +/**
35117 + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
35118 + *
35119 + * Search pending transactions in the start-split pending state and queue them.
35120 + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
35121 + * Note: we specifically don't do isochronous OUT transactions first because better
35122 + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
35123 + */
35124 +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
35125 +{
35126 + int n;
35127 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
35128 + if ((hfnum.b.frnum & 0x7) == 5)
35129 + return;
35130 + for (n = 0; n < num_channels; n++) {
35131 + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
35132 + /* Check to see if any other transactions are using this TT */
35133 + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
35134 + if (!fiq_fsm_too_late(st, n)) {
35135 + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
35136 + fiq_print(FIQDBG_INT, st, "NEXTPER ");
35137 + fiq_fsm_restart_channel(st, n, 0);
35138 + } else {
35139 + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
35140 + }
35141 + break;
35142 + }
35143 + }
35144 + }
35145 + for (n = 0; n < num_channels; n++) {
35146 + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
35147 + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
35148 + fiq_print(FIQDBG_INT, st, "NEXTISO ");
35149 + if (st->channel[n].nrpackets == 1)
35150 + st->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
35151 + else
35152 + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
35153 + fiq_fsm_restart_channel(st, n, 0);
35154 + break;
35155 + }
35156 + }
35157 + }
35158 +}
35159 +
35160 +/**
35161 + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
35162 + * @state: Pointer to fiq_state
35163 + * @n: Channel transaction is active on
35164 + * @hcint: Copy of host channel interrupt register
35165 + *
35166 + * Returns 0 if there are no more transactions for this HC to do, 1
35167 + * otherwise.
35168 + */
35169 +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
35170 +{
35171 + struct fiq_channel_state *st = &state->channel[n];
35172 + int xfer_len = 0, nrpackets = 0;
35173 + hcdma_data_t hcdma;
35174 + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
35175 +
35176 + xfer_len = fiq_get_xfer_len(state, n);
35177 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
35178 +
35179 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
35180 +
35181 + st->hs_isoc_info.index++;
35182 + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
35183 + return 0;
35184 + }
35185 +
35186 + /* grab the next DMA address offset from the array */
35187 + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
35188 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HC_DMA, hcdma.d32);
35189 +
35190 + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
35191 + * the core needs to be told to send the correct number. Caution: for IN transfers,
35192 + * this is always set to the maximum size of the endpoint. */
35193 + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
35194 + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
35195 + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
35196 + if (nrpackets == 0)
35197 + nrpackets = 1;
35198 + st->hcchar_copy.b.multicnt = nrpackets;
35199 + st->hctsiz_copy.b.pktcnt = nrpackets;
35200 +
35201 + /* Initial PID also needs to be set */
35202 + if (st->hcchar_copy.b.epdir == 0) {
35203 + st->hctsiz_copy.b.xfersize = xfer_len;
35204 + switch (st->hcchar_copy.b.multicnt) {
35205 + case 1:
35206 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
35207 + break;
35208 + case 2:
35209 + case 3:
35210 + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
35211 + break;
35212 + }
35213 +
35214 + } else {
35215 + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
35216 + switch (st->hcchar_copy.b.multicnt) {
35217 + case 1:
35218 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
35219 + break;
35220 + case 2:
35221 + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
35222 + break;
35223 + case 3:
35224 + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
35225 + break;
35226 + }
35227 + }
35228 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
35229 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
35230 + /* Channel is enabled on hcint handler exit */
35231 + fiq_print(FIQDBG_INT, state, "HSISOOUT");
35232 + return 1;
35233 +}
35234 +
35235 +
35236 +/**
35237 + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
35238 + * @state: Pointer to the state struct passed from banked FIQ mode registers.
35239 + * @num_channels: set according to the DWC hardware configuration
35240 + *
35241 + * The SOF handler in FSM mode has two functions
35242 + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
35243 + * nothing to do
35244 + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
35245 + * of holdoff.
35246 + *
35247 + * The second part is architecture-specific to mach-bcm2835 -
35248 + * a sane interrupt controller would have a mask register for ARM interrupt sources
35249 + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
35250 + * number (USB) can be enabled. This means that certain parts of the USB specification
35251 + * that require "wait a little while, then issue another packet" cannot be fulfilled with
35252 + * the timing granularity required to achieve optimal throughout. The workaround is to use
35253 + * the SOF "timer" (125uS) to perform this task.
35254 + */
35255 +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
35256 +{
35257 + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
35258 + int n;
35259 + int kick_irq = 0;
35260 +
35261 + if ((hfnum.b.frnum & 0x7) == 1) {
35262 + /* We cannot issue csplits for transactions in the last frame past (n+1).1
35263 + * Check to see if there are any transactions that are stale.
35264 + * Boot them out.
35265 + */
35266 + for (n = 0; n < num_channels; n++) {
35267 + switch (state->channel[n].fsm) {
35268 + case FIQ_PER_CSPLIT_WAIT:
35269 + case FIQ_PER_CSPLIT_NYET1:
35270 + case FIQ_PER_CSPLIT_POLL:
35271 + case FIQ_PER_CSPLIT_LAST:
35272 + /* Check if we are no longer in the same full-speed frame. */
35273 + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
35274 + (hfnum.b.frnum & ~0x7))
35275 + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
35276 + break;
35277 + default:
35278 + break;
35279 + }
35280 + }
35281 + }
35282 +
35283 + for (n = 0; n < num_channels; n++) {
35284 + switch (state->channel[n].fsm) {
35285 +
35286 + case FIQ_NP_SSPLIT_RETRY:
35287 + case FIQ_NP_IN_CSPLIT_RETRY:
35288 + case FIQ_NP_OUT_CSPLIT_RETRY:
35289 + fiq_fsm_restart_channel(state, n, 0);
35290 + break;
35291 +
35292 + case FIQ_HS_ISOC_SLEEPING:
35293 + /* Is it time to wake this channel yet? */
35294 + if (--state->channel[n].uframe_sleeps == 0) {
35295 + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
35296 + fiq_fsm_restart_channel(state, n, 0);
35297 + }
35298 + break;
35299 +
35300 + case FIQ_PER_SSPLIT_QUEUED:
35301 + if ((hfnum.b.frnum & 0x7) == 5)
35302 + break;
35303 + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
35304 + if (!fiq_fsm_too_late(state, n)) {
35305 + fiq_print(FIQDBG_INT, state, "SOF GO %01d", n);
35306 + fiq_fsm_restart_channel(state, n, 0);
35307 + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
35308 + } else {
35309 + /* Transaction cannot be started without risking a device babble error */
35310 + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
35311 + state->haintmsk_saved.b2.chint &= ~(1 << n);
35312 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
35313 + kick_irq |= 1;
35314 + }
35315 + }
35316 + break;
35317 +
35318 + case FIQ_PER_ISO_OUT_PENDING:
35319 + /* Ordinarily, this should be poked after the SSPLIT
35320 + * complete interrupt for a competing transfer on the same
35321 + * TT. Doesn't happen for aborted transactions though.
35322 + */
35323 + if ((hfnum.b.frnum & 0x7) >= 5)
35324 + break;
35325 + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
35326 + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
35327 + * that caused this.
35328 + */
35329 + fiq_fsm_restart_channel(state, n, 0);
35330 + fiq_print(FIQDBG_INT, state, "SOF ISOC");
35331 + if (state->channel[n].nrpackets == 1) {
35332 + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
35333 + } else {
35334 + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
35335 + }
35336 + }
35337 + break;
35338 +
35339 + case FIQ_PER_CSPLIT_WAIT:
35340 + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
35341 + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
35342 + * will utterly bugger this up though.
35343 + */
35344 + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
35345 + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
35346 + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
35347 + fiq_fsm_restart_channel(state, n, 0);
35348 + fiq_fsm_start_next_periodic(state, num_channels);
35349 +
35350 + }
35351 + break;
35352 +
35353 + case FIQ_PER_SPLIT_TIMEOUT:
35354 + case FIQ_DEQUEUE_ISSUED:
35355 + /* Ugly: we have to force a HCD interrupt.
35356 + * Poke the mask for the channel in question.
35357 + * We will take a fake SOF because of this, but
35358 + * that's OK.
35359 + */
35360 + state->haintmsk_saved.b2.chint &= ~(1 << n);
35361 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
35362 + kick_irq |= 1;
35363 + break;
35364 +
35365 + default:
35366 + break;
35367 + }
35368 + }
35369 +
35370 + if (state->kick_np_queues ||
35371 + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
35372 + kick_irq |= 1;
35373 +
35374 + return !kick_irq;
35375 +}
35376 +
35377 +
35378 +/**
35379 + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
35380 + * @state: Pointer to the FIQ state struct
35381 + * @num_channels: Number of channels as per hardware config
35382 + * @n: channel for which HAINT(i) was raised
35383 + *
35384 + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
35385 + */
35386 +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
35387 +{
35388 + hcint_data_t hcint;
35389 + hcintmsk_data_t hcintmsk;
35390 + hcint_data_t hcint_probe;
35391 + hcchar_data_t hcchar;
35392 + int handled = 0;
35393 + int restart = 0;
35394 + int last_csplit = 0;
35395 + int start_next_periodic = 0;
35396 + struct fiq_channel_state *st = &state->channel[n];
35397 + hfnum_data_t hfnum;
35398 +
35399 + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
35400 + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
35401 + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
35402 +
35403 + if (st->fsm != FIQ_PASSTHROUGH) {
35404 + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
35405 + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
35406 + }
35407 +
35408 + switch (st->fsm) {
35409 +
35410 + case FIQ_PASSTHROUGH:
35411 + case FIQ_DEQUEUE_ISSUED:
35412 + /* doesn't belong to us, kick it upstairs */
35413 + break;
35414 +
35415 + case FIQ_PASSTHROUGH_ERRORSTATE:
35416 + /* We are here to emulate the error recovery mechanism of the dwc HCD.
35417 + * Several interrupts are unmasked if a previous transaction failed - it's
35418 + * death for the FIQ to attempt to handle them as the channel isn't halted.
35419 + * Emulate what the HCD does in this situation: mask and continue.
35420 + * The FSM has no other state setup so this has to be handled out-of-band.
35421 + */
35422 + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
35423 + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
35424 + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
35425 + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
35426 + * interrupt, after the device has disappeared.
35427 + */
35428 + if (!hcint.b.xacterr)
35429 + st->nr_errors = 0;
35430 + hcintmsk.b.nak = 0;
35431 + hcintmsk.b.ack = 0;
35432 + hcintmsk.b.datatglerr = 0;
35433 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
35434 + return 1;
35435 + }
35436 + if (hcint_probe.b.chhltd) {
35437 + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
35438 + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
35439 + return 0;
35440 + }
35441 + break;
35442 +
35443 + /* Non-periodic state groups */
35444 + case FIQ_NP_SSPLIT_STARTED:
35445 + case FIQ_NP_SSPLIT_RETRY:
35446 + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
35447 + if (hcint.b.ack) {
35448 + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
35449 + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
35450 + */
35451 + if(st->hcchar_copy.b.epdir == 1)
35452 + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
35453 + else
35454 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35455 + st->nr_errors = 0;
35456 + handled = 1;
35457 + fiq_fsm_setup_csplit(state, n);
35458 + } else if (hcint.b.nak) {
35459 + // No buffer space in TT. Retry on a uframe boundary.
35460 + fiq_fsm_reload_hcdma(state, n);
35461 + st->fsm = FIQ_NP_SSPLIT_RETRY;
35462 + handled = 1;
35463 + } else if (hcint.b.xacterr) {
35464 + // The only other one we care about is xacterr. This implies HS bus error - retry.
35465 + st->nr_errors++;
35466 + if(st->hcchar_copy.b.epdir == 0)
35467 + fiq_fsm_reload_hcdma(state, n);
35468 + st->fsm = FIQ_NP_SSPLIT_RETRY;
35469 + if (st->nr_errors >= 3) {
35470 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35471 + } else {
35472 + handled = 1;
35473 + restart = 1;
35474 + }
35475 + } else {
35476 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35477 + handled = 0;
35478 + restart = 0;
35479 + }
35480 + break;
35481 +
35482 + case FIQ_NP_IN_CSPLIT_RETRY:
35483 + /* Received a CSPLIT done interrupt.
35484 + * Expected Data/NAK/STALL/NYET for IN.
35485 + */
35486 + if (hcint.b.xfercomp) {
35487 + /* For IN, data is present. */
35488 + st->fsm = FIQ_NP_SPLIT_DONE;
35489 + } else if (hcint.b.nak) {
35490 + /* no endpoint data. Punt it upstairs */
35491 + st->fsm = FIQ_NP_SPLIT_DONE;
35492 + } else if (hcint.b.nyet) {
35493 + /* CSPLIT NYET - retry on a uframe boundary. */
35494 + handled = 1;
35495 + st->nr_errors = 0;
35496 + } else if (hcint.b.datatglerr) {
35497 + /* data toggle errors do not set the xfercomp bit. */
35498 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35499 + } else if (hcint.b.xacterr) {
35500 + /* HS error. Retry immediate */
35501 + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
35502 + st->nr_errors++;
35503 + if (st->nr_errors >= 3) {
35504 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35505 + } else {
35506 + handled = 1;
35507 + restart = 1;
35508 + }
35509 + } else if (hcint.b.stall || hcint.b.bblerr) {
35510 + /* A STALL implies either a LS bus error or a genuine STALL. */
35511 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35512 + } else {
35513 + /* Hardware bug. It's possible in some cases to
35514 + * get a channel halt with nothing else set when
35515 + * the response was a NYET. Treat as local 3-strikes retry.
35516 + */
35517 + hcint_data_t hcint_test = hcint;
35518 + hcint_test.b.chhltd = 0;
35519 + if (!hcint_test.d32) {
35520 + st->nr_errors++;
35521 + if (st->nr_errors >= 3) {
35522 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35523 + } else {
35524 + handled = 1;
35525 + }
35526 + } else {
35527 + /* Bail out if something unexpected happened */
35528 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35529 + }
35530 + }
35531 + if (st->fsm != FIQ_NP_IN_CSPLIT_RETRY) {
35532 + fiq_fsm_restart_np_pending(state, num_channels, n);
35533 + }
35534 + break;
35535 +
35536 + case FIQ_NP_OUT_CSPLIT_RETRY:
35537 + /* Received a CSPLIT done interrupt.
35538 + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
35539 + if (hcint.b.xfercomp) {
35540 + st->fsm = FIQ_NP_SPLIT_DONE;
35541 + } else if (hcint.b.nak) {
35542 + // The HCD will implement the holdoff on frame boundaries.
35543 + st->fsm = FIQ_NP_SPLIT_DONE;
35544 + } else if (hcint.b.nyet) {
35545 + // Hub still processing.
35546 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35547 + handled = 1;
35548 + st->nr_errors = 0;
35549 + //restart = 1;
35550 + } else if (hcint.b.xacterr) {
35551 + /* HS error. retry immediate */
35552 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35553 + st->nr_errors++;
35554 + if (st->nr_errors >= 3) {
35555 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35556 + } else {
35557 + handled = 1;
35558 + restart = 1;
35559 + }
35560 + } else if (hcint.b.stall) {
35561 + /* LS bus error or genuine stall */
35562 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35563 + } else {
35564 + /*
35565 + * Hardware bug. It's possible in some cases to get a
35566 + * channel halt with nothing else set when the response was a NYET.
35567 + * Treat as local 3-strikes retry.
35568 + */
35569 + hcint_data_t hcint_test = hcint;
35570 + hcint_test.b.chhltd = 0;
35571 + if (!hcint_test.d32) {
35572 + st->nr_errors++;
35573 + if (st->nr_errors >= 3) {
35574 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35575 + } else {
35576 + handled = 1;
35577 + }
35578 + } else {
35579 + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
35580 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35581 + }
35582 + }
35583 + if (st->fsm != FIQ_NP_OUT_CSPLIT_RETRY) {
35584 + fiq_fsm_restart_np_pending(state, num_channels, n);
35585 + }
35586 + break;
35587 +
35588 + /* Periodic split states (except isoc out) */
35589 + case FIQ_PER_SSPLIT_STARTED:
35590 + /* Expect an ACK or failure for SSPLIT */
35591 + if (hcint.b.ack) {
35592 + /*
35593 + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
35594 + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
35595 + * point for microframe n-3, the packet will not appear on the bus until microframe n.
35596 + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
35597 + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
35598 + * coincident with SOF for n+1.
35599 + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
35600 + * These appear to be caused by timing/clock crossing bugs within the core itself.
35601 + * State machine workaround.
35602 + */
35603 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35604 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35605 + fiq_fsm_setup_csplit(state, n);
35606 + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
35607 + * time. If not, then we're in the next SOF.
35608 + */
35609 + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
35610 + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
35611 + st->expected_uframe = hfnum.b.frnum;
35612 + st->fsm = FIQ_PER_CSPLIT_WAIT;
35613 + } else {
35614 + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
35615 + /* For isochronous IN endpoints,
35616 + * we need to hold off if we are expecting a lot of data */
35617 + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
35618 + start_next_periodic = 1;
35619 + }
35620 + /* Danger will robinson: we are in a broken state. If our first interrupt after
35621 + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
35622 + * lag. Unmask the NYET interrupt.
35623 + */
35624 + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
35625 + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
35626 + restart = 1;
35627 + }
35628 + handled = 1;
35629 + } else if (hcint.b.xacterr) {
35630 + /* 3-strikes retry is enabled, we have hit our max nr_errors */
35631 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35632 + start_next_periodic = 1;
35633 + } else {
35634 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35635 + start_next_periodic = 1;
35636 + }
35637 + /* We can now queue the next isochronous OUT transaction, if one is pending. */
35638 + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
35639 + fiq_print(FIQDBG_INT, state, "NEXTISO ");
35640 + }
35641 + break;
35642 +
35643 + case FIQ_PER_CSPLIT_NYET1:
35644 + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
35645 + * we are too late and the TT has dropped its CSPLIT fifo.
35646 + */
35647 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35648 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35649 + start_next_periodic = 1;
35650 + if (hcint.b.nak) {
35651 + st->fsm = FIQ_PER_SPLIT_DONE;
35652 + } else if (hcint.b.xfercomp) {
35653 + fiq_increment_dma_buf(state, num_channels, n);
35654 + st->fsm = FIQ_PER_CSPLIT_POLL;
35655 + st->nr_errors = 0;
35656 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35657 + handled = 1;
35658 + restart = 1;
35659 + if (!last_csplit)
35660 + start_next_periodic = 0;
35661 + } else {
35662 + st->fsm = FIQ_PER_SPLIT_DONE;
35663 + }
35664 + } else if (hcint.b.nyet) {
35665 + /* Doh. Data lost. */
35666 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35667 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35668 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35669 + } else {
35670 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35671 + }
35672 + break;
35673 +
35674 + case FIQ_PER_CSPLIT_BROKEN_NYET1:
35675 + /*
35676 + * we got here because our host channel is in the delayed-interrupt
35677 + * state and we cannot take a NYET interrupt any later than when it
35678 + * occurred. Disable then re-enable the channel if this happens to force
35679 + * CSPLITs to occur at the right time.
35680 + */
35681 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35682 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35683 + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
35684 + if (hcint.b.nak) {
35685 + st->fsm = FIQ_PER_SPLIT_DONE;
35686 + start_next_periodic = 1;
35687 + } else if (hcint.b.xfercomp) {
35688 + fiq_increment_dma_buf(state, num_channels, n);
35689 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35690 + st->fsm = FIQ_PER_CSPLIT_POLL;
35691 + handled = 1;
35692 + restart = 1;
35693 + start_next_periodic = 1;
35694 + /* Reload HCTSIZ for the next transfer */
35695 + fiq_fsm_reload_hctsiz(state, n);
35696 + if (!last_csplit)
35697 + start_next_periodic = 0;
35698 + } else {
35699 + st->fsm = FIQ_PER_SPLIT_DONE;
35700 + }
35701 + } else if (hcint.b.nyet) {
35702 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35703 + start_next_periodic = 1;
35704 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35705 + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
35706 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35707 + } else {
35708 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35709 + }
35710 + break;
35711 +
35712 + case FIQ_PER_CSPLIT_POLL:
35713 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35714 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35715 + start_next_periodic = 1;
35716 + if (hcint.b.nak) {
35717 + st->fsm = FIQ_PER_SPLIT_DONE;
35718 + } else if (hcint.b.xfercomp) {
35719 + fiq_increment_dma_buf(state, num_channels, n);
35720 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35721 + handled = 1;
35722 + restart = 1;
35723 + /* Reload HCTSIZ for the next transfer */
35724 + fiq_fsm_reload_hctsiz(state, n);
35725 + if (!last_csplit)
35726 + start_next_periodic = 0;
35727 + } else {
35728 + st->fsm = FIQ_PER_SPLIT_DONE;
35729 + }
35730 + } else if (hcint.b.nyet) {
35731 + /* Are we a NYET after the first data packet? */
35732 + if (st->nrpackets == 0) {
35733 + st->fsm = FIQ_PER_CSPLIT_NYET1;
35734 + handled = 1;
35735 + restart = 1;
35736 + } else {
35737 + /* We got a NYET when polling CSPLITs. Can happen
35738 + * if our heuristic fails, or if someone disables us
35739 + * for any significant length of time.
35740 + */
35741 + if (st->nr_errors >= 3) {
35742 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35743 + } else {
35744 + st->fsm = FIQ_PER_SPLIT_DONE;
35745 + }
35746 + }
35747 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35748 + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
35749 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35750 + } else {
35751 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35752 + }
35753 + break;
35754 +
35755 + case FIQ_HS_ISOC_TURBO:
35756 + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
35757 + /* more transactions to come */
35758 + handled = 1;
35759 + fiq_print(FIQDBG_INT, state, "HSISO M ");
35760 + /* For strided transfers, put ourselves to sleep */
35761 + if (st->hs_isoc_info.stride > 1) {
35762 + st->uframe_sleeps = st->hs_isoc_info.stride - 1;
35763 + st->fsm = FIQ_HS_ISOC_SLEEPING;
35764 + } else {
35765 + restart = 1;
35766 + }
35767 + } else {
35768 + st->fsm = FIQ_HS_ISOC_DONE;
35769 + fiq_print(FIQDBG_INT, state, "HSISO F ");
35770 + }
35771 + break;
35772 +
35773 + case FIQ_HS_ISOC_ABORTED:
35774 + /* This abort is called by the driver rewriting the state mid-transaction
35775 + * which allows the dequeue mechanism to work more effectively.
35776 + */
35777 + break;
35778 +
35779 + case FIQ_PER_ISO_OUT_ACTIVE:
35780 + if (hcint.b.ack) {
35781 + if(fiq_iso_out_advance(state, num_channels, n)) {
35782 + /* last OUT transfer */
35783 + st->fsm = FIQ_PER_ISO_OUT_LAST;
35784 + /*
35785 + * Assuming the periodic FIFO in the dwc core
35786 + * actually does its job properly, we can queue
35787 + * the next ssplit now and in theory, the wire
35788 + * transactions will be in-order.
35789 + */
35790 + // No it doesn't. It appears to process requests in host channel order.
35791 + //start_next_periodic = 1;
35792 + }
35793 + handled = 1;
35794 + restart = 1;
35795 + } else {
35796 + /*
35797 + * Isochronous transactions carry on regardless. Log the error
35798 + * and continue.
35799 + */
35800 + //explode += 1;
35801 + st->nr_errors++;
35802 + if(fiq_iso_out_advance(state, num_channels, n)) {
35803 + st->fsm = FIQ_PER_ISO_OUT_LAST;
35804 + //start_next_periodic = 1;
35805 + }
35806 + handled = 1;
35807 + restart = 1;
35808 + }
35809 + break;
35810 +
35811 + case FIQ_PER_ISO_OUT_LAST:
35812 + if (hcint.b.ack) {
35813 + /* All done here */
35814 + st->fsm = FIQ_PER_ISO_OUT_DONE;
35815 + } else {
35816 + st->fsm = FIQ_PER_ISO_OUT_DONE;
35817 + st->nr_errors++;
35818 + }
35819 + start_next_periodic = 1;
35820 + break;
35821 +
35822 + case FIQ_PER_SPLIT_TIMEOUT:
35823 + /* SOF kicked us because we overran. */
35824 + start_next_periodic = 1;
35825 + break;
35826 +
35827 + default:
35828 + break;
35829 + }
35830 +
35831 + if (handled) {
35832 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
35833 + } else {
35834 + /* Copy the regs into the state so the IRQ knows what to do */
35835 + st->hcint_copy.d32 = hcint.d32;
35836 + }
35837 +
35838 + if (restart) {
35839 + /* Restart always implies handled. */
35840 + if (restart == 2) {
35841 + /* For complete-split INs, the show must go on.
35842 + * Force a channel restart */
35843 + fiq_fsm_restart_channel(state, n, 1);
35844 + } else {
35845 + fiq_fsm_restart_channel(state, n, 0);
35846 + }
35847 + }
35848 + if (start_next_periodic) {
35849 + fiq_fsm_start_next_periodic(state, num_channels);
35850 + }
35851 + if (st->fsm != FIQ_PASSTHROUGH)
35852 + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
35853 +
35854 + return handled;
35855 +}
35856 +
35857 +
35858 +/**
35859 + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
35860 + * @state: pointer to state struct passed from the banked FIQ mode registers.
35861 + * @num_channels: set according to the DWC hardware configuration
35862 + * @dma: pointer to DMA bounce buffers for split transaction slots
35863 + *
35864 + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
35865 + * inside an EHCI or similar host controller regarding split transactions. The DWC core
35866 + * interrupts each and every time a split transaction packet is received or sent successfully.
35867 + * This results in either an interrupt storm when everything is working "properly", or
35868 + * the interrupt latency of the system in general breaks time-sensitive periodic split
35869 + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
35870 + * solves these problems.
35871 + *
35872 + * Return: void
35873 + */
35874 +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
35875 +{
35876 + gintsts_data_t gintsts, gintsts_handled;
35877 + gintmsk_data_t gintmsk;
35878 + //hfnum_data_t hfnum;
35879 + haint_data_t haint, haint_handled;
35880 + haintmsk_data_t haintmsk;
35881 + int kick_irq = 0;
35882 +
35883 + gintsts_handled.d32 = 0;
35884 + haint_handled.d32 = 0;
35885 +
35886 + fiq_fsm_spin_lock(&state->lock);
35887 + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
35888 + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
35889 + gintsts.d32 &= gintmsk.d32;
35890 +
35891 + if (gintsts.b.sofintr) {
35892 + /* For FSM mode, SOF is required to keep the state machine advance for
35893 + * certain stages of the periodic pipeline. It's death to mask this
35894 + * interrupt in that case.
35895 + */
35896 +
35897 + if (!fiq_fsm_do_sof(state, num_channels)) {
35898 + /* Kick IRQ once. Queue advancement means that all pending transactions
35899 + * will get serviced when the IRQ finally executes.
35900 + */
35901 + if (state->gintmsk_saved.b.sofintr == 1)
35902 + kick_irq |= 1;
35903 + state->gintmsk_saved.b.sofintr = 0;
35904 + }
35905 + gintsts_handled.b.sofintr = 1;
35906 + }
35907 +
35908 + if (gintsts.b.hcintr) {
35909 + int i;
35910 + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
35911 + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
35912 + haint.d32 &= haintmsk.d32;
35913 + haint_handled.d32 = 0;
35914 + for (i=0; i<num_channels; i++) {
35915 + if (haint.b2.chint & (1 << i)) {
35916 + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
35917 + /* HCINT was not handled in FIQ
35918 + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
35919 + * Mask HAINT(i) but keep top-level hcint unmasked.
35920 + */
35921 + state->haintmsk_saved.b2.chint &= ~(1 << i);
35922 + } else {
35923 + /* do_hcintr cleaned up after itself, but clear haint */
35924 + haint_handled.b2.chint |= (1 << i);
35925 + }
35926 + }
35927 + }
35928 +
35929 + if (haint_handled.b2.chint) {
35930 + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
35931 + }
35932 +
35933 + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
35934 + /*
35935 + * This is necessary to avoid multiple retriggers of the MPHI in the case
35936 + * where interrupts are held off and HCINTs start to pile up.
35937 + * Only wake up the IRQ if a new interrupt came in, was not handled and was
35938 + * masked.
35939 + */
35940 + haintmsk.d32 &= state->haintmsk_saved.d32;
35941 + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
35942 + kick_irq |= 1;
35943 + }
35944 + /* Top-Level interrupt - always handled because it's level-sensitive */
35945 + gintsts_handled.b.hcintr = 1;
35946 + }
35947 +
35948 +
35949 + /* Clear the bits in the saved register that were not handled but were triggered. */
35950 + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
35951 +
35952 + /* FIQ didn't handle something - mask has changed - write new mask */
35953 + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
35954 + gintmsk.d32 &= state->gintmsk_saved.d32;
35955 + gintmsk.b.sofintr = 1;
35956 + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
35957 +// fiq_print(FIQDBG_INT, state, "KICKGINT");
35958 +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
35959 +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
35960 + kick_irq |= 1;
35961 + }
35962 +
35963 + if (gintsts_handled.d32) {
35964 + /* Only applies to edge-sensitive bits in GINTSTS */
35965 + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
35966 + }
35967 +
35968 + /* We got an interrupt, didn't handle it. */
35969 + if (kick_irq) {
35970 + state->mphi_int_count++;
35971 + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
35972 + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
35973 +
35974 + }
35975 + state->fiq_done++;
35976 + mb();
35977 + fiq_fsm_spin_unlock(&state->lock);
35978 +}
35979 +
35980 +
35981 +/**
35982 + * dwc_otg_fiq_nop() - FIQ "lite"
35983 + * @state: pointer to state struct passed from the banked FIQ mode registers.
35984 + *
35985 + * The "nop" handler does not intervene on any interrupts other than SOF.
35986 + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
35987 + * with non-periodic/periodic queues) needs to be kicked.
35988 + *
35989 + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
35990 + *
35991 + * Return: void
35992 + */
35993 +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
35994 +{
35995 + gintsts_data_t gintsts, gintsts_handled;
35996 + gintmsk_data_t gintmsk;
35997 + hfnum_data_t hfnum;
35998 +
35999 + fiq_fsm_spin_lock(&state->lock);
36000 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
36001 + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
36002 + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
36003 + gintsts.d32 &= gintmsk.d32;
36004 + gintsts_handled.d32 = 0;
36005 +
36006 + if (gintsts.b.sofintr) {
36007 + if (!state->kick_np_queues &&
36008 + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
36009 + /* SOF handled, no work to do, just ACK interrupt */
36010 + gintsts_handled.b.sofintr = 1;
36011 + } else {
36012 + /* Kick IRQ */
36013 + state->gintmsk_saved.b.sofintr = 0;
36014 + }
36015 + }
36016 +
36017 + /* Reset handled interrupts */
36018 + if(gintsts_handled.d32) {
36019 + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
36020 + }
36021 +
36022 + /* Clear the bits in the saved register that were not handled but were triggered. */
36023 + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
36024 +
36025 + /* We got an interrupt, didn't handle it and want to mask it */
36026 + if (~(state->gintmsk_saved.d32)) {
36027 + state->mphi_int_count++;
36028 + gintmsk.d32 &= state->gintmsk_saved.d32;
36029 + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
36030 + /* Force a clear before another dummy send */
36031 + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
36032 + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
36033 + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
36034 +
36035 + }
36036 + state->fiq_done++;
36037 + mb();
36038 + fiq_fsm_spin_unlock(&state->lock);
36039 +}
36040 --- /dev/null
36041 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
36042 @@ -0,0 +1,372 @@
36043 +/*
36044 + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
36045 + *
36046 + * Copyright (c) 2013 Raspberry Pi Foundation
36047 + *
36048 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
36049 + * All rights reserved.
36050 + *
36051 + * Redistribution and use in source and binary forms, with or without
36052 + * modification, are permitted provided that the following conditions are met:
36053 + * * Redistributions of source code must retain the above copyright
36054 + * notice, this list of conditions and the following disclaimer.
36055 + * * Redistributions in binary form must reproduce the above copyright
36056 + * notice, this list of conditions and the following disclaimer in the
36057 + * documentation and/or other materials provided with the distribution.
36058 + * * Neither the name of Raspberry Pi nor the
36059 + * names of its contributors may be used to endorse or promote products
36060 + * derived from this software without specific prior written permission.
36061 + *
36062 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
36063 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36064 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36065 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
36066 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36067 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36068 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36069 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36070 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36071 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36072 + *
36073 + * This FIQ implements functionality that performs split transactions on
36074 + * the dwc_otg hardware without any outside intervention. A split transaction
36075 + * is "queued" by nominating a specific host channel to perform the entirety
36076 + * of a split transaction. This FIQ will then perform the microframe-precise
36077 + * scheduling required in each phase of the transaction until completion.
36078 + *
36079 + * The FIQ functionality has been surgically implanted into the Synopsys
36080 + * vendor-provided driver.
36081 + *
36082 + */
36083 +
36084 +#ifndef DWC_OTG_FIQ_FSM_H_
36085 +#define DWC_OTG_FIQ_FSM_H_
36086 +
36087 +#include "dwc_otg_regs.h"
36088 +#include "dwc_otg_cil.h"
36089 +#include "dwc_otg_hcd.h"
36090 +#include <linux/kernel.h>
36091 +#include <linux/irqflags.h>
36092 +#include <linux/string.h>
36093 +#include <asm/barrier.h>
36094 +
36095 +#if 0
36096 +#define FLAME_ON(x) \
36097 +do { \
36098 + int gpioreg; \
36099 + \
36100 + gpioreg = readl(__io_address(0x20200000+0x8)); \
36101 + gpioreg &= ~(7 << (x-20)*3); \
36102 + gpioreg |= 0x1 << (x-20)*3; \
36103 + writel(gpioreg, __io_address(0x20200000+0x8)); \
36104 + \
36105 + writel(1<<x, __io_address(0x20200000+(0x1C))); \
36106 +} while (0)
36107 +
36108 +#define FLAME_OFF(x) \
36109 +do { \
36110 + writel(1<<x, __io_address(0x20200000+(0x28))); \
36111 +} while (0)
36112 +#else
36113 +#define FLAME_ON(x) do { } while (0)
36114 +#define FLAME_OFF(X) do { } while (0)
36115 +#endif
36116 +
36117 +/* This is a quick-and-dirty arch-specific register read/write. We know that
36118 + * writes to a peripheral on BCM2835 will always arrive in-order, also that
36119 + * reads and writes are executed in-order therefore the need for memory barriers
36120 + * is obviated if we're only talking to USB.
36121 + */
36122 +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
36123 +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
36124 +
36125 +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
36126 +#define GINTSTS 0x014
36127 +#define GINTMSK 0x018
36128 +/* Debug register. Poll the top of the received packets FIFO. */
36129 +#define GRXSTSR 0x01C
36130 +#define HFNUM 0x408
36131 +#define HAINT 0x414
36132 +#define HAINTMSK 0x418
36133 +#define HPRT0 0x440
36134 +
36135 +/* HC_regs start from an offset of 0x500 */
36136 +#define HC_START 0x500
36137 +#define HC_OFFSET 0x020
36138 +
36139 +#define HC_DMA 0x14
36140 +
36141 +#define HCCHAR 0x00
36142 +#define HCSPLT 0x04
36143 +#define HCINT 0x08
36144 +#define HCINTMSK 0x0C
36145 +#define HCTSIZ 0x10
36146 +
36147 +#define ISOC_XACTPOS_ALL 0b11
36148 +#define ISOC_XACTPOS_BEGIN 0b10
36149 +#define ISOC_XACTPOS_MID 0b00
36150 +#define ISOC_XACTPOS_END 0b01
36151 +
36152 +#define DWC_PID_DATA2 0b01
36153 +#define DWC_PID_MDATA 0b11
36154 +#define DWC_PID_DATA1 0b10
36155 +#define DWC_PID_DATA0 0b00
36156 +
36157 +typedef struct {
36158 + volatile void* base;
36159 + volatile void* ctrl;
36160 + volatile void* outdda;
36161 + volatile void* outddb;
36162 + volatile void* intstat;
36163 +} mphi_regs_t;
36164 +
36165 +enum fiq_debug_level {
36166 + FIQDBG_SCHED = (1 << 0),
36167 + FIQDBG_INT = (1 << 1),
36168 + FIQDBG_ERR = (1 << 2),
36169 + FIQDBG_PORTHUB = (1 << 3),
36170 +};
36171 +
36172 +typedef struct {
36173 + union {
36174 + uint32_t slock;
36175 + struct _tickets {
36176 + uint16_t owner;
36177 + uint16_t next;
36178 + } tickets;
36179 + };
36180 +} fiq_lock_t;
36181 +
36182 +struct fiq_state;
36183 +
36184 +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
36185 +#if 0
36186 +#define fiq_print _fiq_print
36187 +#else
36188 +#define fiq_print(x, y, ...)
36189 +#endif
36190 +
36191 +extern bool fiq_enable, fiq_fsm_enable;
36192 +extern ushort nak_holdoff;
36193 +
36194 +/**
36195 + * enum fiq_fsm_state - The FIQ FSM states.
36196 + *
36197 + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
36198 + * USB2.0 specification for host responses to various transaction states.
36199 + * There are modifications to this host state machine because of a variety of
36200 + * quirks and limitations in the dwc_otg hardware.
36201 + *
36202 + * The fsm state is also used to communicate back to the driver on completion of
36203 + * a split transaction. The end states are used in conjunction with the interrupts
36204 + * raised by the final transaction.
36205 + */
36206 +enum fiq_fsm_state {
36207 + /* FIQ isn't enabled for this host channel */
36208 + FIQ_PASSTHROUGH = 0,
36209 + /* For the first interrupt received for this channel,
36210 + * the FIQ has to ack any interrupts indicating success. */
36211 + FIQ_PASSTHROUGH_ERRORSTATE = 31,
36212 + /* Nonperiodic state groups */
36213 + FIQ_NP_SSPLIT_STARTED = 1,
36214 + FIQ_NP_SSPLIT_RETRY = 2,
36215 + /* TT contention - working around hub bugs */
36216 + FIQ_NP_SSPLIT_PENDING = 33,
36217 + FIQ_NP_OUT_CSPLIT_RETRY = 3,
36218 + FIQ_NP_IN_CSPLIT_RETRY = 4,
36219 + FIQ_NP_SPLIT_DONE = 5,
36220 + FIQ_NP_SPLIT_LS_ABORTED = 6,
36221 + /* This differentiates a HS transaction error from a LS one
36222 + * (handling the hub state is different) */
36223 + FIQ_NP_SPLIT_HS_ABORTED = 7,
36224 +
36225 + /* Periodic state groups */
36226 + /* Periodic transactions are either started directly by the IRQ handler
36227 + * or deferred if the TT is already in use.
36228 + */
36229 + FIQ_PER_SSPLIT_QUEUED = 8,
36230 + FIQ_PER_SSPLIT_STARTED = 9,
36231 + FIQ_PER_SSPLIT_LAST = 10,
36232 +
36233 +
36234 + FIQ_PER_ISO_OUT_PENDING = 11,
36235 + FIQ_PER_ISO_OUT_ACTIVE = 12,
36236 + FIQ_PER_ISO_OUT_LAST = 13,
36237 + FIQ_PER_ISO_OUT_DONE = 27,
36238 +
36239 + FIQ_PER_CSPLIT_WAIT = 14,
36240 + FIQ_PER_CSPLIT_NYET1 = 15,
36241 + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
36242 + FIQ_PER_CSPLIT_NYET_FAFF = 29,
36243 + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
36244 + FIQ_PER_CSPLIT_POLL = 16,
36245 + /* The last CSPLIT for a transaction has been issued, differentiates
36246 + * for the state machine to queue the next packet.
36247 + */
36248 + FIQ_PER_CSPLIT_LAST = 17,
36249 +
36250 + FIQ_PER_SPLIT_DONE = 18,
36251 + FIQ_PER_SPLIT_LS_ABORTED = 19,
36252 + FIQ_PER_SPLIT_HS_ABORTED = 20,
36253 + FIQ_PER_SPLIT_NYET_ABORTED = 21,
36254 + /* Frame rollover has occurred without the transaction finishing. */
36255 + FIQ_PER_SPLIT_TIMEOUT = 22,
36256 +
36257 + /* FIQ-accelerated HS Isochronous state groups */
36258 + FIQ_HS_ISOC_TURBO = 23,
36259 + /* For interval > 1, SOF wakes up the isochronous FSM */
36260 + FIQ_HS_ISOC_SLEEPING = 24,
36261 + FIQ_HS_ISOC_DONE = 25,
36262 + FIQ_HS_ISOC_ABORTED = 26,
36263 + FIQ_DEQUEUE_ISSUED = 30,
36264 + FIQ_TEST = 32,
36265 +};
36266 +
36267 +struct fiq_stack {
36268 + int magic1;
36269 + uint8_t stack[2048];
36270 + int magic2;
36271 +};
36272 +
36273 +
36274 +/**
36275 + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
36276 + * @index: Number of slots reported used for IN transactions / number of slots
36277 + * transmitted for an OUT transaction
36278 + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
36279 + *
36280 + * Split transaction transfers can have variable length depending on other bus
36281 + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
36282 + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
36283 + * can happen per-frame.
36284 + */
36285 +struct fiq_dma_info {
36286 + u8 index;
36287 + u8 slot_len[6];
36288 +};
36289 +
36290 +struct __attribute__((packed)) fiq_split_dma_slot {
36291 + u8 buf[188];
36292 +};
36293 +
36294 +struct fiq_dma_channel {
36295 + struct __attribute__((packed)) fiq_split_dma_slot index[6];
36296 +};
36297 +
36298 +struct fiq_dma_blob {
36299 + struct __attribute__((packed)) fiq_dma_channel channel[0];
36300 +};
36301 +
36302 +/**
36303 + * struct fiq_hs_isoc_info - USB2.0 isochronous data
36304 + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
36305 + * @nrframes: Total length of iso_frame_desc array
36306 + * @index: Current index (FIQ-maintained)
36307 + * @stride: Interval in uframes between HS isoc transactions
36308 + */
36309 +struct fiq_hs_isoc_info {
36310 + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
36311 + unsigned int nrframes;
36312 + unsigned int index;
36313 + unsigned int stride;
36314 +};
36315 +
36316 +/**
36317 + * struct fiq_channel_state - FIQ state machine storage
36318 + * @fsm: Current state of the channel as understood by the FIQ
36319 + * @nr_errors: Number of transaction errors on this split-transaction
36320 + * @hub_addr: SSPLIT/CSPLIT destination hub
36321 + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
36322 + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
36323 + * split-IN, number of CSPLIT data packets that were received.
36324 + * @hcchar_copy:
36325 + * @hcsplt_copy:
36326 + * @hcintmsk_copy:
36327 + * @hctsiz_copy: Copies of the host channel registers.
36328 + * For use as scratch, or for returning state.
36329 + *
36330 + * The fiq_channel_state is state storage between interrupts for a host channel. The
36331 + * FSM state is stored here. Members of this structure must only be set up by the
36332 + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
36333 + * has updated the state to either a COMPLETE state group or ABORT state group.
36334 + */
36335 +
36336 +struct fiq_channel_state {
36337 + enum fiq_fsm_state fsm;
36338 + unsigned int nr_errors;
36339 + unsigned int hub_addr;
36340 + unsigned int port_addr;
36341 + /* Hardware bug workaround: sometimes channel halt interrupts are
36342 + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
36343 + unsigned int expected_uframe;
36344 + /* number of uframes remaining (for interval > 1 HS isoc transfers) before next transfer */
36345 + unsigned int uframe_sleeps;
36346 + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
36347 + unsigned int nrpackets;
36348 + struct fiq_dma_info dma_info;
36349 + struct fiq_hs_isoc_info hs_isoc_info;
36350 + /* Copies of HC registers - in/out communication from/to IRQ handler
36351 + * and for ease of channel setup. A bit of mungeing is performed - for
36352 + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
36353 + */
36354 + hcchar_data_t hcchar_copy;
36355 + hcsplt_data_t hcsplt_copy;
36356 + hcint_data_t hcint_copy;
36357 + hcintmsk_data_t hcintmsk_copy;
36358 + hctsiz_data_t hctsiz_copy;
36359 + hcdma_data_t hcdma_copy;
36360 +};
36361 +
36362 +/**
36363 + * struct fiq_state - top-level FIQ state machine storage
36364 + * @mphi_regs: virtual address of the MPHI peripheral register file
36365 + * @dwc_regs_base: virtual address of the base of the DWC core register file
36366 + * @dma_base: physical address for the base of the DMA bounce buffers
36367 + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
36368 + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
36369 + * Used for determining which interrupts fired to set off the IRQ handler.
36370 + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
36371 + * @np_count: Non-periodic transactions in the active queue
36372 + * @np_sent: Count of non-periodic transactions that have completed
36373 + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
36374 + * this is the next frame on which a SOF interrupt is required. Used to hold off
36375 + * passing SOF through to the driver until necessary.
36376 + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
36377 + * channels configured into the core logic.
36378 + *
36379 + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
36380 + * It contains top-level state information.
36381 + */
36382 +struct fiq_state {
36383 + fiq_lock_t lock;
36384 + mphi_regs_t mphi_regs;
36385 + void *dwc_regs_base;
36386 + dma_addr_t dma_base;
36387 + struct fiq_dma_blob *fiq_dmab;
36388 + void *dummy_send;
36389 + gintmsk_data_t gintmsk_saved;
36390 + haintmsk_data_t haintmsk_saved;
36391 + int mphi_int_count;
36392 + unsigned int fiq_done;
36393 + unsigned int kick_np_queues;
36394 + unsigned int next_sched_frame;
36395 +#ifdef FIQ_DEBUG
36396 + char * buffer;
36397 + unsigned int bufsiz;
36398 +#endif
36399 + struct fiq_channel_state channel[0];
36400 +};
36401 +
36402 +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
36403 +
36404 +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
36405 +
36406 +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
36407 +
36408 +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
36409 +
36410 +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
36411 +
36412 +extern void dwc_otg_fiq_nop(struct fiq_state *state);
36413 +
36414 +#endif /* DWC_OTG_FIQ_FSM_H_ */
36415 --- /dev/null
36416 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
36417 @@ -0,0 +1,80 @@
36418 +/*
36419 + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
36420 + *
36421 + * Copyright (c) 2013 Raspberry Pi Foundation
36422 + *
36423 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
36424 + * All rights reserved.
36425 + *
36426 + * Redistribution and use in source and binary forms, with or without
36427 + * modification, are permitted provided that the following conditions are met:
36428 + * * Redistributions of source code must retain the above copyright
36429 + * notice, this list of conditions and the following disclaimer.
36430 + * * Redistributions in binary form must reproduce the above copyright
36431 + * notice, this list of conditions and the following disclaimer in the
36432 + * documentation and/or other materials provided with the distribution.
36433 + * * Neither the name of Raspberry Pi nor the
36434 + * names of its contributors may be used to endorse or promote products
36435 + * derived from this software without specific prior written permission.
36436 + *
36437 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
36438 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36439 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36440 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
36441 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36442 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36443 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36444 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36445 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36446 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36447 + */
36448 +
36449 +
36450 +#include <asm/assembler.h>
36451 +#include <linux/linkage.h>
36452 +
36453 +
36454 +.text
36455 +
36456 +.global _dwc_otg_fiq_stub_end;
36457 +
36458 +/**
36459 + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
36460 + * a C-style function call with arguments from the FIQ banked registers.
36461 + * r0 = &hcd->fiq_state
36462 + * r1 = &hcd->num_channels
36463 + * r2 = &hcd->dma_buffers
36464 + * Tramples: r0, r1, r2, r4, fp, ip
36465 + */
36466 +
36467 +ENTRY(_dwc_otg_fiq_stub)
36468 + /* Stash unbanked regs - SP will have been set up for us */
36469 + mov ip, sp;
36470 + stmdb sp!, {r0-r12, lr};
36471 +#ifdef FIQ_DEBUG
36472 + // Cycle profiling - read cycle counter at start
36473 + mrc p15, 0, r5, c15, c12, 1;
36474 +#endif
36475 + /* r11 = fp, don't trample it */
36476 + mov r4, fp;
36477 + /* set EABI frame size */
36478 + sub fp, ip, #512;
36479 +
36480 + /* for fiq NOP mode - just need state */
36481 + mov r0, r8;
36482 + /* r9 = num_channels */
36483 + mov r1, r9;
36484 + /* r10 = struct *dma_bufs */
36485 +// mov r2, r10;
36486 +
36487 + /* r4 = &fiq_c_function */
36488 + blx r4;
36489 +#ifdef FIQ_DEBUG
36490 + mrc p15, 0, r4, c15, c12, 1;
36491 + subs r5, r5, r4;
36492 + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
36493 +#endif
36494 + ldmia sp!, {r0-r12, lr};
36495 + subs pc, lr, #4;
36496 +_dwc_otg_fiq_stub_end:
36497 +END(_dwc_otg_fiq_stub)
36498 --- /dev/null
36499 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
36500 @@ -0,0 +1,4297 @@
36501 +
36502 +/* ==========================================================================
36503 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
36504 + * $Revision: #104 $
36505 + * $Date: 2011/10/24 $
36506 + * $Change: 1871159 $
36507 + *
36508 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
36509 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
36510 + * otherwise expressly agreed to in writing between Synopsys and you.
36511 + *
36512 + * The Software IS NOT an item of Licensed Software or Licensed Product under
36513 + * any End User Software License Agreement or Agreement for Licensed Product
36514 + * with Synopsys or any supplement thereto. You are permitted to use and
36515 + * redistribute this Software in source and binary forms, with or without
36516 + * modification, provided that redistributions of source code must retain this
36517 + * notice. You may not view, use, disclose, copy or distribute this file or
36518 + * any information contained herein except pursuant to this license grant from
36519 + * Synopsys. If you do not agree with this notice, including the disclaimer
36520 + * below, then you are not authorized to use the Software.
36521 + *
36522 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
36523 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36524 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36525 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
36526 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36527 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36528 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36529 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36530 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36531 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
36532 + * DAMAGE.
36533 + * ========================================================================== */
36534 +#ifndef DWC_DEVICE_ONLY
36535 +
36536 +/** @file
36537 + * This file implements HCD Core. All code in this file is portable and doesn't
36538 + * use any OS specific functions.
36539 + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
36540 + * header file.
36541 + */
36542 +
36543 +#include <linux/usb.h>
36544 +#include <linux/usb/hcd.h>
36545 +
36546 +#include "dwc_otg_hcd.h"
36547 +#include "dwc_otg_regs.h"
36548 +#include "dwc_otg_fiq_fsm.h"
36549 +
36550 +extern bool microframe_schedule;
36551 +extern uint16_t fiq_fsm_mask, nak_holdoff;
36552 +
36553 +//#define DEBUG_HOST_CHANNELS
36554 +#ifdef DEBUG_HOST_CHANNELS
36555 +static int last_sel_trans_num_per_scheduled = 0;
36556 +static int last_sel_trans_num_nonper_scheduled = 0;
36557 +static int last_sel_trans_num_avail_hc_at_start = 0;
36558 +static int last_sel_trans_num_avail_hc_at_end = 0;
36559 +#endif /* DEBUG_HOST_CHANNELS */
36560 +
36561 +
36562 +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
36563 +{
36564 + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
36565 +}
36566 +
36567 +/**
36568 + * Connection timeout function. An OTG host is required to display a
36569 + * message if the device does not connect within 10 seconds.
36570 + */
36571 +void dwc_otg_hcd_connect_timeout(void *ptr)
36572 +{
36573 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
36574 + DWC_PRINTF("Connect Timeout\n");
36575 + __DWC_ERROR("Device Not Connected/Responding\n");
36576 +}
36577 +
36578 +#if defined(DEBUG)
36579 +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
36580 +{
36581 + if (qh->channel != NULL) {
36582 + dwc_hc_t *hc = qh->channel;
36583 + dwc_list_link_t *item;
36584 + dwc_otg_qh_t *qh_item;
36585 + int num_channels = hcd->core_if->core_params->host_channels;
36586 + int i;
36587 +
36588 + dwc_otg_hc_regs_t *hc_regs;
36589 + hcchar_data_t hcchar;
36590 + hcsplt_data_t hcsplt;
36591 + hctsiz_data_t hctsiz;
36592 + uint32_t hcdma;
36593 +
36594 + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
36595 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
36596 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
36597 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
36598 + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
36599 +
36600 + DWC_PRINTF(" Assigned to channel %p:\n", hc);
36601 + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
36602 + hcsplt.d32);
36603 + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
36604 + hcdma);
36605 + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
36606 + hc->dev_addr, hc->ep_num, hc->ep_is_in);
36607 + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
36608 + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
36609 + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
36610 + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
36611 + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
36612 + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
36613 + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
36614 + DWC_PRINTF(" qh: %p\n", hc->qh);
36615 + DWC_PRINTF(" NP inactive sched:\n");
36616 + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
36617 + qh_item =
36618 + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36619 + DWC_PRINTF(" %p\n", qh_item);
36620 + }
36621 + DWC_PRINTF(" NP active sched:\n");
36622 + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
36623 + qh_item =
36624 + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36625 + DWC_PRINTF(" %p\n", qh_item);
36626 + }
36627 + DWC_PRINTF(" Channels: \n");
36628 + for (i = 0; i < num_channels; i++) {
36629 + dwc_hc_t *hc = hcd->hc_ptr_array[i];
36630 + DWC_PRINTF(" %2d: %p\n", i, hc);
36631 + }
36632 + }
36633 +}
36634 +#else
36635 +#define dump_channel_info(hcd, qh)
36636 +#endif /* DEBUG */
36637 +
36638 +/**
36639 + * Work queue function for starting the HCD when A-Cable is connected.
36640 + * The hcd_start() must be called in a process context.
36641 + */
36642 +static void hcd_start_func(void *_vp)
36643 +{
36644 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
36645 +
36646 + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
36647 + if (hcd) {
36648 + hcd->fops->start(hcd);
36649 + }
36650 +}
36651 +
36652 +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
36653 +{
36654 +#ifdef DEBUG
36655 + int i;
36656 + int num_channels = hcd->core_if->core_params->host_channels;
36657 + for (i = 0; i < num_channels; i++) {
36658 + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
36659 + }
36660 +#endif
36661 +}
36662 +
36663 +static void del_timers(dwc_otg_hcd_t * hcd)
36664 +{
36665 + del_xfer_timers(hcd);
36666 + DWC_TIMER_CANCEL(hcd->conn_timer);
36667 +}
36668 +
36669 +/**
36670 + * Processes all the URBs in a single list of QHs. Completes them with
36671 + * -ESHUTDOWN and frees the QTD.
36672 + */
36673 +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
36674 +{
36675 + dwc_list_link_t *qh_item, *qh_tmp;
36676 + dwc_otg_qh_t *qh;
36677 + dwc_otg_qtd_t *qtd, *qtd_tmp;
36678 +
36679 + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
36680 + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
36681 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
36682 + &qh->qtd_list, qtd_list_entry) {
36683 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
36684 + if (qtd->urb != NULL) {
36685 + hcd->fops->complete(hcd, qtd->urb->priv,
36686 + qtd->urb, -DWC_E_SHUTDOWN);
36687 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
36688 + }
36689 +
36690 + }
36691 + if(qh->channel) {
36692 + int n = qh->channel->hc_num;
36693 + /* Using hcchar.chen == 1 is not a reliable test.
36694 + * It is possible that the channel has already halted
36695 + * but not yet been through the IRQ handler.
36696 + */
36697 + if (fiq_fsm_enable && (hcd->fiq_state->channel[qh->channel->hc_num].fsm != FIQ_PASSTHROUGH)) {
36698 + local_fiq_disable();
36699 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
36700 + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
36701 + qh->channel->halt_pending = 1;
36702 + if (hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_TURBO ||
36703 + hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_SLEEPING)
36704 + hcd->fiq_state->channel[n].fsm = FIQ_HS_ISOC_ABORTED;
36705 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
36706 + local_fiq_enable();
36707 + } else {
36708 + dwc_otg_hc_halt(hcd->core_if, qh->channel,
36709 + DWC_OTG_HC_XFER_URB_DEQUEUE);
36710 + }
36711 + qh->channel = NULL;
36712 + }
36713 + dwc_otg_hcd_qh_remove(hcd, qh);
36714 + }
36715 +}
36716 +
36717 +/**
36718 + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
36719 + * and periodic schedules. The QTD associated with each URB is removed from
36720 + * the schedule and freed. This function may be called when a disconnect is
36721 + * detected or when the HCD is being stopped.
36722 + */
36723 +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
36724 +{
36725 + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
36726 + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
36727 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
36728 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
36729 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
36730 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
36731 +}
36732 +
36733 +/**
36734 + * Start the connection timer. An OTG host is required to display a
36735 + * message if the device does not connect within 10 seconds. The
36736 + * timer is deleted if a port connect interrupt occurs before the
36737 + * timer expires.
36738 + */
36739 +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
36740 +{
36741 + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
36742 +}
36743 +
36744 +/**
36745 + * HCD Callback function for disconnect of the HCD.
36746 + *
36747 + * @param p void pointer to the <code>struct usb_hcd</code>
36748 + */
36749 +static int32_t dwc_otg_hcd_session_start_cb(void *p)
36750 +{
36751 + dwc_otg_hcd_t *dwc_otg_hcd;
36752 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
36753 + dwc_otg_hcd = p;
36754 + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
36755 + return 1;
36756 +}
36757 +
36758 +/**
36759 + * HCD Callback function for starting the HCD when A-Cable is
36760 + * connected.
36761 + *
36762 + * @param p void pointer to the <code>struct usb_hcd</code>
36763 + */
36764 +static int32_t dwc_otg_hcd_start_cb(void *p)
36765 +{
36766 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36767 + dwc_otg_core_if_t *core_if;
36768 + hprt0_data_t hprt0;
36769 +
36770 + core_if = dwc_otg_hcd->core_if;
36771 +
36772 + if (core_if->op_state == B_HOST) {
36773 + /*
36774 + * Reset the port. During a HNP mode switch the reset
36775 + * needs to occur within 1ms and have a duration of at
36776 + * least 50ms.
36777 + */
36778 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
36779 + hprt0.b.prtrst = 1;
36780 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
36781 + }
36782 + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
36783 + hcd_start_func, dwc_otg_hcd, 50,
36784 + "start hcd");
36785 +
36786 + return 1;
36787 +}
36788 +
36789 +/**
36790 + * HCD Callback function for disconnect of the HCD.
36791 + *
36792 + * @param p void pointer to the <code>struct usb_hcd</code>
36793 + */
36794 +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
36795 +{
36796 + gintsts_data_t intr;
36797 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36798 +
36799 + DWC_SPINLOCK(dwc_otg_hcd->lock);
36800 + /*
36801 + * Set status flags for the hub driver.
36802 + */
36803 + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
36804 + dwc_otg_hcd->flags.b.port_connect_status = 0;
36805 + if(fiq_enable) {
36806 + local_fiq_disable();
36807 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
36808 + }
36809 + /*
36810 + * Shutdown any transfers in process by clearing the Tx FIFO Empty
36811 + * interrupt mask and status bits and disabling subsequent host
36812 + * channel interrupts.
36813 + */
36814 + intr.d32 = 0;
36815 + intr.b.nptxfempty = 1;
36816 + intr.b.ptxfempty = 1;
36817 + intr.b.hcintr = 1;
36818 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
36819 + intr.d32, 0);
36820 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
36821 + intr.d32, 0);
36822 +
36823 + del_timers(dwc_otg_hcd);
36824 +
36825 + /*
36826 + * Turn off the vbus power only if the core has transitioned to device
36827 + * mode. If still in host mode, need to keep power on to detect a
36828 + * reconnection.
36829 + */
36830 + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
36831 + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
36832 + hprt0_data_t hprt0 = {.d32 = 0 };
36833 + DWC_PRINTF("Disconnect: PortPower off\n");
36834 + hprt0.b.prtpwr = 0;
36835 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
36836 + hprt0.d32);
36837 + }
36838 +
36839 + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
36840 + }
36841 +
36842 + /* Respond with an error status to all URBs in the schedule. */
36843 + kill_all_urbs(dwc_otg_hcd);
36844 +
36845 + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
36846 + /* Clean up any host channels that were in use. */
36847 + int num_channels;
36848 + int i;
36849 + dwc_hc_t *channel;
36850 + dwc_otg_hc_regs_t *hc_regs;
36851 + hcchar_data_t hcchar;
36852 +
36853 + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
36854 +
36855 + if (!dwc_otg_hcd->core_if->dma_enable) {
36856 + /* Flush out any channel requests in slave mode. */
36857 + for (i = 0; i < num_channels; i++) {
36858 + channel = dwc_otg_hcd->hc_ptr_array[i];
36859 + if (DWC_CIRCLEQ_EMPTY_ENTRY
36860 + (channel, hc_list_entry)) {
36861 + hc_regs =
36862 + dwc_otg_hcd->core_if->
36863 + host_if->hc_regs[i];
36864 + hcchar.d32 =
36865 + DWC_READ_REG32(&hc_regs->hcchar);
36866 + if (hcchar.b.chen) {
36867 + hcchar.b.chen = 0;
36868 + hcchar.b.chdis = 1;
36869 + hcchar.b.epdir = 0;
36870 + DWC_WRITE_REG32
36871 + (&hc_regs->hcchar,
36872 + hcchar.d32);
36873 + }
36874 + }
36875 + }
36876 + }
36877 +
36878 + if(fiq_fsm_enable) {
36879 + for(i=0; i < 128; i++) {
36880 + dwc_otg_hcd->hub_port[i] = 0;
36881 + }
36882 + }
36883 + }
36884 +
36885 + if(fiq_enable) {
36886 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
36887 + local_fiq_enable();
36888 + }
36889 +
36890 + if (dwc_otg_hcd->fops->disconnect) {
36891 + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
36892 + }
36893 +
36894 + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
36895 + return 1;
36896 +}
36897 +
36898 +/**
36899 + * HCD Callback function for stopping the HCD.
36900 + *
36901 + * @param p void pointer to the <code>struct usb_hcd</code>
36902 + */
36903 +static int32_t dwc_otg_hcd_stop_cb(void *p)
36904 +{
36905 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36906 +
36907 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
36908 + dwc_otg_hcd_stop(dwc_otg_hcd);
36909 + return 1;
36910 +}
36911 +
36912 +#ifdef CONFIG_USB_DWC_OTG_LPM
36913 +/**
36914 + * HCD Callback function for sleep of HCD.
36915 + *
36916 + * @param p void pointer to the <code>struct usb_hcd</code>
36917 + */
36918 +static int dwc_otg_hcd_sleep_cb(void *p)
36919 +{
36920 + dwc_otg_hcd_t *hcd = p;
36921 +
36922 + dwc_otg_hcd_free_hc_from_lpm(hcd);
36923 +
36924 + return 0;
36925 +}
36926 +#endif
36927 +
36928 +
36929 +/**
36930 + * HCD Callback function for Remote Wakeup.
36931 + *
36932 + * @param p void pointer to the <code>struct usb_hcd</code>
36933 + */
36934 +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
36935 +{
36936 + dwc_otg_hcd_t *hcd = p;
36937 +
36938 + if (hcd->core_if->lx_state == DWC_OTG_L2) {
36939 + hcd->flags.b.port_suspend_change = 1;
36940 + }
36941 +#ifdef CONFIG_USB_DWC_OTG_LPM
36942 + else {
36943 + hcd->flags.b.port_l1_change = 1;
36944 + }
36945 +#endif
36946 + return 0;
36947 +}
36948 +
36949 +/**
36950 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
36951 + * stopped.
36952 + */
36953 +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
36954 +{
36955 + hprt0_data_t hprt0 = {.d32 = 0 };
36956 +
36957 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
36958 +
36959 + /*
36960 + * The root hub should be disconnected before this function is called.
36961 + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
36962 + * and the QH lists (via ..._hcd_endpoint_disable).
36963 + */
36964 +
36965 + /* Turn off all host-specific interrupts. */
36966 + dwc_otg_disable_host_interrupts(hcd->core_if);
36967 +
36968 + /* Turn off the vbus power */
36969 + DWC_PRINTF("PortPower off\n");
36970 + hprt0.b.prtpwr = 0;
36971 + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
36972 + dwc_mdelay(1);
36973 +}
36974 +
36975 +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
36976 + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
36977 + int atomic_alloc)
36978 +{
36979 + int retval = 0;
36980 + uint8_t needs_scheduling = 0;
36981 + dwc_otg_transaction_type_e tr_type;
36982 + dwc_otg_qtd_t *qtd;
36983 + gintmsk_data_t intr_mask = {.d32 = 0 };
36984 + hprt0_data_t hprt0 = { .d32 = 0 };
36985 +
36986 +#ifdef DEBUG /* integrity checks (Broadcom) */
36987 + if (NULL == hcd->core_if) {
36988 + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
36989 + /* No longer connected. */
36990 + return -DWC_E_INVALID;
36991 + }
36992 +#endif
36993 + if (!hcd->flags.b.port_connect_status) {
36994 + /* No longer connected. */
36995 + DWC_ERROR("Not connected\n");
36996 + return -DWC_E_NO_DEVICE;
36997 + }
36998 +
36999 + /* Some core configurations cannot support LS traffic on a FS root port */
37000 + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
37001 + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
37002 + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
37003 + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
37004 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
37005 + return -DWC_E_NO_DEVICE;
37006 + }
37007 + }
37008 +
37009 + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
37010 + if (qtd == NULL) {
37011 + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
37012 + return -DWC_E_NO_MEMORY;
37013 + }
37014 +#ifdef DEBUG /* integrity checks (Broadcom) */
37015 + if (qtd->urb == NULL) {
37016 + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
37017 + return -DWC_E_NO_MEMORY;
37018 + }
37019 + if (qtd->urb->priv == NULL) {
37020 + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
37021 + return -DWC_E_NO_MEMORY;
37022 + }
37023 +#endif
37024 + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
37025 + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
37026 + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
37027 + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
37028 + needs_scheduling = 0;
37029 +
37030 + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
37031 + // creates a new queue in ep_handle if it doesn't exist already
37032 + if (retval < 0) {
37033 + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
37034 + "Error status %d\n", retval);
37035 + dwc_otg_hcd_qtd_free(qtd);
37036 + return retval;
37037 + }
37038 +
37039 + if(needs_scheduling) {
37040 + tr_type = dwc_otg_hcd_select_transactions(hcd);
37041 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
37042 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
37043 + }
37044 + }
37045 + return retval;
37046 +}
37047 +
37048 +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
37049 + dwc_otg_hcd_urb_t * dwc_otg_urb)
37050 +{
37051 + dwc_otg_qh_t *qh;
37052 + dwc_otg_qtd_t *urb_qtd;
37053 + BUG_ON(!hcd);
37054 + BUG_ON(!dwc_otg_urb);
37055 +
37056 +#ifdef DEBUG /* integrity checks (Broadcom) */
37057 +
37058 + if (hcd == NULL) {
37059 + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
37060 + return -DWC_E_INVALID;
37061 + }
37062 + if (dwc_otg_urb == NULL) {
37063 + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
37064 + return -DWC_E_INVALID;
37065 + }
37066 + if (dwc_otg_urb->qtd == NULL) {
37067 + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
37068 + return -DWC_E_INVALID;
37069 + }
37070 + urb_qtd = dwc_otg_urb->qtd;
37071 + BUG_ON(!urb_qtd);
37072 + if (urb_qtd->qh == NULL) {
37073 + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
37074 + return -DWC_E_INVALID;
37075 + }
37076 +#else
37077 + urb_qtd = dwc_otg_urb->qtd;
37078 + BUG_ON(!urb_qtd);
37079 +#endif
37080 + qh = urb_qtd->qh;
37081 + BUG_ON(!qh);
37082 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
37083 + if (urb_qtd->in_process) {
37084 + dump_channel_info(hcd, qh);
37085 + }
37086 + }
37087 +#ifdef DEBUG /* integrity checks (Broadcom) */
37088 + if (hcd->core_if == NULL) {
37089 + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
37090 + return -DWC_E_INVALID;
37091 + }
37092 +#endif
37093 + if (urb_qtd->in_process && qh->channel) {
37094 + /* The QTD is in process (it has been assigned to a channel). */
37095 + if (hcd->flags.b.port_connect_status) {
37096 + int n = qh->channel->hc_num;
37097 + /*
37098 + * If still connected (i.e. in host mode), halt the
37099 + * channel so it can be used for other transfers. If
37100 + * no longer connected, the host registers can't be
37101 + * written to halt the channel since the core is in
37102 + * device mode.
37103 + */
37104 + /* In FIQ FSM mode, we need to shut down carefully.
37105 + * The FIQ may attempt to restart a disabled channel */
37106 + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
37107 + local_fiq_disable();
37108 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
37109 + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
37110 + qh->channel->halt_pending = 1;
37111 + if (hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_TURBO ||
37112 + hcd->fiq_state->channel[n].fsm == FIQ_HS_ISOC_SLEEPING)
37113 + hcd->fiq_state->channel[n].fsm = FIQ_HS_ISOC_ABORTED;
37114 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
37115 + local_fiq_enable();
37116 + } else {
37117 + dwc_otg_hc_halt(hcd->core_if, qh->channel,
37118 + DWC_OTG_HC_XFER_URB_DEQUEUE);
37119 + }
37120 + }
37121 + }
37122 +
37123 + /*
37124 + * Free the QTD and clean up the associated QH. Leave the QH in the
37125 + * schedule if it has any remaining QTDs.
37126 + */
37127 +
37128 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
37129 + "delete %sQueue handler\n",
37130 + hcd->core_if->dma_desc_enable?"DMA ":"");
37131 + if (!hcd->core_if->dma_desc_enable) {
37132 + uint8_t b = urb_qtd->in_process;
37133 + if (nak_holdoff && qh->do_split && dwc_qh_is_non_per(qh))
37134 + qh->nak_frame = 0xFFFF;
37135 + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
37136 + if (b) {
37137 + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
37138 + qh->channel = NULL;
37139 + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
37140 + dwc_otg_hcd_qh_remove(hcd, qh);
37141 + }
37142 + } else {
37143 + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
37144 + }
37145 + return 0;
37146 +}
37147 +
37148 +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
37149 + int retry)
37150 +{
37151 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
37152 + int retval = 0;
37153 + dwc_irqflags_t flags;
37154 +
37155 + if (retry < 0) {
37156 + retval = -DWC_E_INVALID;
37157 + goto done;
37158 + }
37159 +
37160 + if (!qh) {
37161 + retval = -DWC_E_INVALID;
37162 + goto done;
37163 + }
37164 +
37165 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37166 +
37167 + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
37168 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37169 + retry--;
37170 + dwc_msleep(5);
37171 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37172 + }
37173 +
37174 + dwc_otg_hcd_qh_remove(hcd, qh);
37175 +
37176 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37177 + /*
37178 + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
37179 + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
37180 + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
37181 + * and dwc_otg_hcd_frame_list_alloc().
37182 + */
37183 + dwc_otg_hcd_qh_free(hcd, qh);
37184 +
37185 +done:
37186 + return retval;
37187 +}
37188 +
37189 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
37190 +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
37191 +{
37192 + int retval = 0;
37193 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
37194 + if (!qh)
37195 + return -DWC_E_INVALID;
37196 +
37197 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
37198 + return retval;
37199 +}
37200 +#endif
37201 +
37202 +/**
37203 + * HCD Callback structure for handling mode switching.
37204 + */
37205 +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
37206 + .start = dwc_otg_hcd_start_cb,
37207 + .stop = dwc_otg_hcd_stop_cb,
37208 + .disconnect = dwc_otg_hcd_disconnect_cb,
37209 + .session_start = dwc_otg_hcd_session_start_cb,
37210 + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
37211 +#ifdef CONFIG_USB_DWC_OTG_LPM
37212 + .sleep = dwc_otg_hcd_sleep_cb,
37213 +#endif
37214 + .p = 0,
37215 +};
37216 +
37217 +/**
37218 + * Reset tasklet function
37219 + */
37220 +static void reset_tasklet_func(void *data)
37221 +{
37222 + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
37223 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
37224 + hprt0_data_t hprt0;
37225 +
37226 + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
37227 +
37228 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
37229 + hprt0.b.prtrst = 1;
37230 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
37231 + dwc_mdelay(60);
37232 +
37233 + hprt0.b.prtrst = 0;
37234 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
37235 + dwc_otg_hcd->flags.b.port_reset_change = 1;
37236 +}
37237 +
37238 +static void completion_tasklet_func(void *ptr)
37239 +{
37240 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
37241 + struct urb *urb;
37242 + urb_tq_entry_t *item;
37243 + dwc_irqflags_t flags;
37244 +
37245 + /* This could just be spin_lock_irq */
37246 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37247 + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
37248 + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
37249 + urb = item->urb;
37250 + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
37251 + urb_tq_entries);
37252 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37253 + DWC_FREE(item);
37254 +
37255 + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
37256 +
37257 +
37258 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37259 + }
37260 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37261 + return;
37262 +}
37263 +
37264 +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
37265 +{
37266 + dwc_list_link_t *item;
37267 + dwc_otg_qh_t *qh;
37268 + dwc_irqflags_t flags;
37269 +
37270 + if (!qh_list->next) {
37271 + /* The list hasn't been initialized yet. */
37272 + return;
37273 + }
37274 + /*
37275 + * Hold spinlock here. Not needed in that case if bellow
37276 + * function is being called from ISR
37277 + */
37278 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37279 + /* Ensure there are no QTDs or URBs left. */
37280 + kill_urbs_in_qh_list(hcd, qh_list);
37281 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37282 +
37283 + DWC_LIST_FOREACH(item, qh_list) {
37284 + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
37285 + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
37286 + }
37287 +}
37288 +
37289 +/**
37290 + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
37291 + * Device during SRP time by host power up.
37292 + */
37293 +void dwc_otg_hcd_power_up(void *ptr)
37294 +{
37295 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
37296 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
37297 +
37298 + DWC_PRINTF("%s called\n", __FUNCTION__);
37299 +
37300 + if (!core_if->hibernation_suspend) {
37301 + DWC_PRINTF("Already exited from Hibernation\n");
37302 + return;
37303 + }
37304 +
37305 + /* Switch on the voltage to the core */
37306 + gpwrdn.b.pwrdnswtch = 1;
37307 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37308 + dwc_udelay(10);
37309 +
37310 + /* Reset the core */
37311 + gpwrdn.d32 = 0;
37312 + gpwrdn.b.pwrdnrstn = 1;
37313 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37314 + dwc_udelay(10);
37315 +
37316 + /* Disable power clamps */
37317 + gpwrdn.d32 = 0;
37318 + gpwrdn.b.pwrdnclmp = 1;
37319 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37320 +
37321 + /* Remove reset the core signal */
37322 + gpwrdn.d32 = 0;
37323 + gpwrdn.b.pwrdnrstn = 1;
37324 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
37325 + dwc_udelay(10);
37326 +
37327 + /* Disable PMU interrupt */
37328 + gpwrdn.d32 = 0;
37329 + gpwrdn.b.pmuintsel = 1;
37330 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37331 +
37332 + core_if->hibernation_suspend = 0;
37333 +
37334 + /* Disable PMU */
37335 + gpwrdn.d32 = 0;
37336 + gpwrdn.b.pmuactv = 1;
37337 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37338 + dwc_udelay(10);
37339 +
37340 + /* Enable VBUS */
37341 + gpwrdn.d32 = 0;
37342 + gpwrdn.b.dis_vbus = 1;
37343 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37344 +
37345 + core_if->op_state = A_HOST;
37346 + dwc_otg_core_init(core_if);
37347 + dwc_otg_enable_global_interrupts(core_if);
37348 + cil_hcd_start(core_if);
37349 +}
37350 +
37351 +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
37352 +{
37353 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
37354 + struct fiq_dma_blob *blob = hcd->fiq_dmab;
37355 + int i;
37356 +
37357 + st->fsm = FIQ_PASSTHROUGH;
37358 + st->hcchar_copy.d32 = 0;
37359 + st->hcsplt_copy.d32 = 0;
37360 + st->hcint_copy.d32 = 0;
37361 + st->hcintmsk_copy.d32 = 0;
37362 + st->hctsiz_copy.d32 = 0;
37363 + st->hcdma_copy.d32 = 0;
37364 + st->nr_errors = 0;
37365 + st->hub_addr = 0;
37366 + st->port_addr = 0;
37367 + st->expected_uframe = 0;
37368 + st->nrpackets = 0;
37369 + st->dma_info.index = 0;
37370 + for (i = 0; i < 6; i++)
37371 + st->dma_info.slot_len[i] = 255;
37372 + st->hs_isoc_info.index = 0;
37373 + st->hs_isoc_info.iso_desc = NULL;
37374 + st->hs_isoc_info.nrframes = 0;
37375 +
37376 + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
37377 +}
37378 +
37379 +/**
37380 + * Frees secondary storage associated with the dwc_otg_hcd structure contained
37381 + * in the struct usb_hcd field.
37382 + */
37383 +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
37384 +{
37385 + struct device *dev = dwc_otg_hcd_to_dev(dwc_otg_hcd);
37386 + int i;
37387 +
37388 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
37389 +
37390 + del_timers(dwc_otg_hcd);
37391 +
37392 + /* Free memory for QH/QTD lists */
37393 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
37394 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
37395 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
37396 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
37397 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
37398 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
37399 +
37400 + /* Free memory for the host channels. */
37401 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
37402 + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
37403 +
37404 +#ifdef DEBUG
37405 + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
37406 + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
37407 + }
37408 +#endif
37409 + if (hc != NULL) {
37410 + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
37411 + i, hc);
37412 + DWC_FREE(hc);
37413 + }
37414 + }
37415 +
37416 + if (dwc_otg_hcd->core_if->dma_enable) {
37417 + if (dwc_otg_hcd->status_buf_dma) {
37418 + DWC_DMA_FREE(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
37419 + dwc_otg_hcd->status_buf,
37420 + dwc_otg_hcd->status_buf_dma);
37421 + }
37422 + } else if (dwc_otg_hcd->status_buf != NULL) {
37423 + DWC_FREE(dwc_otg_hcd->status_buf);
37424 + }
37425 + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
37426 + /* Set core_if's lock pointer to NULL */
37427 + dwc_otg_hcd->core_if->lock = NULL;
37428 +
37429 + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
37430 + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
37431 + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
37432 + DWC_FREE(dwc_otg_hcd->fiq_state);
37433 +
37434 +#ifdef DWC_DEV_SRPCAP
37435 + if (dwc_otg_hcd->core_if->power_down == 2 &&
37436 + dwc_otg_hcd->core_if->pwron_timer) {
37437 + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
37438 + }
37439 +#endif
37440 + DWC_FREE(dwc_otg_hcd);
37441 +}
37442 +
37443 +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
37444 +{
37445 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
37446 + int retval = 0;
37447 + int num_channels;
37448 + int i;
37449 + dwc_hc_t *channel;
37450 +
37451 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
37452 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
37453 +#else
37454 + hcd->lock = DWC_SPINLOCK_ALLOC();
37455 +#endif
37456 + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
37457 + hcd, core_if);
37458 + if (!hcd->lock) {
37459 + DWC_ERROR("Could not allocate lock for pcd");
37460 + DWC_FREE(hcd);
37461 + retval = -DWC_E_NO_MEMORY;
37462 + goto out;
37463 + }
37464 + hcd->core_if = core_if;
37465 +
37466 + /* Register the HCD CIL Callbacks */
37467 + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
37468 + &hcd_cil_callbacks, hcd);
37469 +
37470 + /* Initialize the non-periodic schedule. */
37471 + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
37472 + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
37473 +
37474 + /* Initialize the periodic schedule. */
37475 + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
37476 + DWC_LIST_INIT(&hcd->periodic_sched_ready);
37477 + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
37478 + DWC_LIST_INIT(&hcd->periodic_sched_queued);
37479 + DWC_TAILQ_INIT(&hcd->completed_urb_list);
37480 + /*
37481 + * Create a host channel descriptor for each host channel implemented
37482 + * in the controller. Initialize the channel descriptor array.
37483 + */
37484 + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
37485 + num_channels = hcd->core_if->core_params->host_channels;
37486 + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
37487 + for (i = 0; i < num_channels; i++) {
37488 + channel = DWC_ALLOC(sizeof(dwc_hc_t));
37489 + if (channel == NULL) {
37490 + retval = -DWC_E_NO_MEMORY;
37491 + DWC_ERROR("%s: host channel allocation failed\n",
37492 + __func__);
37493 + dwc_otg_hcd_free(hcd);
37494 + goto out;
37495 + }
37496 + channel->hc_num = i;
37497 + hcd->hc_ptr_array[i] = channel;
37498 +#ifdef DEBUG
37499 + hcd->core_if->hc_xfer_timer[i] =
37500 + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
37501 + &hcd->core_if->hc_xfer_info[i]);
37502 +#endif
37503 + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
37504 + channel);
37505 + }
37506 +
37507 + if (fiq_enable) {
37508 + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
37509 + if (!hcd->fiq_state) {
37510 + retval = -DWC_E_NO_MEMORY;
37511 + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
37512 + dwc_otg_hcd_free(hcd);
37513 + goto out;
37514 + }
37515 + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
37516 +
37517 + for (i = 0; i < num_channels; i++) {
37518 + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
37519 + }
37520 + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
37521 +
37522 + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
37523 + if (!hcd->fiq_stack) {
37524 + retval = -DWC_E_NO_MEMORY;
37525 + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
37526 + dwc_otg_hcd_free(hcd);
37527 + goto out;
37528 + }
37529 + hcd->fiq_stack->magic1 = 0xDEADBEEF;
37530 + hcd->fiq_stack->magic2 = 0xD00DFEED;
37531 + hcd->fiq_state->gintmsk_saved.d32 = ~0;
37532 + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
37533 +
37534 + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
37535 + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
37536 + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
37537 + * moderately readable array casts.
37538 + */
37539 + hcd->fiq_dmab = DWC_DMA_ALLOC(dev, (sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
37540 + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
37541 + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
37542 + sizeof(struct fiq_dma_channel) * num_channels);
37543 +
37544 + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
37545 +
37546 + /* pointer for debug in fiq_print */
37547 + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
37548 + if (fiq_fsm_enable) {
37549 + int i;
37550 + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
37551 + dwc_otg_cleanup_fiq_channel(hcd, i);
37552 + }
37553 + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
37554 + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
37555 + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
37556 + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
37557 + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
37558 + }
37559 + }
37560 +
37561 + /* Initialize the Connection timeout timer. */
37562 + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
37563 + dwc_otg_hcd_connect_timeout, 0);
37564 +
37565 + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
37566 + if (microframe_schedule)
37567 + init_hcd_usecs(hcd);
37568 +
37569 + /* Initialize reset tasklet. */
37570 + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
37571 +
37572 + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
37573 + completion_tasklet_func, hcd);
37574 +#ifdef DWC_DEV_SRPCAP
37575 + if (hcd->core_if->power_down == 2) {
37576 + /* Initialize Power on timer for Host power up in case hibernation */
37577 + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
37578 + dwc_otg_hcd_power_up, core_if);
37579 + }
37580 +#endif
37581 +
37582 + /*
37583 + * Allocate space for storing data on status transactions. Normally no
37584 + * data is sent, but this space acts as a bit bucket. This must be
37585 + * done after usb_add_hcd since that function allocates the DMA buffer
37586 + * pool.
37587 + */
37588 + if (hcd->core_if->dma_enable) {
37589 + hcd->status_buf =
37590 + DWC_DMA_ALLOC(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
37591 + &hcd->status_buf_dma);
37592 + } else {
37593 + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
37594 + }
37595 + if (!hcd->status_buf) {
37596 + retval = -DWC_E_NO_MEMORY;
37597 + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
37598 + dwc_otg_hcd_free(hcd);
37599 + goto out;
37600 + }
37601 +
37602 + hcd->otg_port = 1;
37603 + hcd->frame_list = NULL;
37604 + hcd->frame_list_dma = 0;
37605 + hcd->periodic_qh_count = 0;
37606 +
37607 + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
37608 +#ifdef FIQ_DEBUG
37609 + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
37610 +#endif
37611 +
37612 +out:
37613 + return retval;
37614 +}
37615 +
37616 +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
37617 +{
37618 + /* Turn off all host-specific interrupts. */
37619 + dwc_otg_disable_host_interrupts(hcd->core_if);
37620 +
37621 + dwc_otg_hcd_free(hcd);
37622 +}
37623 +
37624 +/**
37625 + * Initializes dynamic portions of the DWC_otg HCD state.
37626 + */
37627 +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
37628 +{
37629 + int num_channels;
37630 + int i;
37631 + dwc_hc_t *channel;
37632 + dwc_hc_t *channel_tmp;
37633 +
37634 + hcd->flags.d32 = 0;
37635 +
37636 + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
37637 + if (!microframe_schedule) {
37638 + hcd->non_periodic_channels = 0;
37639 + hcd->periodic_channels = 0;
37640 + } else {
37641 + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
37642 + }
37643 + /*
37644 + * Put all channels in the free channel list and clean up channel
37645 + * states.
37646 + */
37647 + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
37648 + &hcd->free_hc_list, hc_list_entry) {
37649 + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
37650 + }
37651 +
37652 + num_channels = hcd->core_if->core_params->host_channels;
37653 + for (i = 0; i < num_channels; i++) {
37654 + channel = hcd->hc_ptr_array[i];
37655 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
37656 + hc_list_entry);
37657 + dwc_otg_hc_cleanup(hcd->core_if, channel);
37658 + }
37659 +
37660 + /* Initialize the DWC core for host mode operation. */
37661 + dwc_otg_core_host_init(hcd->core_if);
37662 +
37663 + /* Set core_if's lock pointer to the hcd->lock */
37664 + hcd->core_if->lock = hcd->lock;
37665 +}
37666 +
37667 +/**
37668 + * Assigns transactions from a QTD to a free host channel and initializes the
37669 + * host channel to perform the transactions. The host channel is removed from
37670 + * the free list.
37671 + *
37672 + * @param hcd The HCD state structure.
37673 + * @param qh Transactions from the first QTD for this QH are selected and
37674 + * assigned to a free host channel.
37675 + */
37676 +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
37677 +{
37678 + dwc_hc_t *hc;
37679 + dwc_otg_qtd_t *qtd;
37680 + dwc_otg_hcd_urb_t *urb;
37681 + void* ptr = NULL;
37682 + uint32_t intr_enable;
37683 + unsigned long flags;
37684 + gintmsk_data_t gintmsk = { .d32 = 0, };
37685 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
37686 +
37687 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37688 +
37689 + urb = qtd->urb;
37690 +
37691 + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
37692 +
37693 + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
37694 + urb->actual_length = urb->length;
37695 +
37696 +
37697 + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
37698 +
37699 + /* Remove the host channel from the free list. */
37700 + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
37701 +
37702 + qh->channel = hc;
37703 +
37704 + qtd->in_process = 1;
37705 +
37706 + /*
37707 + * Use usb_pipedevice to determine device address. This address is
37708 + * 0 before the SET_ADDRESS command and the correct address afterward.
37709 + */
37710 + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
37711 + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
37712 + hc->speed = qh->dev_speed;
37713 + hc->max_packet = dwc_max_packet(qh->maxp);
37714 +
37715 + hc->xfer_started = 0;
37716 + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
37717 + hc->error_state = (qtd->error_count > 0);
37718 + hc->halt_on_queue = 0;
37719 + hc->halt_pending = 0;
37720 + hc->requests = 0;
37721 +
37722 + /*
37723 + * The following values may be modified in the transfer type section
37724 + * below. The xfer_len value may be reduced when the transfer is
37725 + * started to accommodate the max widths of the XferSize and PktCnt
37726 + * fields in the HCTSIZn register.
37727 + */
37728 +
37729 + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
37730 + if (hc->ep_is_in) {
37731 + hc->do_ping = 0;
37732 + } else {
37733 + hc->do_ping = qh->ping_state;
37734 + }
37735 +
37736 + hc->data_pid_start = qh->data_toggle;
37737 + hc->multi_count = 1;
37738 +
37739 + if (hcd->core_if->dma_enable) {
37740 + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
37741 +
37742 + /* For non-dword aligned case */
37743 + if (((unsigned long)hc->xfer_buff & 0x3)
37744 + && !hcd->core_if->dma_desc_enable) {
37745 + ptr = (uint8_t *) urb->buf + urb->actual_length;
37746 + }
37747 + } else {
37748 + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
37749 + }
37750 + hc->xfer_len = urb->length - urb->actual_length;
37751 + hc->xfer_count = 0;
37752 +
37753 + /*
37754 + * Set the split attributes
37755 + */
37756 + hc->do_split = 0;
37757 + if (qh->do_split) {
37758 + uint32_t hub_addr, port_addr;
37759 + hc->do_split = 1;
37760 + hc->start_pkt_count = 1;
37761 + hc->xact_pos = qtd->isoc_split_pos;
37762 + /* We don't need to do complete splits anymore */
37763 +// if(fiq_fsm_enable)
37764 + if (0)
37765 + hc->complete_split = qtd->complete_split = 0;
37766 + else
37767 + hc->complete_split = qtd->complete_split;
37768 +
37769 + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
37770 + hc->hub_addr = (uint8_t) hub_addr;
37771 + hc->port_addr = (uint8_t) port_addr;
37772 + }
37773 +
37774 + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
37775 + case UE_CONTROL:
37776 + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
37777 + switch (qtd->control_phase) {
37778 + case DWC_OTG_CONTROL_SETUP:
37779 + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
37780 + hc->do_ping = 0;
37781 + hc->ep_is_in = 0;
37782 + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
37783 + if (hcd->core_if->dma_enable) {
37784 + hc->xfer_buff = (uint8_t *) urb->setup_dma;
37785 + } else {
37786 + hc->xfer_buff = (uint8_t *) urb->setup_packet;
37787 + }
37788 + hc->xfer_len = 8;
37789 + ptr = NULL;
37790 + break;
37791 + case DWC_OTG_CONTROL_DATA:
37792 + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
37793 + hc->data_pid_start = qtd->data_toggle;
37794 + break;
37795 + case DWC_OTG_CONTROL_STATUS:
37796 + /*
37797 + * Direction is opposite of data direction or IN if no
37798 + * data.
37799 + */
37800 + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
37801 + if (urb->length == 0) {
37802 + hc->ep_is_in = 1;
37803 + } else {
37804 + hc->ep_is_in =
37805 + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
37806 + }
37807 + if (hc->ep_is_in) {
37808 + hc->do_ping = 0;
37809 + }
37810 +
37811 + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
37812 +
37813 + hc->xfer_len = 0;
37814 + if (hcd->core_if->dma_enable) {
37815 + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
37816 + } else {
37817 + hc->xfer_buff = (uint8_t *) hcd->status_buf;
37818 + }
37819 + ptr = NULL;
37820 + break;
37821 + }
37822 + break;
37823 + case UE_BULK:
37824 + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
37825 + break;
37826 + case UE_INTERRUPT:
37827 + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
37828 + break;
37829 + case UE_ISOCHRONOUS:
37830 + {
37831 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
37832 +
37833 + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
37834 +
37835 + if (hcd->core_if->dma_desc_enable)
37836 + break;
37837 +
37838 + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
37839 +
37840 + frame_desc->status = 0;
37841 +
37842 + if (hcd->core_if->dma_enable) {
37843 + hc->xfer_buff = (uint8_t *) urb->dma;
37844 + } else {
37845 + hc->xfer_buff = (uint8_t *) urb->buf;
37846 + }
37847 + hc->xfer_buff +=
37848 + frame_desc->offset + qtd->isoc_split_offset;
37849 + hc->xfer_len =
37850 + frame_desc->length - qtd->isoc_split_offset;
37851 +
37852 + /* For non-dword aligned buffers */
37853 + if (((unsigned long)hc->xfer_buff & 0x3)
37854 + && hcd->core_if->dma_enable) {
37855 + ptr =
37856 + (uint8_t *) urb->buf + frame_desc->offset +
37857 + qtd->isoc_split_offset;
37858 + } else
37859 + ptr = NULL;
37860 +
37861 + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
37862 + if (hc->xfer_len <= 188) {
37863 + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
37864 + } else {
37865 + hc->xact_pos =
37866 + DWC_HCSPLIT_XACTPOS_BEGIN;
37867 + }
37868 + }
37869 + }
37870 + break;
37871 + }
37872 + /* non DWORD-aligned buffer case */
37873 + if (ptr) {
37874 + uint32_t buf_size;
37875 + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
37876 + buf_size = hcd->core_if->core_params->max_transfer_size;
37877 + } else {
37878 + buf_size = 4096;
37879 + }
37880 + if (!qh->dw_align_buf) {
37881 + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(dev, buf_size,
37882 + &qh->dw_align_buf_dma);
37883 + if (!qh->dw_align_buf) {
37884 + DWC_ERROR
37885 + ("%s: Failed to allocate memory to handle "
37886 + "non-dword aligned buffer case\n",
37887 + __func__);
37888 + return;
37889 + }
37890 + }
37891 + if (!hc->ep_is_in) {
37892 + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
37893 + }
37894 + hc->align_buff = qh->dw_align_buf_dma;
37895 + } else {
37896 + hc->align_buff = 0;
37897 + }
37898 +
37899 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
37900 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
37901 + /*
37902 + * This value may be modified when the transfer is started to
37903 + * reflect the actual transfer length.
37904 + */
37905 + hc->multi_count = dwc_hb_mult(qh->maxp);
37906 + }
37907 +
37908 + if (hcd->core_if->dma_desc_enable)
37909 + hc->desc_list_addr = qh->desc_list_dma;
37910 +
37911 + dwc_otg_hc_init(hcd->core_if, hc);
37912 +
37913 + local_irq_save(flags);
37914 +
37915 + if (fiq_enable) {
37916 + local_fiq_disable();
37917 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
37918 + }
37919 +
37920 + /* Enable the top level host channel interrupt. */
37921 + intr_enable = (1 << hc->hc_num);
37922 + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
37923 +
37924 + /* Make sure host channel interrupts are enabled. */
37925 + gintmsk.b.hcintr = 1;
37926 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
37927 +
37928 + if (fiq_enable) {
37929 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
37930 + local_fiq_enable();
37931 + }
37932 +
37933 + local_irq_restore(flags);
37934 + hc->qh = qh;
37935 +}
37936 +
37937 +
37938 +/**
37939 + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
37940 + * @hcd: Pointer to the dwc_otg_hcd struct
37941 + * @qh: pointer to the endpoint's queue head
37942 + *
37943 + * Transaction start/end control flow is grafted onto the existing dwc_otg
37944 + * mechanisms, to avoid spaghettifying the functions more than they already are.
37945 + * This function's eligibility check is altered by debug parameter.
37946 + *
37947 + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
37948 + */
37949 +
37950 +int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37951 +{
37952 + if (qh->do_split) {
37953 + switch (qh->ep_type) {
37954 + case UE_CONTROL:
37955 + case UE_BULK:
37956 + if (fiq_fsm_mask & (1 << 0))
37957 + return 1;
37958 + break;
37959 + case UE_INTERRUPT:
37960 + case UE_ISOCHRONOUS:
37961 + if (fiq_fsm_mask & (1 << 1))
37962 + return 1;
37963 + break;
37964 + default:
37965 + break;
37966 + }
37967 + } else if (qh->ep_type == UE_ISOCHRONOUS) {
37968 + if (fiq_fsm_mask & (1 << 2)) {
37969 + /* ISOCH support. We test for compatibility:
37970 + * - DWORD aligned buffers
37971 + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
37972 + * If yes, then the fsm enqueue function will handle the state machine setup.
37973 + */
37974 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37975 + dwc_otg_hcd_urb_t *urb = qtd->urb;
37976 + dwc_dma_t ptr;
37977 + int i;
37978 +
37979 + if (urb->packet_count < 2)
37980 + return 0;
37981 + for (i = 0; i < urb->packet_count; i++) {
37982 + ptr = urb->dma + urb->iso_descs[i].offset;
37983 + if (ptr & 0x3)
37984 + return 0;
37985 + }
37986 + return 1;
37987 + }
37988 + }
37989 + return 0;
37990 +}
37991 +
37992 +/**
37993 + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
37994 + * @hcd: Pointer to the dwc_otg_hcd struct
37995 + * @qh: Pointer to the endpoint's queue head
37996 + *
37997 + * Periodic split transactions are transmitted modulo 188 bytes.
37998 + * This necessitates slicing data up into buckets for isochronous out
37999 + * and fixing up the DMA address for all IN transfers.
38000 + *
38001 + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
38002 + * HC buffer has been used.
38003 + */
38004 +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
38005 + {
38006 + int frame_length, i = 0;
38007 + uint8_t *ptr = NULL;
38008 + dwc_hc_t *hc = qh->channel;
38009 + struct fiq_dma_blob *blob;
38010 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
38011 +
38012 + for (i = 0; i < 6; i++) {
38013 + st->dma_info.slot_len[i] = 255;
38014 + }
38015 + st->dma_info.index = 0;
38016 + i = 0;
38017 + if (hc->ep_is_in) {
38018 + /*
38019 + * Set dma_regs to bounce buffer. FIQ will update the
38020 + * state depending on transaction progress.
38021 + */
38022 + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
38023 + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
38024 + /* Calculate the max number of CSPLITS such that the FIQ can time out
38025 + * a transaction if it fails.
38026 + */
38027 + frame_length = st->hcchar_copy.b.mps;
38028 + do {
38029 + i++;
38030 + frame_length -= 188;
38031 + } while (frame_length >= 0);
38032 + st->nrpackets = i;
38033 + return 1;
38034 + } else {
38035 + if (qh->ep_type == UE_ISOCHRONOUS) {
38036 +
38037 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
38038 +
38039 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
38040 + frame_length = frame_desc->length;
38041 +
38042 + /* Virtual address for bounce buffers */
38043 + blob = hcd->fiq_dmab;
38044 +
38045 + ptr = qtd->urb->buf + frame_desc->offset;
38046 + if (frame_length == 0) {
38047 + /*
38048 + * for isochronous transactions, we must still transmit a packet
38049 + * even if the length is zero.
38050 + */
38051 + st->dma_info.slot_len[0] = 0;
38052 + st->nrpackets = 1;
38053 + } else {
38054 + do {
38055 + if (frame_length <= 188) {
38056 + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
38057 + st->dma_info.slot_len[i] = frame_length;
38058 + ptr += frame_length;
38059 + } else {
38060 + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
38061 + st->dma_info.slot_len[i] = 188;
38062 + ptr += 188;
38063 + }
38064 + i++;
38065 + frame_length -= 188;
38066 + } while (frame_length > 0);
38067 + st->nrpackets = i;
38068 + }
38069 + ptr = qtd->urb->buf + frame_desc->offset;
38070 + /* Point the HC at the DMA address of the bounce buffers */
38071 + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
38072 + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
38073 +
38074 + /* fixup xfersize to the actual packet size */
38075 + st->hctsiz_copy.b.pid = 0;
38076 + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
38077 + return 1;
38078 + } else {
38079 + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
38080 + return 0;
38081 + }
38082 + }
38083 +}
38084 +
38085 +/**
38086 + * fiq_fsm_np_tt_contended() - Avoid performing contended non-periodic transfers
38087 + * @hcd: Pointer to the dwc_otg_hcd struct
38088 + * @qh: Pointer to the endpoint's queue head
38089 + *
38090 + * Certain hub chips don't differentiate between IN and OUT non-periodic pipes
38091 + * with the same endpoint number. If transfers get completed out of order
38092 + * (disregarding the direction token) then the hub can lock up
38093 + * or return erroneous responses.
38094 + *
38095 + * Returns 1 if initiating the transfer would cause contention, 0 otherwise.
38096 + */
38097 +int fiq_fsm_np_tt_contended(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
38098 +{
38099 + int i;
38100 + struct fiq_channel_state *st;
38101 + int dev_addr = qh->channel->dev_addr;
38102 + int ep_num = qh->channel->ep_num;
38103 + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
38104 + if (i == qh->channel->hc_num)
38105 + continue;
38106 + st = &hcd->fiq_state->channel[i];
38107 + switch (st->fsm) {
38108 + case FIQ_NP_SSPLIT_STARTED:
38109 + case FIQ_NP_SSPLIT_RETRY:
38110 + case FIQ_NP_SSPLIT_PENDING:
38111 + case FIQ_NP_OUT_CSPLIT_RETRY:
38112 + case FIQ_NP_IN_CSPLIT_RETRY:
38113 + if (st->hcchar_copy.b.devaddr == dev_addr &&
38114 + st->hcchar_copy.b.epnum == ep_num)
38115 + return 1;
38116 + break;
38117 + default:
38118 + break;
38119 + }
38120 + }
38121 + return 0;
38122 +}
38123 +
38124 +/*
38125 + * Pushing a periodic request into the queue near the EOF1 point
38126 + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
38127 + * Usually, the request goes out on the bus causing a transfer but
38128 + * the core does not transfer the data to memory.
38129 + * This guard interval (in number of 60MHz clocks) is required which
38130 + * must cater for CPU latency between reading the value and enabling
38131 + * the channel.
38132 + */
38133 +#define PERIODIC_FRREM_BACKOFF 1000
38134 +
38135 +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
38136 +{
38137 + dwc_hc_t *hc = qh->channel;
38138 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
38139 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
38140 + int frame;
38141 + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
38142 + int xfer_len, nrpackets;
38143 + hcdma_data_t hcdma;
38144 + hfnum_data_t hfnum;
38145 +
38146 + if (st->fsm != FIQ_PASSTHROUGH)
38147 + return 0;
38148 +
38149 + st->nr_errors = 0;
38150 +
38151 + st->hcchar_copy.d32 = 0;
38152 + st->hcchar_copy.b.mps = hc->max_packet;
38153 + st->hcchar_copy.b.epdir = hc->ep_is_in;
38154 + st->hcchar_copy.b.devaddr = hc->dev_addr;
38155 + st->hcchar_copy.b.epnum = hc->ep_num;
38156 + st->hcchar_copy.b.eptype = hc->ep_type;
38157 +
38158 + st->hcintmsk_copy.b.chhltd = 1;
38159 +
38160 + frame = dwc_otg_hcd_get_frame_number(hcd);
38161 + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
38162 +
38163 + st->hcchar_copy.b.lspddev = 0;
38164 + /* Enable the channel later as a final register write. */
38165 +
38166 + st->hcsplt_copy.d32 = 0;
38167 +
38168 + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
38169 + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
38170 + /* grab the next DMA address offset from the array */
38171 + st->hcdma_copy.d32 = qtd->urb->dma;
38172 + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
38173 +
38174 + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
38175 + * the core needs to be told to send the correct number. Caution: for IN transfers,
38176 + * this is always set to the maximum size of the endpoint. */
38177 + xfer_len = st->hs_isoc_info.iso_desc[0].length;
38178 + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
38179 + if (nrpackets == 0)
38180 + nrpackets = 1;
38181 + st->hcchar_copy.b.multicnt = nrpackets;
38182 + st->hctsiz_copy.b.pktcnt = nrpackets;
38183 +
38184 + /* Initial PID also needs to be set */
38185 + if (st->hcchar_copy.b.epdir == 0) {
38186 + st->hctsiz_copy.b.xfersize = xfer_len;
38187 + switch (st->hcchar_copy.b.multicnt) {
38188 + case 1:
38189 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
38190 + break;
38191 + case 2:
38192 + case 3:
38193 + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
38194 + break;
38195 + }
38196 +
38197 + } else {
38198 + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
38199 + switch (st->hcchar_copy.b.multicnt) {
38200 + case 1:
38201 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
38202 + break;
38203 + case 2:
38204 + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
38205 + break;
38206 + case 3:
38207 + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
38208 + break;
38209 + }
38210 + }
38211 +
38212 + st->hs_isoc_info.stride = qh->interval;
38213 + st->uframe_sleeps = 0;
38214 +
38215 + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
38216 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
38217 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
38218 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
38219 + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
38220 + local_fiq_disable();
38221 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38222 + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
38223 + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
38224 + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
38225 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38226 + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
38227 + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
38228 + /* Prevent queueing near EOF1. Bad things happen if a periodic
38229 + * split transaction is queued very close to EOF. SOF interrupt handler
38230 + * will wake this channel at the next interrupt.
38231 + */
38232 + st->fsm = FIQ_HS_ISOC_SLEEPING;
38233 + st->uframe_sleeps = 1;
38234 + } else {
38235 + st->fsm = FIQ_HS_ISOC_TURBO;
38236 + st->hcchar_copy.b.chen = 1;
38237 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38238 + }
38239 + mb();
38240 + st->hcchar_copy.b.chen = 0;
38241 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38242 + local_fiq_enable();
38243 + return 0;
38244 +}
38245 +
38246 +
38247 +/**
38248 + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
38249 + * @hcd: Pointer to the dwc_otg_hcd struct
38250 + * @qh: Pointer to the endpoint's queue head
38251 + *
38252 + * This overrides the dwc_otg driver's normal method of queueing a transaction.
38253 + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
38254 + * for the nominated host channel.
38255 + *
38256 + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
38257 + * start is possible. If not, then the FIQ is left to start the transfer.
38258 + */
38259 +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
38260 +{
38261 + int start_immediate = 1, i;
38262 + hfnum_data_t hfnum;
38263 + dwc_hc_t *hc = qh->channel;
38264 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
38265 + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
38266 + int hub_addr, port_addr, frame, uframe;
38267 + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
38268 +
38269 + /*
38270 + * Non-periodic channel assignments stay in the non_periodic_active queue.
38271 + * Therefore we get repeatedly called until the FIQ's done processing this channel.
38272 + */
38273 + if (qh->channel->xfer_started == 1)
38274 + return 0;
38275 +
38276 + if (st->fsm != FIQ_PASSTHROUGH) {
38277 + pr_warn_ratelimited("%s:%d: Queue called for an active channel\n", __func__, __LINE__);
38278 + return 0;
38279 + }
38280 +
38281 + qh->channel->xfer_started = 1;
38282 +
38283 + st->nr_errors = 0;
38284 +
38285 + st->hcchar_copy.d32 = 0;
38286 + st->hcchar_copy.b.mps = hc->max_packet;
38287 + st->hcchar_copy.b.epdir = hc->ep_is_in;
38288 + st->hcchar_copy.b.devaddr = hc->dev_addr;
38289 + st->hcchar_copy.b.epnum = hc->ep_num;
38290 + st->hcchar_copy.b.eptype = hc->ep_type;
38291 + if (hc->ep_type & 0x1) {
38292 + if (hc->ep_is_in)
38293 + st->hcchar_copy.b.multicnt = 3;
38294 + else
38295 + /* Docs say set this to 1, but driver sets to 0! */
38296 + st->hcchar_copy.b.multicnt = 0;
38297 + } else {
38298 + st->hcchar_copy.b.multicnt = 1;
38299 + st->hcchar_copy.b.oddfrm = 0;
38300 + }
38301 + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
38302 + /* Enable the channel later as a final register write. */
38303 +
38304 + st->hcsplt_copy.d32 = 0;
38305 + if(qh->do_split) {
38306 + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
38307 + st->hcsplt_copy.b.compsplt = 0;
38308 + st->hcsplt_copy.b.spltena = 1;
38309 + // XACTPOS is for isoc-out only but needs initialising anyway.
38310 + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
38311 + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
38312 + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
38313 + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
38314 + * will update as necessary.
38315 + */
38316 + if (hc->xfer_len > 188) {
38317 + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
38318 + }
38319 + }
38320 + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
38321 + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
38322 + st->hub_addr = hub_addr;
38323 + st->port_addr = port_addr;
38324 + }
38325 +
38326 + st->hctsiz_copy.d32 = 0;
38327 + st->hctsiz_copy.b.dopng = 0;
38328 + st->hctsiz_copy.b.pid = hc->data_pid_start;
38329 +
38330 + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
38331 + hc->xfer_len = hc->max_packet;
38332 + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
38333 + hc->xfer_len = 188;
38334 + }
38335 + st->hctsiz_copy.b.xfersize = hc->xfer_len;
38336 +
38337 + st->hctsiz_copy.b.pktcnt = 1;
38338 +
38339 + if (hc->ep_type & 0x1) {
38340 + /*
38341 + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
38342 + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
38343 + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
38344 + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
38345 + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
38346 + * must not touch internal driver state.
38347 + */
38348 + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
38349 + if (hc->align_buff) {
38350 + st->hcdma_copy.d32 = hc->align_buff;
38351 + } else {
38352 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38353 + }
38354 + }
38355 + } else {
38356 + if (hc->align_buff) {
38357 + st->hcdma_copy.d32 = hc->align_buff;
38358 + } else {
38359 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38360 + }
38361 + }
38362 + /* The FIQ depends upon no other interrupts being enabled except channel halt.
38363 + * Fixup channel interrupt mask. */
38364 + st->hcintmsk_copy.d32 = 0;
38365 + st->hcintmsk_copy.b.chhltd = 1;
38366 + st->hcintmsk_copy.b.ahberr = 1;
38367 +
38368 + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
38369 + * as Control puts the transfer into the non-periodic request queue and the
38370 + * non-periodic handler in the hub. Makes things lots easier.
38371 + */
38372 + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
38373 + st->hcchar_copy.b.multicnt = 0;
38374 + st->hcchar_copy.b.oddfrm = 0;
38375 + st->hcchar_copy.b.eptype = UE_CONTROL;
38376 + if (hc->align_buff) {
38377 + st->hcdma_copy.d32 = hc->align_buff;
38378 + } else {
38379 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38380 + }
38381 + }
38382 + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
38383 + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
38384 + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
38385 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38386 + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
38387 +
38388 + local_fiq_disable();
38389 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38390 +
38391 + if (hc->ep_type & 0x1) {
38392 + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
38393 + frame = (hfnum.b.frnum & ~0x7) >> 3;
38394 + uframe = hfnum.b.frnum & 0x7;
38395 + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
38396 + /* Prevent queueing near EOF1. Bad things happen if a periodic
38397 + * split transaction is queued very close to EOF.
38398 + */
38399 + start_immediate = 0;
38400 + } else if (uframe == 5) {
38401 + start_immediate = 0;
38402 + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
38403 + start_immediate = 0;
38404 + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
38405 + start_immediate = 0;
38406 + } else {
38407 + /* Search through all host channels to determine if a transaction
38408 + * is currently in progress */
38409 + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
38410 + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
38411 + continue;
38412 + switch (hcd->fiq_state->channel[i].fsm) {
38413 + /* TT is reserved for channels that are in the middle of a periodic
38414 + * split transaction.
38415 + */
38416 + case FIQ_PER_SSPLIT_STARTED:
38417 + case FIQ_PER_CSPLIT_WAIT:
38418 + case FIQ_PER_CSPLIT_NYET1:
38419 + case FIQ_PER_CSPLIT_POLL:
38420 + case FIQ_PER_ISO_OUT_ACTIVE:
38421 + case FIQ_PER_ISO_OUT_LAST:
38422 + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
38423 + hcd->fiq_state->channel[i].port_addr == port_addr) {
38424 + start_immediate = 0;
38425 + }
38426 + break;
38427 + default:
38428 + break;
38429 + }
38430 + if (!start_immediate)
38431 + break;
38432 + }
38433 + }
38434 + }
38435 + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
38436 + start_immediate = 1;
38437 +
38438 + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
38439 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
38440 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
38441 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
38442 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
38443 + switch (hc->ep_type) {
38444 + case UE_CONTROL:
38445 + case UE_BULK:
38446 + if (fiq_fsm_np_tt_contended(hcd, qh)) {
38447 + st->fsm = FIQ_NP_SSPLIT_PENDING;
38448 + start_immediate = 0;
38449 + } else {
38450 + st->fsm = FIQ_NP_SSPLIT_STARTED;
38451 + }
38452 + break;
38453 + case UE_ISOCHRONOUS:
38454 + if (hc->ep_is_in) {
38455 + if (start_immediate) {
38456 + st->fsm = FIQ_PER_SSPLIT_STARTED;
38457 + } else {
38458 + st->fsm = FIQ_PER_SSPLIT_QUEUED;
38459 + }
38460 + } else {
38461 + if (start_immediate) {
38462 + /* Single-isoc OUT packets don't require FIQ involvement */
38463 + if (st->nrpackets == 1) {
38464 + st->fsm = FIQ_PER_ISO_OUT_LAST;
38465 + } else {
38466 + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
38467 + }
38468 + } else {
38469 + st->fsm = FIQ_PER_ISO_OUT_PENDING;
38470 + }
38471 + }
38472 + break;
38473 + case UE_INTERRUPT:
38474 + if (fiq_fsm_mask & 0x8) {
38475 + if (fiq_fsm_np_tt_contended(hcd, qh)) {
38476 + st->fsm = FIQ_NP_SSPLIT_PENDING;
38477 + start_immediate = 0;
38478 + } else {
38479 + st->fsm = FIQ_NP_SSPLIT_STARTED;
38480 + }
38481 + } else if (start_immediate) {
38482 + st->fsm = FIQ_PER_SSPLIT_STARTED;
38483 + } else {
38484 + st->fsm = FIQ_PER_SSPLIT_QUEUED;
38485 + }
38486 + default:
38487 + break;
38488 + }
38489 + if (start_immediate) {
38490 + /* Set the oddfrm bit as close as possible to actual queueing */
38491 + frame = dwc_otg_hcd_get_frame_number(hcd);
38492 + st->expected_uframe = (frame + 1) & 0x3FFF;
38493 + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
38494 + st->hcchar_copy.b.chen = 1;
38495 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38496 + }
38497 + mb();
38498 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38499 + local_fiq_enable();
38500 + return 0;
38501 +}
38502 +
38503 +
38504 +/**
38505 + * This function selects transactions from the HCD transfer schedule and
38506 + * assigns them to available host channels. It is called from HCD interrupt
38507 + * handler functions.
38508 + *
38509 + * @param hcd The HCD state structure.
38510 + *
38511 + * @return The types of new transactions that were assigned to host channels.
38512 + */
38513 +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
38514 +{
38515 + dwc_list_link_t *qh_ptr;
38516 + dwc_otg_qh_t *qh;
38517 + int num_channels;
38518 + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
38519 +
38520 +#ifdef DEBUG_HOST_CHANNELS
38521 + last_sel_trans_num_per_scheduled = 0;
38522 + last_sel_trans_num_nonper_scheduled = 0;
38523 + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
38524 +#endif /* DEBUG_HOST_CHANNELS */
38525 +
38526 + /* Process entries in the periodic ready list. */
38527 + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
38528 +
38529 + while (qh_ptr != &hcd->periodic_sched_ready &&
38530 + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
38531 +
38532 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38533 +
38534 + if (microframe_schedule) {
38535 + // Make sure we leave one channel for non periodic transactions.
38536 + if (hcd->available_host_channels <= 1) {
38537 + break;
38538 + }
38539 + hcd->available_host_channels--;
38540 +#ifdef DEBUG_HOST_CHANNELS
38541 + last_sel_trans_num_per_scheduled++;
38542 +#endif /* DEBUG_HOST_CHANNELS */
38543 + }
38544 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38545 + assign_and_init_hc(hcd, qh);
38546 +
38547 + /*
38548 + * Move the QH from the periodic ready schedule to the
38549 + * periodic assigned schedule.
38550 + */
38551 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38552 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
38553 + &qh->qh_list_entry);
38554 + }
38555 +
38556 + /*
38557 + * Process entries in the inactive portion of the non-periodic
38558 + * schedule. Some free host channels may not be used if they are
38559 + * reserved for periodic transfers.
38560 + */
38561 + qh_ptr = hcd->non_periodic_sched_inactive.next;
38562 + num_channels = hcd->core_if->core_params->host_channels;
38563 + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
38564 + (microframe_schedule || hcd->non_periodic_channels <
38565 + num_channels - hcd->periodic_channels) &&
38566 + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
38567 +
38568 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38569 + /*
38570 + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
38571 + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
38572 + * cheeky devices that just hold off using NAKs
38573 + */
38574 + if (fiq_enable && nak_holdoff && qh->do_split) {
38575 + if (qh->nak_frame != 0xffff) {
38576 + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
38577 + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
38578 + if (dwc_frame_num_le(frame, next_frame)) {
38579 + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
38580 + hcd->fiq_state->next_sched_frame = next_frame;
38581 + }
38582 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38583 + continue;
38584 + } else {
38585 + qh->nak_frame = 0xFFFF;
38586 + }
38587 + }
38588 + }
38589 +
38590 + if (microframe_schedule) {
38591 + if (hcd->available_host_channels < 1) {
38592 + break;
38593 + }
38594 + hcd->available_host_channels--;
38595 +#ifdef DEBUG_HOST_CHANNELS
38596 + last_sel_trans_num_nonper_scheduled++;
38597 +#endif /* DEBUG_HOST_CHANNELS */
38598 + }
38599 +
38600 + assign_and_init_hc(hcd, qh);
38601 +
38602 + /*
38603 + * Move the QH from the non-periodic inactive schedule to the
38604 + * non-periodic active schedule.
38605 + */
38606 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38607 + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
38608 + &qh->qh_list_entry);
38609 +
38610 + if (!microframe_schedule)
38611 + hcd->non_periodic_channels++;
38612 + }
38613 + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
38614 + * stop the FIQ from kicking us. We could potentially still have elements here if we
38615 + * ran out of host channels.
38616 + */
38617 + if (fiq_enable) {
38618 + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
38619 + hcd->fiq_state->kick_np_queues = 0;
38620 + } else {
38621 + /* For each entry remaining in the NP inactive queue,
38622 + * if this a NAK'd retransmit then don't set the kick flag.
38623 + */
38624 + if(nak_holdoff) {
38625 + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
38626 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38627 + if (qh->nak_frame == 0xFFFF) {
38628 + hcd->fiq_state->kick_np_queues = 1;
38629 + }
38630 + }
38631 + }
38632 + }
38633 + }
38634 + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
38635 + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
38636 +
38637 + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
38638 + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
38639 +
38640 +
38641 +#ifdef DEBUG_HOST_CHANNELS
38642 + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
38643 +#endif /* DEBUG_HOST_CHANNELS */
38644 + return ret_val;
38645 +}
38646 +
38647 +/**
38648 + * Attempts to queue a single transaction request for a host channel
38649 + * associated with either a periodic or non-periodic transfer. This function
38650 + * assumes that there is space available in the appropriate request queue. For
38651 + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
38652 + * is available in the appropriate Tx FIFO.
38653 + *
38654 + * @param hcd The HCD state structure.
38655 + * @param hc Host channel descriptor associated with either a periodic or
38656 + * non-periodic transfer.
38657 + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
38658 + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
38659 + * transfers.
38660 + *
38661 + * @return 1 if a request is queued and more requests may be needed to
38662 + * complete the transfer, 0 if no more requests are required for this
38663 + * transfer, -1 if there is insufficient space in the Tx FIFO.
38664 + */
38665 +static int queue_transaction(dwc_otg_hcd_t * hcd,
38666 + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
38667 +{
38668 + int retval;
38669 +
38670 + if (hcd->core_if->dma_enable) {
38671 + if (hcd->core_if->dma_desc_enable) {
38672 + if (!hc->xfer_started
38673 + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
38674 + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
38675 + hc->qh->ping_state = 0;
38676 + }
38677 + } else if (!hc->xfer_started) {
38678 + if (fiq_fsm_enable && hc->error_state) {
38679 + hcd->fiq_state->channel[hc->hc_num].nr_errors =
38680 + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
38681 + hcd->fiq_state->channel[hc->hc_num].fsm =
38682 + FIQ_PASSTHROUGH_ERRORSTATE;
38683 + }
38684 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38685 + hc->qh->ping_state = 0;
38686 + }
38687 + retval = 0;
38688 + } else if (hc->halt_pending) {
38689 + /* Don't queue a request if the channel has been halted. */
38690 + retval = 0;
38691 + } else if (hc->halt_on_queue) {
38692 + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
38693 + retval = 0;
38694 + } else if (hc->do_ping) {
38695 + if (!hc->xfer_started) {
38696 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38697 + }
38698 + retval = 0;
38699 + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
38700 + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
38701 + if (!hc->xfer_started) {
38702 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38703 + retval = 1;
38704 + } else {
38705 + retval =
38706 + dwc_otg_hc_continue_transfer(hcd->core_if,
38707 + hc);
38708 + }
38709 + } else {
38710 + retval = -1;
38711 + }
38712 + } else {
38713 + if (!hc->xfer_started) {
38714 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38715 + retval = 1;
38716 + } else {
38717 + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
38718 + }
38719 + }
38720 +
38721 + return retval;
38722 +}
38723 +
38724 +/**
38725 + * Processes periodic channels for the next frame and queues transactions for
38726 + * these channels to the DWC_otg controller. After queueing transactions, the
38727 + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
38728 + * to queue as Periodic Tx FIFO or request queue space becomes available.
38729 + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
38730 + */
38731 +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
38732 +{
38733 + hptxsts_data_t tx_status;
38734 + dwc_list_link_t *qh_ptr;
38735 + dwc_otg_qh_t *qh;
38736 + int status = 0;
38737 + int no_queue_space = 0;
38738 + int no_fifo_space = 0;
38739 +
38740 + dwc_otg_host_global_regs_t *host_regs;
38741 + host_regs = hcd->core_if->host_if->host_global_regs;
38742 +
38743 + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
38744 +#ifdef DEBUG
38745 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38746 + DWC_DEBUGPL(DBG_HCDV,
38747 + " P Tx Req Queue Space Avail (before queue): %d\n",
38748 + tx_status.b.ptxqspcavail);
38749 + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
38750 + tx_status.b.ptxfspcavail);
38751 +#endif
38752 +
38753 + qh_ptr = hcd->periodic_sched_assigned.next;
38754 + while (qh_ptr != &hcd->periodic_sched_assigned) {
38755 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38756 + if (tx_status.b.ptxqspcavail == 0) {
38757 + no_queue_space = 1;
38758 + break;
38759 + }
38760 +
38761 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38762 +
38763 + // Do not send a split start transaction any later than frame .6
38764 + // Note, we have to schedule a periodic in .5 to make it go in .6
38765 + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
38766 + {
38767 + qh_ptr = qh_ptr->next;
38768 + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
38769 + continue;
38770 + }
38771 +
38772 + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
38773 + if (qh->do_split)
38774 + fiq_fsm_queue_split_transaction(hcd, qh);
38775 + else
38776 + fiq_fsm_queue_isoc_transaction(hcd, qh);
38777 + } else {
38778 +
38779 + /*
38780 + * Set a flag if we're queueing high-bandwidth in slave mode.
38781 + * The flag prevents any halts to get into the request queue in
38782 + * the middle of multiple high-bandwidth packets getting queued.
38783 + */
38784 + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
38785 + hcd->core_if->queuing_high_bandwidth = 1;
38786 + }
38787 + status = queue_transaction(hcd, qh->channel,
38788 + tx_status.b.ptxfspcavail);
38789 + if (status < 0) {
38790 + no_fifo_space = 1;
38791 + break;
38792 + }
38793 + }
38794 +
38795 + /*
38796 + * In Slave mode, stay on the current transfer until there is
38797 + * nothing more to do or the high-bandwidth request count is
38798 + * reached. In DMA mode, only need to queue one request. The
38799 + * controller automatically handles multiple packets for
38800 + * high-bandwidth transfers.
38801 + */
38802 + if (hcd->core_if->dma_enable || status == 0 ||
38803 + qh->channel->requests == qh->channel->multi_count) {
38804 + qh_ptr = qh_ptr->next;
38805 + /*
38806 + * Move the QH from the periodic assigned schedule to
38807 + * the periodic queued schedule.
38808 + */
38809 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
38810 + &qh->qh_list_entry);
38811 +
38812 + /* done queuing high bandwidth */
38813 + hcd->core_if->queuing_high_bandwidth = 0;
38814 + }
38815 + }
38816 +
38817 + if (!hcd->core_if->dma_enable) {
38818 + dwc_otg_core_global_regs_t *global_regs;
38819 + gintmsk_data_t intr_mask = {.d32 = 0 };
38820 +
38821 + global_regs = hcd->core_if->core_global_regs;
38822 + intr_mask.b.ptxfempty = 1;
38823 +#ifdef DEBUG
38824 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38825 + DWC_DEBUGPL(DBG_HCDV,
38826 + " P Tx Req Queue Space Avail (after queue): %d\n",
38827 + tx_status.b.ptxqspcavail);
38828 + DWC_DEBUGPL(DBG_HCDV,
38829 + " P Tx FIFO Space Avail (after queue): %d\n",
38830 + tx_status.b.ptxfspcavail);
38831 +#endif
38832 + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
38833 + no_queue_space || no_fifo_space) {
38834 + /*
38835 + * May need to queue more transactions as the request
38836 + * queue or Tx FIFO empties. Enable the periodic Tx
38837 + * FIFO empty interrupt. (Always use the half-empty
38838 + * level to ensure that new requests are loaded as
38839 + * soon as possible.)
38840 + */
38841 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
38842 + intr_mask.d32);
38843 + } else {
38844 + /*
38845 + * Disable the Tx FIFO empty interrupt since there are
38846 + * no more transactions that need to be queued right
38847 + * now. This function is called from interrupt
38848 + * handlers to queue more transactions as transfer
38849 + * states change.
38850 + */
38851 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
38852 + 0);
38853 + }
38854 + }
38855 +}
38856 +
38857 +/**
38858 + * Processes active non-periodic channels and queues transactions for these
38859 + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
38860 + * FIFO Empty interrupt is enabled if there are more transactions to queue as
38861 + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
38862 + * FIFO Empty interrupt is disabled.
38863 + */
38864 +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
38865 +{
38866 + gnptxsts_data_t tx_status;
38867 + dwc_list_link_t *orig_qh_ptr;
38868 + dwc_otg_qh_t *qh;
38869 + int status;
38870 + int no_queue_space = 0;
38871 + int no_fifo_space = 0;
38872 + int more_to_do = 0;
38873 +
38874 + dwc_otg_core_global_regs_t *global_regs =
38875 + hcd->core_if->core_global_regs;
38876 +
38877 + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
38878 +#ifdef DEBUG
38879 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38880 + DWC_DEBUGPL(DBG_HCDV,
38881 + " NP Tx Req Queue Space Avail (before queue): %d\n",
38882 + tx_status.b.nptxqspcavail);
38883 + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
38884 + tx_status.b.nptxfspcavail);
38885 +#endif
38886 + /*
38887 + * Keep track of the starting point. Skip over the start-of-list
38888 + * entry.
38889 + */
38890 + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
38891 + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
38892 + }
38893 + orig_qh_ptr = hcd->non_periodic_qh_ptr;
38894 +
38895 + /*
38896 + * Process once through the active list or until no more space is
38897 + * available in the request queue or the Tx FIFO.
38898 + */
38899 + do {
38900 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38901 + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
38902 + no_queue_space = 1;
38903 + break;
38904 + }
38905 +
38906 + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
38907 + qh_list_entry);
38908 +
38909 + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
38910 + fiq_fsm_queue_split_transaction(hcd, qh);
38911 + } else {
38912 + status = queue_transaction(hcd, qh->channel,
38913 + tx_status.b.nptxfspcavail);
38914 +
38915 + if (status > 0) {
38916 + more_to_do = 1;
38917 + } else if (status < 0) {
38918 + no_fifo_space = 1;
38919 + break;
38920 + }
38921 + }
38922 + /* Advance to next QH, skipping start-of-list entry. */
38923 + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
38924 + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
38925 + hcd->non_periodic_qh_ptr =
38926 + hcd->non_periodic_qh_ptr->next;
38927 + }
38928 +
38929 + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
38930 +
38931 + if (!hcd->core_if->dma_enable) {
38932 + gintmsk_data_t intr_mask = {.d32 = 0 };
38933 + intr_mask.b.nptxfempty = 1;
38934 +
38935 +#ifdef DEBUG
38936 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38937 + DWC_DEBUGPL(DBG_HCDV,
38938 + " NP Tx Req Queue Space Avail (after queue): %d\n",
38939 + tx_status.b.nptxqspcavail);
38940 + DWC_DEBUGPL(DBG_HCDV,
38941 + " NP Tx FIFO Space Avail (after queue): %d\n",
38942 + tx_status.b.nptxfspcavail);
38943 +#endif
38944 + if (more_to_do || no_queue_space || no_fifo_space) {
38945 + /*
38946 + * May need to queue more transactions as the request
38947 + * queue or Tx FIFO empties. Enable the non-periodic
38948 + * Tx FIFO empty interrupt. (Always use the half-empty
38949 + * level to ensure that new requests are loaded as
38950 + * soon as possible.)
38951 + */
38952 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
38953 + intr_mask.d32);
38954 + } else {
38955 + /*
38956 + * Disable the Tx FIFO empty interrupt since there are
38957 + * no more transactions that need to be queued right
38958 + * now. This function is called from interrupt
38959 + * handlers to queue more transactions as transfer
38960 + * states change.
38961 + */
38962 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
38963 + 0);
38964 + }
38965 + }
38966 +}
38967 +
38968 +/**
38969 + * This function processes the currently active host channels and queues
38970 + * transactions for these channels to the DWC_otg controller. It is called
38971 + * from HCD interrupt handler functions.
38972 + *
38973 + * @param hcd The HCD state structure.
38974 + * @param tr_type The type(s) of transactions to queue (non-periodic,
38975 + * periodic, or both).
38976 + */
38977 +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
38978 + dwc_otg_transaction_type_e tr_type)
38979 +{
38980 +#ifdef DEBUG_SOF
38981 + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
38982 +#endif
38983 + /* Process host channels associated with periodic transfers. */
38984 + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
38985 + tr_type == DWC_OTG_TRANSACTION_ALL) &&
38986 + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
38987 +
38988 + process_periodic_channels(hcd);
38989 + }
38990 +
38991 + /* Process host channels associated with non-periodic transfers. */
38992 + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
38993 + tr_type == DWC_OTG_TRANSACTION_ALL) {
38994 + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
38995 + process_non_periodic_channels(hcd);
38996 + } else {
38997 + /*
38998 + * Ensure NP Tx FIFO empty interrupt is disabled when
38999 + * there are no non-periodic transfers to process.
39000 + */
39001 + gintmsk_data_t gintmsk = {.d32 = 0 };
39002 + gintmsk.b.nptxfempty = 1;
39003 +
39004 + if (fiq_enable) {
39005 + local_fiq_disable();
39006 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
39007 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
39008 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
39009 + local_fiq_enable();
39010 + } else {
39011 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
39012 + }
39013 + }
39014 + }
39015 +}
39016 +
39017 +#ifdef DWC_HS_ELECT_TST
39018 +/*
39019 + * Quick and dirty hack to implement the HS Electrical Test
39020 + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
39021 + *
39022 + * This code was copied from our userspace app "hset". It sends a
39023 + * Get Device Descriptor control sequence in two parts, first the
39024 + * Setup packet by itself, followed some time later by the In and
39025 + * Ack packets. Rather than trying to figure out how to add this
39026 + * functionality to the normal driver code, we just hijack the
39027 + * hardware, using these two function to drive the hardware
39028 + * directly.
39029 + */
39030 +
39031 +static dwc_otg_core_global_regs_t *global_regs;
39032 +static dwc_otg_host_global_regs_t *hc_global_regs;
39033 +static dwc_otg_hc_regs_t *hc_regs;
39034 +static uint32_t *data_fifo;
39035 +
39036 +static void do_setup(void)
39037 +{
39038 + gintsts_data_t gintsts;
39039 + hctsiz_data_t hctsiz;
39040 + hcchar_data_t hcchar;
39041 + haint_data_t haint;
39042 + hcint_data_t hcint;
39043 +
39044 + /* Enable HAINTs */
39045 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
39046 +
39047 + /* Enable HCINTs */
39048 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
39049 +
39050 + /* Read GINTSTS */
39051 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39052 +
39053 + /* Read HAINT */
39054 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39055 +
39056 + /* Read HCINT */
39057 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39058 +
39059 + /* Read HCCHAR */
39060 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39061 +
39062 + /* Clear HCINT */
39063 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39064 +
39065 + /* Clear HAINT */
39066 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39067 +
39068 + /* Clear GINTSTS */
39069 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39070 +
39071 + /* Read GINTSTS */
39072 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39073 +
39074 + /*
39075 + * Send Setup packet (Get Device Descriptor)
39076 + */
39077 +
39078 + /* Make sure channel is disabled */
39079 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39080 + if (hcchar.b.chen) {
39081 + hcchar.b.chdis = 1;
39082 +// hcchar.b.chen = 1;
39083 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39084 + //sleep(1);
39085 + dwc_mdelay(1000);
39086 +
39087 + /* Read GINTSTS */
39088 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39089 +
39090 + /* Read HAINT */
39091 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39092 +
39093 + /* Read HCINT */
39094 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39095 +
39096 + /* Read HCCHAR */
39097 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39098 +
39099 + /* Clear HCINT */
39100 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39101 +
39102 + /* Clear HAINT */
39103 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39104 +
39105 + /* Clear GINTSTS */
39106 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39107 +
39108 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39109 + }
39110 +
39111 + /* Set HCTSIZ */
39112 + hctsiz.d32 = 0;
39113 + hctsiz.b.xfersize = 8;
39114 + hctsiz.b.pktcnt = 1;
39115 + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
39116 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
39117 +
39118 + /* Set HCCHAR */
39119 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39120 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39121 + hcchar.b.epdir = 0;
39122 + hcchar.b.epnum = 0;
39123 + hcchar.b.mps = 8;
39124 + hcchar.b.chen = 1;
39125 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39126 +
39127 + /* Fill FIFO with Setup data for Get Device Descriptor */
39128 + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
39129 + DWC_WRITE_REG32(data_fifo++, 0x01000680);
39130 + DWC_WRITE_REG32(data_fifo++, 0x00080000);
39131 +
39132 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39133 +
39134 + /* Wait for host channel interrupt */
39135 + do {
39136 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39137 + } while (gintsts.b.hcintr == 0);
39138 +
39139 + /* Disable HCINTs */
39140 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
39141 +
39142 + /* Disable HAINTs */
39143 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
39144 +
39145 + /* Read HAINT */
39146 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39147 +
39148 + /* Read HCINT */
39149 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39150 +
39151 + /* Read HCCHAR */
39152 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39153 +
39154 + /* Clear HCINT */
39155 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39156 +
39157 + /* Clear HAINT */
39158 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39159 +
39160 + /* Clear GINTSTS */
39161 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39162 +
39163 + /* Read GINTSTS */
39164 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39165 +}
39166 +
39167 +static void do_in_ack(void)
39168 +{
39169 + gintsts_data_t gintsts;
39170 + hctsiz_data_t hctsiz;
39171 + hcchar_data_t hcchar;
39172 + haint_data_t haint;
39173 + hcint_data_t hcint;
39174 + host_grxsts_data_t grxsts;
39175 +
39176 + /* Enable HAINTs */
39177 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
39178 +
39179 + /* Enable HCINTs */
39180 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
39181 +
39182 + /* Read GINTSTS */
39183 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39184 +
39185 + /* Read HAINT */
39186 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39187 +
39188 + /* Read HCINT */
39189 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39190 +
39191 + /* Read HCCHAR */
39192 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39193 +
39194 + /* Clear HCINT */
39195 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39196 +
39197 + /* Clear HAINT */
39198 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39199 +
39200 + /* Clear GINTSTS */
39201 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39202 +
39203 + /* Read GINTSTS */
39204 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39205 +
39206 + /*
39207 + * Receive Control In packet
39208 + */
39209 +
39210 + /* Make sure channel is disabled */
39211 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39212 + if (hcchar.b.chen) {
39213 + hcchar.b.chdis = 1;
39214 + hcchar.b.chen = 1;
39215 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39216 + //sleep(1);
39217 + dwc_mdelay(1000);
39218 +
39219 + /* Read GINTSTS */
39220 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39221 +
39222 + /* Read HAINT */
39223 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39224 +
39225 + /* Read HCINT */
39226 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39227 +
39228 + /* Read HCCHAR */
39229 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39230 +
39231 + /* Clear HCINT */
39232 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39233 +
39234 + /* Clear HAINT */
39235 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39236 +
39237 + /* Clear GINTSTS */
39238 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39239 +
39240 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39241 + }
39242 +
39243 + /* Set HCTSIZ */
39244 + hctsiz.d32 = 0;
39245 + hctsiz.b.xfersize = 8;
39246 + hctsiz.b.pktcnt = 1;
39247 + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
39248 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
39249 +
39250 + /* Set HCCHAR */
39251 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39252 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39253 + hcchar.b.epdir = 1;
39254 + hcchar.b.epnum = 0;
39255 + hcchar.b.mps = 8;
39256 + hcchar.b.chen = 1;
39257 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39258 +
39259 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39260 +
39261 + /* Wait for receive status queue interrupt */
39262 + do {
39263 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39264 + } while (gintsts.b.rxstsqlvl == 0);
39265 +
39266 + /* Read RXSTS */
39267 + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
39268 +
39269 + /* Clear RXSTSQLVL in GINTSTS */
39270 + gintsts.d32 = 0;
39271 + gintsts.b.rxstsqlvl = 1;
39272 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39273 +
39274 + switch (grxsts.b.pktsts) {
39275 + case DWC_GRXSTS_PKTSTS_IN:
39276 + /* Read the data into the host buffer */
39277 + if (grxsts.b.bcnt > 0) {
39278 + int i;
39279 + int word_count = (grxsts.b.bcnt + 3) / 4;
39280 +
39281 + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
39282 +
39283 + for (i = 0; i < word_count; i++) {
39284 + (void)DWC_READ_REG32(data_fifo++);
39285 + }
39286 + }
39287 + break;
39288 +
39289 + default:
39290 + break;
39291 + }
39292 +
39293 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39294 +
39295 + /* Wait for receive status queue interrupt */
39296 + do {
39297 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39298 + } while (gintsts.b.rxstsqlvl == 0);
39299 +
39300 + /* Read RXSTS */
39301 + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
39302 +
39303 + /* Clear RXSTSQLVL in GINTSTS */
39304 + gintsts.d32 = 0;
39305 + gintsts.b.rxstsqlvl = 1;
39306 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39307 +
39308 + switch (grxsts.b.pktsts) {
39309 + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
39310 + break;
39311 +
39312 + default:
39313 + break;
39314 + }
39315 +
39316 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39317 +
39318 + /* Wait for host channel interrupt */
39319 + do {
39320 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39321 + } while (gintsts.b.hcintr == 0);
39322 +
39323 + /* Read HAINT */
39324 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39325 +
39326 + /* Read HCINT */
39327 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39328 +
39329 + /* Read HCCHAR */
39330 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39331 +
39332 + /* Clear HCINT */
39333 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39334 +
39335 + /* Clear HAINT */
39336 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39337 +
39338 + /* Clear GINTSTS */
39339 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39340 +
39341 + /* Read GINTSTS */
39342 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39343 +
39344 +// usleep(100000);
39345 +// mdelay(100);
39346 + dwc_mdelay(1);
39347 +
39348 + /*
39349 + * Send handshake packet
39350 + */
39351 +
39352 + /* Read HAINT */
39353 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39354 +
39355 + /* Read HCINT */
39356 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39357 +
39358 + /* Read HCCHAR */
39359 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39360 +
39361 + /* Clear HCINT */
39362 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39363 +
39364 + /* Clear HAINT */
39365 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39366 +
39367 + /* Clear GINTSTS */
39368 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39369 +
39370 + /* Read GINTSTS */
39371 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39372 +
39373 + /* Make sure channel is disabled */
39374 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39375 + if (hcchar.b.chen) {
39376 + hcchar.b.chdis = 1;
39377 + hcchar.b.chen = 1;
39378 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39379 + //sleep(1);
39380 + dwc_mdelay(1000);
39381 +
39382 + /* Read GINTSTS */
39383 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39384 +
39385 + /* Read HAINT */
39386 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39387 +
39388 + /* Read HCINT */
39389 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39390 +
39391 + /* Read HCCHAR */
39392 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39393 +
39394 + /* Clear HCINT */
39395 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39396 +
39397 + /* Clear HAINT */
39398 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39399 +
39400 + /* Clear GINTSTS */
39401 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39402 +
39403 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39404 + }
39405 +
39406 + /* Set HCTSIZ */
39407 + hctsiz.d32 = 0;
39408 + hctsiz.b.xfersize = 0;
39409 + hctsiz.b.pktcnt = 1;
39410 + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
39411 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
39412 +
39413 + /* Set HCCHAR */
39414 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39415 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39416 + hcchar.b.epdir = 0;
39417 + hcchar.b.epnum = 0;
39418 + hcchar.b.mps = 8;
39419 + hcchar.b.chen = 1;
39420 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39421 +
39422 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39423 +
39424 + /* Wait for host channel interrupt */
39425 + do {
39426 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39427 + } while (gintsts.b.hcintr == 0);
39428 +
39429 + /* Disable HCINTs */
39430 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
39431 +
39432 + /* Disable HAINTs */
39433 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
39434 +
39435 + /* Read HAINT */
39436 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39437 +
39438 + /* Read HCINT */
39439 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39440 +
39441 + /* Read HCCHAR */
39442 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39443 +
39444 + /* Clear HCINT */
39445 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39446 +
39447 + /* Clear HAINT */
39448 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39449 +
39450 + /* Clear GINTSTS */
39451 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39452 +
39453 + /* Read GINTSTS */
39454 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39455 +}
39456 +#endif
39457 +
39458 +/** Handles hub class-specific requests. */
39459 +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
39460 + uint16_t typeReq,
39461 + uint16_t wValue,
39462 + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
39463 +{
39464 + int retval = 0;
39465 +
39466 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
39467 + usb_hub_descriptor_t *hub_desc;
39468 + hprt0_data_t hprt0 = {.d32 = 0 };
39469 +
39470 + uint32_t port_status;
39471 +
39472 + switch (typeReq) {
39473 + case UCR_CLEAR_HUB_FEATURE:
39474 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39475 + "ClearHubFeature 0x%x\n", wValue);
39476 + switch (wValue) {
39477 + case UHF_C_HUB_LOCAL_POWER:
39478 + case UHF_C_HUB_OVER_CURRENT:
39479 + /* Nothing required here */
39480 + break;
39481 + default:
39482 + retval = -DWC_E_INVALID;
39483 + DWC_ERROR("DWC OTG HCD - "
39484 + "ClearHubFeature request %xh unknown\n",
39485 + wValue);
39486 + }
39487 + break;
39488 + case UCR_CLEAR_PORT_FEATURE:
39489 +#ifdef CONFIG_USB_DWC_OTG_LPM
39490 + if (wValue != UHF_PORT_L1)
39491 +#endif
39492 + if (!wIndex || wIndex > 1)
39493 + goto error;
39494 +
39495 + switch (wValue) {
39496 + case UHF_PORT_ENABLE:
39497 + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
39498 + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
39499 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39500 + hprt0.b.prtena = 1;
39501 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39502 + break;
39503 + case UHF_PORT_SUSPEND:
39504 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39505 + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
39506 +
39507 + if (core_if->power_down == 2) {
39508 + dwc_otg_host_hibernation_restore(core_if, 0, 0);
39509 + } else {
39510 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
39511 + dwc_mdelay(5);
39512 +
39513 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39514 + hprt0.b.prtres = 1;
39515 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39516 + hprt0.b.prtsusp = 0;
39517 + /* Clear Resume bit */
39518 + dwc_mdelay(100);
39519 + hprt0.b.prtres = 0;
39520 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39521 + }
39522 + break;
39523 +#ifdef CONFIG_USB_DWC_OTG_LPM
39524 + case UHF_PORT_L1:
39525 + {
39526 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39527 + glpmcfg_data_t lpmcfg = {.d32 = 0 };
39528 +
39529 + lpmcfg.d32 =
39530 + DWC_READ_REG32(&core_if->
39531 + core_global_regs->glpmcfg);
39532 + lpmcfg.b.en_utmi_sleep = 0;
39533 + lpmcfg.b.hird_thres &= (~(1 << 4));
39534 + lpmcfg.b.prt_sleep_sts = 1;
39535 + DWC_WRITE_REG32(&core_if->
39536 + core_global_regs->glpmcfg,
39537 + lpmcfg.d32);
39538 +
39539 + /* Clear Enbl_L1Gating bit. */
39540 + pcgcctl.b.enbl_sleep_gating = 1;
39541 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
39542 + 0);
39543 +
39544 + dwc_mdelay(5);
39545 +
39546 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39547 + hprt0.b.prtres = 1;
39548 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39549 + hprt0.d32);
39550 + /* This bit will be cleared in wakeup interrupt handle */
39551 + break;
39552 + }
39553 +#endif
39554 + case UHF_PORT_POWER:
39555 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39556 + "ClearPortFeature USB_PORT_FEAT_POWER\n");
39557 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39558 + hprt0.b.prtpwr = 0;
39559 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39560 + break;
39561 + case UHF_PORT_INDICATOR:
39562 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39563 + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
39564 + /* Port inidicator not supported */
39565 + break;
39566 + case UHF_C_PORT_CONNECTION:
39567 + /* Clears drivers internal connect status change
39568 + * flag */
39569 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39570 + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
39571 + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
39572 + break;
39573 + case UHF_C_PORT_RESET:
39574 + /* Clears the driver's internal Port Reset Change
39575 + * flag */
39576 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39577 + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
39578 + dwc_otg_hcd->flags.b.port_reset_change = 0;
39579 + break;
39580 + case UHF_C_PORT_ENABLE:
39581 + /* Clears the driver's internal Port
39582 + * Enable/Disable Change flag */
39583 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39584 + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
39585 + dwc_otg_hcd->flags.b.port_enable_change = 0;
39586 + break;
39587 + case UHF_C_PORT_SUSPEND:
39588 + /* Clears the driver's internal Port Suspend
39589 + * Change flag, which is set when resume signaling on
39590 + * the host port is complete */
39591 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39592 + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
39593 + dwc_otg_hcd->flags.b.port_suspend_change = 0;
39594 + break;
39595 +#ifdef CONFIG_USB_DWC_OTG_LPM
39596 + case UHF_C_PORT_L1:
39597 + dwc_otg_hcd->flags.b.port_l1_change = 0;
39598 + break;
39599 +#endif
39600 + case UHF_C_PORT_OVER_CURRENT:
39601 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39602 + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
39603 + dwc_otg_hcd->flags.b.port_over_current_change = 0;
39604 + break;
39605 + default:
39606 + retval = -DWC_E_INVALID;
39607 + DWC_ERROR("DWC OTG HCD - "
39608 + "ClearPortFeature request %xh "
39609 + "unknown or unsupported\n", wValue);
39610 + }
39611 + break;
39612 + case UCR_GET_HUB_DESCRIPTOR:
39613 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39614 + "GetHubDescriptor\n");
39615 + hub_desc = (usb_hub_descriptor_t *) buf;
39616 + hub_desc->bDescLength = 9;
39617 + hub_desc->bDescriptorType = 0x29;
39618 + hub_desc->bNbrPorts = 1;
39619 + USETW(hub_desc->wHubCharacteristics, 0x08);
39620 + hub_desc->bPwrOn2PwrGood = 1;
39621 + hub_desc->bHubContrCurrent = 0;
39622 + hub_desc->DeviceRemovable[0] = 0;
39623 + hub_desc->DeviceRemovable[1] = 0xff;
39624 + break;
39625 + case UCR_GET_HUB_STATUS:
39626 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39627 + "GetHubStatus\n");
39628 + DWC_MEMSET(buf, 0, 4);
39629 + break;
39630 + case UCR_GET_PORT_STATUS:
39631 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39632 + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
39633 + wIndex, dwc_otg_hcd->flags.d32);
39634 + if (!wIndex || wIndex > 1)
39635 + goto error;
39636 +
39637 + port_status = 0;
39638 +
39639 + if (dwc_otg_hcd->flags.b.port_connect_status_change)
39640 + port_status |= (1 << UHF_C_PORT_CONNECTION);
39641 +
39642 + if (dwc_otg_hcd->flags.b.port_enable_change)
39643 + port_status |= (1 << UHF_C_PORT_ENABLE);
39644 +
39645 + if (dwc_otg_hcd->flags.b.port_suspend_change)
39646 + port_status |= (1 << UHF_C_PORT_SUSPEND);
39647 +
39648 + if (dwc_otg_hcd->flags.b.port_l1_change)
39649 + port_status |= (1 << UHF_C_PORT_L1);
39650 +
39651 + if (dwc_otg_hcd->flags.b.port_reset_change) {
39652 + port_status |= (1 << UHF_C_PORT_RESET);
39653 + }
39654 +
39655 + if (dwc_otg_hcd->flags.b.port_over_current_change) {
39656 + DWC_WARN("Overcurrent change detected\n");
39657 + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
39658 + }
39659 +
39660 + if (!dwc_otg_hcd->flags.b.port_connect_status) {
39661 + /*
39662 + * The port is disconnected, which means the core is
39663 + * either in device mode or it soon will be. Just
39664 + * return 0's for the remainder of the port status
39665 + * since the port register can't be read if the core
39666 + * is in device mode.
39667 + */
39668 + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
39669 + break;
39670 + }
39671 +
39672 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
39673 + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
39674 +
39675 + if (hprt0.b.prtconnsts)
39676 + port_status |= (1 << UHF_PORT_CONNECTION);
39677 +
39678 + if (hprt0.b.prtena)
39679 + port_status |= (1 << UHF_PORT_ENABLE);
39680 +
39681 + if (hprt0.b.prtsusp)
39682 + port_status |= (1 << UHF_PORT_SUSPEND);
39683 +
39684 + if (hprt0.b.prtovrcurract)
39685 + port_status |= (1 << UHF_PORT_OVER_CURRENT);
39686 +
39687 + if (hprt0.b.prtrst)
39688 + port_status |= (1 << UHF_PORT_RESET);
39689 +
39690 + if (hprt0.b.prtpwr)
39691 + port_status |= (1 << UHF_PORT_POWER);
39692 +
39693 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
39694 + port_status |= (1 << UHF_PORT_HIGH_SPEED);
39695 + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
39696 + port_status |= (1 << UHF_PORT_LOW_SPEED);
39697 +
39698 + if (hprt0.b.prttstctl)
39699 + port_status |= (1 << UHF_PORT_TEST);
39700 + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
39701 + port_status |= (1 << UHF_PORT_L1);
39702 + }
39703 + /*
39704 + For Synopsys HW emulation of Power down wkup_control asserts the
39705 + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
39706 + We intentionally tell the software that port is in L2Suspend state.
39707 + Only for STE.
39708 + */
39709 + if ((core_if->power_down == 2)
39710 + && (core_if->hibernation_suspend == 1)) {
39711 + port_status |= (1 << UHF_PORT_SUSPEND);
39712 + }
39713 + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
39714 +
39715 + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
39716 +
39717 + break;
39718 + case UCR_SET_HUB_FEATURE:
39719 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39720 + "SetHubFeature\n");
39721 + /* No HUB features supported */
39722 + break;
39723 + case UCR_SET_PORT_FEATURE:
39724 + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
39725 + goto error;
39726 +
39727 + if (!dwc_otg_hcd->flags.b.port_connect_status) {
39728 + /*
39729 + * The port is disconnected, which means the core is
39730 + * either in device mode or it soon will be. Just
39731 + * return without doing anything since the port
39732 + * register can't be written if the core is in device
39733 + * mode.
39734 + */
39735 + break;
39736 + }
39737 +
39738 + switch (wValue) {
39739 + case UHF_PORT_SUSPEND:
39740 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39741 + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
39742 + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
39743 + goto error;
39744 + }
39745 + if (core_if->power_down == 2) {
39746 + int timeout = 300;
39747 + dwc_irqflags_t flags;
39748 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39749 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
39750 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
39751 +#ifdef DWC_DEV_SRPCAP
39752 + int32_t otg_cap_param = core_if->core_params->otg_cap;
39753 +#endif
39754 + DWC_PRINTF("Preparing for complete power-off\n");
39755 +
39756 + /* Save registers before hibernation */
39757 + dwc_otg_save_global_regs(core_if);
39758 + dwc_otg_save_host_regs(core_if);
39759 +
39760 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39761 + hprt0.b.prtsusp = 1;
39762 + hprt0.b.prtena = 0;
39763 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39764 + /* Spin hprt0.b.prtsusp to became 1 */
39765 + do {
39766 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39767 + if (hprt0.b.prtsusp) {
39768 + break;
39769 + }
39770 + dwc_mdelay(1);
39771 + } while (--timeout);
39772 + if (!timeout) {
39773 + DWC_WARN("Suspend wasn't genereted\n");
39774 + }
39775 + dwc_udelay(10);
39776 +
39777 + /*
39778 + * We need to disable interrupts to prevent servicing of any IRQ
39779 + * during going to hibernation
39780 + */
39781 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
39782 + core_if->lx_state = DWC_OTG_L2;
39783 +#ifdef DWC_DEV_SRPCAP
39784 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39785 + hprt0.b.prtpwr = 0;
39786 + hprt0.b.prtena = 0;
39787 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39788 + hprt0.d32);
39789 +#endif
39790 + gusbcfg.d32 =
39791 + DWC_READ_REG32(&core_if->core_global_regs->
39792 + gusbcfg);
39793 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
39794 + /* ULPI interface */
39795 + /* Suspend the Phy Clock */
39796 + pcgcctl.d32 = 0;
39797 + pcgcctl.b.stoppclk = 1;
39798 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
39799 + pcgcctl.d32);
39800 + dwc_udelay(10);
39801 + gpwrdn.b.pmuactv = 1;
39802 + DWC_MODIFY_REG32(&core_if->
39803 + core_global_regs->
39804 + gpwrdn, 0, gpwrdn.d32);
39805 + } else {
39806 + /* UTMI+ Interface */
39807 + gpwrdn.b.pmuactv = 1;
39808 + DWC_MODIFY_REG32(&core_if->
39809 + core_global_regs->
39810 + gpwrdn, 0, gpwrdn.d32);
39811 + dwc_udelay(10);
39812 + pcgcctl.b.stoppclk = 1;
39813 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
39814 + dwc_udelay(10);
39815 + }
39816 +#ifdef DWC_DEV_SRPCAP
39817 + gpwrdn.d32 = 0;
39818 + gpwrdn.b.dis_vbus = 1;
39819 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39820 + gpwrdn, 0, gpwrdn.d32);
39821 +#endif
39822 + gpwrdn.d32 = 0;
39823 + gpwrdn.b.pmuintsel = 1;
39824 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39825 + gpwrdn, 0, gpwrdn.d32);
39826 + dwc_udelay(10);
39827 +
39828 + gpwrdn.d32 = 0;
39829 +#ifdef DWC_DEV_SRPCAP
39830 + gpwrdn.b.srp_det_msk = 1;
39831 +#endif
39832 + gpwrdn.b.disconn_det_msk = 1;
39833 + gpwrdn.b.lnstchng_msk = 1;
39834 + gpwrdn.b.sts_chngint_msk = 1;
39835 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39836 + gpwrdn, 0, gpwrdn.d32);
39837 + dwc_udelay(10);
39838 +
39839 + /* Enable Power Down Clamp and all interrupts in GPWRDN */
39840 + gpwrdn.d32 = 0;
39841 + gpwrdn.b.pwrdnclmp = 1;
39842 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39843 + gpwrdn, 0, gpwrdn.d32);
39844 + dwc_udelay(10);
39845 +
39846 + /* Switch off VDD */
39847 + gpwrdn.d32 = 0;
39848 + gpwrdn.b.pwrdnswtch = 1;
39849 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39850 + gpwrdn, 0, gpwrdn.d32);
39851 +
39852 +#ifdef DWC_DEV_SRPCAP
39853 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
39854 + {
39855 + core_if->pwron_timer_started = 1;
39856 + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
39857 + }
39858 +#endif
39859 + /* Save gpwrdn register for further usage if stschng interrupt */
39860 + core_if->gr_backup->gpwrdn_local =
39861 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
39862 +
39863 + /* Set flag to indicate that we are in hibernation */
39864 + core_if->hibernation_suspend = 1;
39865 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
39866 +
39867 + DWC_PRINTF("Host hibernation completed\n");
39868 + // Exit from case statement
39869 + break;
39870 +
39871 + }
39872 + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
39873 + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
39874 + gotgctl_data_t gotgctl = {.d32 = 0 };
39875 + gotgctl.b.hstsethnpen = 1;
39876 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39877 + gotgctl, 0, gotgctl.d32);
39878 + core_if->op_state = A_SUSPEND;
39879 + }
39880 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39881 + hprt0.b.prtsusp = 1;
39882 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39883 + {
39884 + dwc_irqflags_t flags;
39885 + /* Update lx_state */
39886 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
39887 + core_if->lx_state = DWC_OTG_L2;
39888 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
39889 + }
39890 + /* Suspend the Phy Clock */
39891 + {
39892 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39893 + pcgcctl.b.stoppclk = 1;
39894 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
39895 + pcgcctl.d32);
39896 + dwc_udelay(10);
39897 + }
39898 +
39899 + /* For HNP the bus must be suspended for at least 200ms. */
39900 + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
39901 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39902 + pcgcctl.b.stoppclk = 1;
39903 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
39904 + dwc_mdelay(200);
39905 + }
39906 +
39907 + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
39908 +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
39909 + if (core_if->adp_enable) {
39910 + gotgctl_data_t gotgctl = {.d32 = 0 };
39911 + gpwrdn_data_t gpwrdn;
39912 +
39913 + while (gotgctl.b.asesvld == 1) {
39914 + gotgctl.d32 =
39915 + DWC_READ_REG32(&core_if->
39916 + core_global_regs->
39917 + gotgctl);
39918 + dwc_mdelay(100);
39919 + }
39920 +
39921 + /* Enable Power Down Logic */
39922 + gpwrdn.d32 = 0;
39923 + gpwrdn.b.pmuactv = 1;
39924 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39925 + gpwrdn, 0, gpwrdn.d32);
39926 +
39927 + /* Unmask SRP detected interrupt from Power Down Logic */
39928 + gpwrdn.d32 = 0;
39929 + gpwrdn.b.srp_det_msk = 1;
39930 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39931 + gpwrdn, 0, gpwrdn.d32);
39932 +
39933 + dwc_otg_adp_probe_start(core_if);
39934 + }
39935 +#endif
39936 + break;
39937 + case UHF_PORT_POWER:
39938 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39939 + "SetPortFeature - USB_PORT_FEAT_POWER\n");
39940 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39941 + hprt0.b.prtpwr = 1;
39942 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39943 + break;
39944 + case UHF_PORT_RESET:
39945 + if ((core_if->power_down == 2)
39946 + && (core_if->hibernation_suspend == 1)) {
39947 + /* If we are going to exit from Hibernated
39948 + * state via USB RESET.
39949 + */
39950 + dwc_otg_host_hibernation_restore(core_if, 0, 1);
39951 + } else {
39952 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39953 +
39954 + DWC_DEBUGPL(DBG_HCD,
39955 + "DWC OTG HCD HUB CONTROL - "
39956 + "SetPortFeature - USB_PORT_FEAT_RESET\n");
39957 + {
39958 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39959 + pcgcctl.b.enbl_sleep_gating = 1;
39960 + pcgcctl.b.stoppclk = 1;
39961 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
39962 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
39963 + }
39964 +#ifdef CONFIG_USB_DWC_OTG_LPM
39965 + {
39966 + glpmcfg_data_t lpmcfg;
39967 + lpmcfg.d32 =
39968 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
39969 + if (lpmcfg.b.prt_sleep_sts) {
39970 + lpmcfg.b.en_utmi_sleep = 0;
39971 + lpmcfg.b.hird_thres &= (~(1 << 4));
39972 + DWC_WRITE_REG32
39973 + (&core_if->core_global_regs->glpmcfg,
39974 + lpmcfg.d32);
39975 + dwc_mdelay(1);
39976 + }
39977 + }
39978 +#endif
39979 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39980 + /* Clear suspend bit if resetting from suspended state. */
39981 + hprt0.b.prtsusp = 0;
39982 + /* When B-Host the Port reset bit is set in
39983 + * the Start HCD Callback function, so that
39984 + * the reset is started within 1ms of the HNP
39985 + * success interrupt. */
39986 + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
39987 + hprt0.b.prtpwr = 1;
39988 + hprt0.b.prtrst = 1;
39989 + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
39990 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39991 + hprt0.d32);
39992 + }
39993 + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
39994 + dwc_mdelay(60);
39995 + hprt0.b.prtrst = 0;
39996 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39997 + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
39998 + }
39999 + break;
40000 +#ifdef DWC_HS_ELECT_TST
40001 + case UHF_PORT_TEST:
40002 + {
40003 + uint32_t t;
40004 + gintmsk_data_t gintmsk;
40005 +
40006 + t = (wIndex >> 8); /* MSB wIndex USB */
40007 + DWC_DEBUGPL(DBG_HCD,
40008 + "DWC OTG HCD HUB CONTROL - "
40009 + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
40010 + t);
40011 + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
40012 + if (t < 6) {
40013 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
40014 + hprt0.b.prttstctl = t;
40015 + DWC_WRITE_REG32(core_if->host_if->hprt0,
40016 + hprt0.d32);
40017 + } else {
40018 + /* Setup global vars with reg addresses (quick and
40019 + * dirty hack, should be cleaned up)
40020 + */
40021 + global_regs = core_if->core_global_regs;
40022 + hc_global_regs =
40023 + core_if->host_if->host_global_regs;
40024 + hc_regs =
40025 + (dwc_otg_hc_regs_t *) ((char *)
40026 + global_regs +
40027 + 0x500);
40028 + data_fifo =
40029 + (uint32_t *) ((char *)global_regs +
40030 + 0x1000);
40031 +
40032 + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
40033 + /* Save current interrupt mask */
40034 + gintmsk.d32 =
40035 + DWC_READ_REG32
40036 + (&global_regs->gintmsk);
40037 +
40038 + /* Disable all interrupts while we muck with
40039 + * the hardware directly
40040 + */
40041 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
40042 +
40043 + /* 15 second delay per the test spec */
40044 + dwc_mdelay(15000);
40045 +
40046 + /* Drive suspend on the root port */
40047 + hprt0.d32 =
40048 + dwc_otg_read_hprt0(core_if);
40049 + hprt0.b.prtsusp = 1;
40050 + hprt0.b.prtres = 0;
40051 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
40052 +
40053 + /* 15 second delay per the test spec */
40054 + dwc_mdelay(15000);
40055 +
40056 + /* Drive resume on the root port */
40057 + hprt0.d32 =
40058 + dwc_otg_read_hprt0(core_if);
40059 + hprt0.b.prtsusp = 0;
40060 + hprt0.b.prtres = 1;
40061 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
40062 + dwc_mdelay(100);
40063 +
40064 + /* Clear the resume bit */
40065 + hprt0.b.prtres = 0;
40066 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
40067 +
40068 + /* Restore interrupts */
40069 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
40070 + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
40071 + /* Save current interrupt mask */
40072 + gintmsk.d32 =
40073 + DWC_READ_REG32
40074 + (&global_regs->gintmsk);
40075 +
40076 + /* Disable all interrupts while we muck with
40077 + * the hardware directly
40078 + */
40079 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
40080 +
40081 + /* 15 second delay per the test spec */
40082 + dwc_mdelay(15000);
40083 +
40084 + /* Send the Setup packet */
40085 + do_setup();
40086 +
40087 + /* 15 second delay so nothing else happens for awhile */
40088 + dwc_mdelay(15000);
40089 +
40090 + /* Restore interrupts */
40091 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
40092 + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
40093 + /* Save current interrupt mask */
40094 + gintmsk.d32 =
40095 + DWC_READ_REG32
40096 + (&global_regs->gintmsk);
40097 +
40098 + /* Disable all interrupts while we muck with
40099 + * the hardware directly
40100 + */
40101 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
40102 +
40103 + /* Send the Setup packet */
40104 + do_setup();
40105 +
40106 + /* 15 second delay so nothing else happens for awhile */
40107 + dwc_mdelay(15000);
40108 +
40109 + /* Send the In and Ack packets */
40110 + do_in_ack();
40111 +
40112 + /* 15 second delay so nothing else happens for awhile */
40113 + dwc_mdelay(15000);
40114 +
40115 + /* Restore interrupts */
40116 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
40117 + }
40118 + }
40119 + break;
40120 + }
40121 +#endif /* DWC_HS_ELECT_TST */
40122 +
40123 + case UHF_PORT_INDICATOR:
40124 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
40125 + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
40126 + /* Not supported */
40127 + break;
40128 + default:
40129 + retval = -DWC_E_INVALID;
40130 + DWC_ERROR("DWC OTG HCD - "
40131 + "SetPortFeature request %xh "
40132 + "unknown or unsupported\n", wValue);
40133 + break;
40134 + }
40135 + break;
40136 +#ifdef CONFIG_USB_DWC_OTG_LPM
40137 + case UCR_SET_AND_TEST_PORT_FEATURE:
40138 + if (wValue != UHF_PORT_L1) {
40139 + goto error;
40140 + }
40141 + {
40142 + int portnum, hird, devaddr, remwake;
40143 + glpmcfg_data_t lpmcfg;
40144 + uint32_t time_usecs;
40145 + gintsts_data_t gintsts;
40146 + gintmsk_data_t gintmsk;
40147 +
40148 + if (!dwc_otg_get_param_lpm_enable(core_if)) {
40149 + goto error;
40150 + }
40151 + if (wValue != UHF_PORT_L1 || wLength != 1) {
40152 + goto error;
40153 + }
40154 + /* Check if the port currently is in SLEEP state */
40155 + lpmcfg.d32 =
40156 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
40157 + if (lpmcfg.b.prt_sleep_sts) {
40158 + DWC_INFO("Port is already in sleep mode\n");
40159 + buf[0] = 0; /* Return success */
40160 + break;
40161 + }
40162 +
40163 + portnum = wIndex & 0xf;
40164 + hird = (wIndex >> 4) & 0xf;
40165 + devaddr = (wIndex >> 8) & 0x7f;
40166 + remwake = (wIndex >> 15);
40167 +
40168 + if (portnum != 1) {
40169 + retval = -DWC_E_INVALID;
40170 + DWC_WARN
40171 + ("Wrong port number(%d) in SetandTestPortFeature request\n",
40172 + portnum);
40173 + break;
40174 + }
40175 +
40176 + DWC_PRINTF
40177 + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
40178 + portnum, hird, devaddr, remwake);
40179 + /* Disable LPM interrupt */
40180 + gintmsk.d32 = 0;
40181 + gintmsk.b.lpmtranrcvd = 1;
40182 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
40183 + gintmsk.d32, 0);
40184 +
40185 + if (dwc_otg_hcd_send_lpm
40186 + (dwc_otg_hcd, devaddr, hird, remwake)) {
40187 + retval = -DWC_E_INVALID;
40188 + break;
40189 + }
40190 +
40191 + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
40192 + /* We will consider timeout if time_usecs microseconds pass,
40193 + * and we don't receive LPM transaction status.
40194 + * After receiving non-error responce(ACK/NYET/STALL) from device,
40195 + * core will set lpmtranrcvd bit.
40196 + */
40197 + do {
40198 + gintsts.d32 =
40199 + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
40200 + if (gintsts.b.lpmtranrcvd) {
40201 + break;
40202 + }
40203 + dwc_udelay(1);
40204 + } while (--time_usecs);
40205 + /* lpm_int bit will be cleared in LPM interrupt handler */
40206 +
40207 + /* Now fill status
40208 + * 0x00 - Success
40209 + * 0x10 - NYET
40210 + * 0x11 - Timeout
40211 + */
40212 + if (!gintsts.b.lpmtranrcvd) {
40213 + buf[0] = 0x3; /* Completion code is Timeout */
40214 + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
40215 + } else {
40216 + lpmcfg.d32 =
40217 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
40218 + if (lpmcfg.b.lpm_resp == 0x3) {
40219 + /* ACK responce from the device */
40220 + buf[0] = 0x00; /* Success */
40221 + } else if (lpmcfg.b.lpm_resp == 0x2) {
40222 + /* NYET responce from the device */
40223 + buf[0] = 0x2;
40224 + } else {
40225 + /* Otherwise responce with Timeout */
40226 + buf[0] = 0x3;
40227 + }
40228 + }
40229 + DWC_PRINTF("Device responce to LPM trans is %x\n",
40230 + lpmcfg.b.lpm_resp);
40231 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
40232 + gintmsk.d32);
40233 +
40234 + break;
40235 + }
40236 +#endif /* CONFIG_USB_DWC_OTG_LPM */
40237 + default:
40238 +error:
40239 + retval = -DWC_E_INVALID;
40240 + DWC_WARN("DWC OTG HCD - "
40241 + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
40242 + typeReq, wIndex, wValue);
40243 + break;
40244 + }
40245 +
40246 + return retval;
40247 +}
40248 +
40249 +#ifdef CONFIG_USB_DWC_OTG_LPM
40250 +/** Returns index of host channel to perform LPM transaction. */
40251 +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
40252 +{
40253 + dwc_otg_core_if_t *core_if = hcd->core_if;
40254 + dwc_hc_t *hc;
40255 + hcchar_data_t hcchar;
40256 + gintmsk_data_t gintmsk = {.d32 = 0 };
40257 +
40258 + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
40259 + DWC_PRINTF("No free channel to select for LPM transaction\n");
40260 + return -1;
40261 + }
40262 +
40263 + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
40264 +
40265 + /* Mask host channel interrupts. */
40266 + gintmsk.b.hcintr = 1;
40267 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
40268 +
40269 + /* Fill fields that core needs for LPM transaction */
40270 + hcchar.b.devaddr = devaddr;
40271 + hcchar.b.epnum = 0;
40272 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
40273 + hcchar.b.mps = 64;
40274 + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
40275 + hcchar.b.epdir = 0; /* OUT */
40276 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
40277 + hcchar.d32);
40278 +
40279 + /* Remove the host channel from the free list. */
40280 + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
40281 +
40282 + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
40283 +
40284 + return hc->hc_num;
40285 +}
40286 +
40287 +/** Release hc after performing LPM transaction */
40288 +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
40289 +{
40290 + dwc_hc_t *hc;
40291 + glpmcfg_data_t lpmcfg;
40292 + uint8_t hc_num;
40293 +
40294 + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
40295 + hc_num = lpmcfg.b.lpm_chan_index;
40296 +
40297 + hc = hcd->hc_ptr_array[hc_num];
40298 +
40299 + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
40300 + /* Return host channel to free list */
40301 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
40302 +}
40303 +
40304 +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
40305 + uint8_t bRemoteWake)
40306 +{
40307 + glpmcfg_data_t lpmcfg;
40308 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
40309 + int channel;
40310 +
40311 + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
40312 + if (channel < 0) {
40313 + return channel;
40314 + }
40315 +
40316 + pcgcctl.b.enbl_sleep_gating = 1;
40317 + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
40318 +
40319 + /* Read LPM config register */
40320 + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
40321 +
40322 + /* Program LPM transaction fields */
40323 + lpmcfg.b.rem_wkup_en = bRemoteWake;
40324 + lpmcfg.b.hird = hird;
40325 + lpmcfg.b.hird_thres = 0x1c;
40326 + lpmcfg.b.lpm_chan_index = channel;
40327 + lpmcfg.b.en_utmi_sleep = 1;
40328 + /* Program LPM config register */
40329 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
40330 +
40331 + /* Send LPM transaction */
40332 + lpmcfg.b.send_lpm = 1;
40333 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
40334 +
40335 + return 0;
40336 +}
40337 +
40338 +#endif /* CONFIG_USB_DWC_OTG_LPM */
40339 +
40340 +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
40341 +{
40342 + int retval;
40343 +
40344 + if (port != 1) {
40345 + return -DWC_E_INVALID;
40346 + }
40347 +
40348 + retval = (hcd->flags.b.port_connect_status_change ||
40349 + hcd->flags.b.port_reset_change ||
40350 + hcd->flags.b.port_enable_change ||
40351 + hcd->flags.b.port_suspend_change ||
40352 + hcd->flags.b.port_over_current_change);
40353 +#ifdef DEBUG
40354 + if (retval) {
40355 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
40356 + " Root port status changed\n");
40357 + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
40358 + hcd->flags.b.port_connect_status_change);
40359 + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
40360 + hcd->flags.b.port_reset_change);
40361 + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
40362 + hcd->flags.b.port_enable_change);
40363 + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
40364 + hcd->flags.b.port_suspend_change);
40365 + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
40366 + hcd->flags.b.port_over_current_change);
40367 + }
40368 +#endif
40369 + return retval;
40370 +}
40371 +
40372 +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
40373 +{
40374 + hfnum_data_t hfnum;
40375 + hfnum.d32 =
40376 + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
40377 + hfnum);
40378 +
40379 +#ifdef DEBUG_SOF
40380 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
40381 + hfnum.b.frnum);
40382 +#endif
40383 + return hfnum.b.frnum;
40384 +}
40385 +
40386 +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
40387 + struct dwc_otg_hcd_function_ops *fops)
40388 +{
40389 + int retval = 0;
40390 +
40391 + hcd->fops = fops;
40392 + if (!dwc_otg_is_device_mode(hcd->core_if) &&
40393 + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
40394 + dwc_otg_hcd_reinit(hcd);
40395 + } else {
40396 + retval = -DWC_E_NO_DEVICE;
40397 + }
40398 +
40399 + return retval;
40400 +}
40401 +
40402 +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
40403 +{
40404 + return hcd->priv;
40405 +}
40406 +
40407 +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
40408 +{
40409 + hcd->priv = priv_data;
40410 +}
40411 +
40412 +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
40413 +{
40414 + return hcd->otg_port;
40415 +}
40416 +
40417 +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
40418 +{
40419 + uint32_t is_b_host;
40420 + if (hcd->core_if->op_state == B_HOST) {
40421 + is_b_host = 1;
40422 + } else {
40423 + is_b_host = 0;
40424 + }
40425 +
40426 + return is_b_host;
40427 +}
40428 +
40429 +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
40430 + int iso_desc_count, int atomic_alloc)
40431 +{
40432 + dwc_otg_hcd_urb_t *dwc_otg_urb;
40433 + uint32_t size;
40434 +
40435 + size =
40436 + sizeof(*dwc_otg_urb) +
40437 + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
40438 + if (atomic_alloc)
40439 + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
40440 + else
40441 + dwc_otg_urb = DWC_ALLOC(size);
40442 +
40443 + if (dwc_otg_urb)
40444 + dwc_otg_urb->packet_count = iso_desc_count;
40445 + else {
40446 + DWC_ERROR("**** DWC OTG HCD URB alloc - "
40447 + "%salloc of %db failed\n",
40448 + atomic_alloc?"atomic ":"", size);
40449 + }
40450 + return dwc_otg_urb;
40451 +}
40452 +
40453 +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
40454 + uint8_t dev_addr, uint8_t ep_num,
40455 + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
40456 +{
40457 + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
40458 + ep_type, ep_dir, mps);
40459 +#if 0
40460 + DWC_PRINTF
40461 + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
40462 + dev_addr, ep_num, ep_dir, ep_type, mps);
40463 +#endif
40464 +}
40465 +
40466 +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
40467 + void *urb_handle, void *buf, dwc_dma_t dma,
40468 + uint32_t buflen, void *setup_packet,
40469 + dwc_dma_t setup_dma, uint32_t flags,
40470 + uint16_t interval)
40471 +{
40472 + dwc_otg_urb->priv = urb_handle;
40473 + dwc_otg_urb->buf = buf;
40474 + dwc_otg_urb->dma = dma;
40475 + dwc_otg_urb->length = buflen;
40476 + dwc_otg_urb->setup_packet = setup_packet;
40477 + dwc_otg_urb->setup_dma = setup_dma;
40478 + dwc_otg_urb->flags = flags;
40479 + dwc_otg_urb->interval = interval;
40480 + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
40481 +}
40482 +
40483 +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
40484 +{
40485 + return dwc_otg_urb->status;
40486 +}
40487 +
40488 +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
40489 +{
40490 + return dwc_otg_urb->actual_length;
40491 +}
40492 +
40493 +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
40494 +{
40495 + return dwc_otg_urb->error_count;
40496 +}
40497 +
40498 +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
40499 + int desc_num, uint32_t offset,
40500 + uint32_t length)
40501 +{
40502 + dwc_otg_urb->iso_descs[desc_num].offset = offset;
40503 + dwc_otg_urb->iso_descs[desc_num].length = length;
40504 +}
40505 +
40506 +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
40507 + int desc_num)
40508 +{
40509 + return dwc_otg_urb->iso_descs[desc_num].status;
40510 +}
40511 +
40512 +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
40513 + dwc_otg_urb, int desc_num)
40514 +{
40515 + return dwc_otg_urb->iso_descs[desc_num].actual_length;
40516 +}
40517 +
40518 +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
40519 +{
40520 + int allocated = 0;
40521 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40522 +
40523 + if (qh) {
40524 + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
40525 + allocated = 1;
40526 + }
40527 + }
40528 + return allocated;
40529 +}
40530 +
40531 +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
40532 +{
40533 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40534 + int freed = 0;
40535 + DWC_ASSERT(qh, "qh is not allocated\n");
40536 +
40537 + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
40538 + freed = 1;
40539 + }
40540 +
40541 + return freed;
40542 +}
40543 +
40544 +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
40545 +{
40546 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40547 + DWC_ASSERT(qh, "qh is not allocated\n");
40548 + return qh->usecs;
40549 +}
40550 +
40551 +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
40552 +{
40553 +#ifdef DEBUG
40554 + int num_channels;
40555 + int i;
40556 + gnptxsts_data_t np_tx_status;
40557 + hptxsts_data_t p_tx_status;
40558 +
40559 + num_channels = hcd->core_if->core_params->host_channels;
40560 + DWC_PRINTF("\n");
40561 + DWC_PRINTF
40562 + ("************************************************************\n");
40563 + DWC_PRINTF("HCD State:\n");
40564 + DWC_PRINTF(" Num channels: %d\n", num_channels);
40565 + for (i = 0; i < num_channels; i++) {
40566 + dwc_hc_t *hc = hcd->hc_ptr_array[i];
40567 + DWC_PRINTF(" Channel %d:\n", i);
40568 + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
40569 + hc->dev_addr, hc->ep_num, hc->ep_is_in);
40570 + DWC_PRINTF(" speed: %d\n", hc->speed);
40571 + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
40572 + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
40573 + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
40574 + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
40575 + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
40576 + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
40577 + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
40578 + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
40579 + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
40580 + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
40581 + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
40582 + DWC_PRINTF(" do_split: %d\n", hc->do_split);
40583 + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
40584 + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
40585 + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
40586 + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
40587 + DWC_PRINTF(" requests: %d\n", hc->requests);
40588 + DWC_PRINTF(" qh: %p\n", hc->qh);
40589 + if (hc->xfer_started) {
40590 + hfnum_data_t hfnum;
40591 + hcchar_data_t hcchar;
40592 + hctsiz_data_t hctsiz;
40593 + hcint_data_t hcint;
40594 + hcintmsk_data_t hcintmsk;
40595 + hfnum.d32 =
40596 + DWC_READ_REG32(&hcd->core_if->
40597 + host_if->host_global_regs->hfnum);
40598 + hcchar.d32 =
40599 + DWC_READ_REG32(&hcd->core_if->host_if->
40600 + hc_regs[i]->hcchar);
40601 + hctsiz.d32 =
40602 + DWC_READ_REG32(&hcd->core_if->host_if->
40603 + hc_regs[i]->hctsiz);
40604 + hcint.d32 =
40605 + DWC_READ_REG32(&hcd->core_if->host_if->
40606 + hc_regs[i]->hcint);
40607 + hcintmsk.d32 =
40608 + DWC_READ_REG32(&hcd->core_if->host_if->
40609 + hc_regs[i]->hcintmsk);
40610 + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
40611 + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
40612 + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
40613 + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
40614 + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
40615 + }
40616 + if (hc->xfer_started && hc->qh) {
40617 + dwc_otg_qtd_t *qtd;
40618 + dwc_otg_hcd_urb_t *urb;
40619 +
40620 + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
40621 + if (!qtd->in_process)
40622 + break;
40623 +
40624 + urb = qtd->urb;
40625 + DWC_PRINTF(" URB Info:\n");
40626 + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
40627 + if (urb) {
40628 + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
40629 + dwc_otg_hcd_get_dev_addr(&urb->
40630 + pipe_info),
40631 + dwc_otg_hcd_get_ep_num(&urb->
40632 + pipe_info),
40633 + dwc_otg_hcd_is_pipe_in(&urb->
40634 + pipe_info) ?
40635 + "IN" : "OUT");
40636 + DWC_PRINTF(" Max packet size: %d\n",
40637 + dwc_otg_hcd_get_mps(&urb->
40638 + pipe_info));
40639 + DWC_PRINTF(" transfer_buffer: %p\n",
40640 + urb->buf);
40641 + DWC_PRINTF(" transfer_dma: %p\n",
40642 + (void *)urb->dma);
40643 + DWC_PRINTF(" transfer_buffer_length: %d\n",
40644 + urb->length);
40645 + DWC_PRINTF(" actual_length: %d\n",
40646 + urb->actual_length);
40647 + }
40648 + }
40649 + }
40650 + }
40651 + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
40652 + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
40653 + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
40654 + np_tx_status.d32 =
40655 + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
40656 + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
40657 + np_tx_status.b.nptxqspcavail);
40658 + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
40659 + np_tx_status.b.nptxfspcavail);
40660 + p_tx_status.d32 =
40661 + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
40662 + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
40663 + p_tx_status.b.ptxqspcavail);
40664 + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
40665 + dwc_otg_hcd_dump_frrem(hcd);
40666 + dwc_otg_dump_global_registers(hcd->core_if);
40667 + dwc_otg_dump_host_registers(hcd->core_if);
40668 + DWC_PRINTF
40669 + ("************************************************************\n");
40670 + DWC_PRINTF("\n");
40671 +#endif
40672 +}
40673 +
40674 +#ifdef DEBUG
40675 +void dwc_print_setup_data(uint8_t * setup)
40676 +{
40677 + int i;
40678 + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
40679 + DWC_PRINTF("Setup Data = MSB ");
40680 + for (i = 7; i >= 0; i--)
40681 + DWC_PRINTF("%02x ", setup[i]);
40682 + DWC_PRINTF("\n");
40683 + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
40684 + (setup[0] & 0x80) ? "Device-to-Host" :
40685 + "Host-to-Device");
40686 + DWC_PRINTF(" bmRequestType Type = ");
40687 + switch ((setup[0] & 0x60) >> 5) {
40688 + case 0:
40689 + DWC_PRINTF("Standard\n");
40690 + break;
40691 + case 1:
40692 + DWC_PRINTF("Class\n");
40693 + break;
40694 + case 2:
40695 + DWC_PRINTF("Vendor\n");
40696 + break;
40697 + case 3:
40698 + DWC_PRINTF("Reserved\n");
40699 + break;
40700 + }
40701 + DWC_PRINTF(" bmRequestType Recipient = ");
40702 + switch (setup[0] & 0x1f) {
40703 + case 0:
40704 + DWC_PRINTF("Device\n");
40705 + break;
40706 + case 1:
40707 + DWC_PRINTF("Interface\n");
40708 + break;
40709 + case 2:
40710 + DWC_PRINTF("Endpoint\n");
40711 + break;
40712 + case 3:
40713 + DWC_PRINTF("Other\n");
40714 + break;
40715 + default:
40716 + DWC_PRINTF("Reserved\n");
40717 + break;
40718 + }
40719 + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
40720 + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
40721 + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
40722 + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
40723 + }
40724 +}
40725 +#endif
40726 +
40727 +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
40728 +{
40729 +#if 0
40730 + DWC_PRINTF("Frame remaining at SOF:\n");
40731 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40732 + hcd->frrem_samples, hcd->frrem_accum,
40733 + (hcd->frrem_samples > 0) ?
40734 + hcd->frrem_accum / hcd->frrem_samples : 0);
40735 +
40736 + DWC_PRINTF("\n");
40737 + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
40738 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40739 + hcd->core_if->hfnum_7_samples,
40740 + hcd->core_if->hfnum_7_frrem_accum,
40741 + (hcd->core_if->hfnum_7_samples >
40742 + 0) ? hcd->core_if->hfnum_7_frrem_accum /
40743 + hcd->core_if->hfnum_7_samples : 0);
40744 + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
40745 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40746 + hcd->core_if->hfnum_0_samples,
40747 + hcd->core_if->hfnum_0_frrem_accum,
40748 + (hcd->core_if->hfnum_0_samples >
40749 + 0) ? hcd->core_if->hfnum_0_frrem_accum /
40750 + hcd->core_if->hfnum_0_samples : 0);
40751 + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
40752 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40753 + hcd->core_if->hfnum_other_samples,
40754 + hcd->core_if->hfnum_other_frrem_accum,
40755 + (hcd->core_if->hfnum_other_samples >
40756 + 0) ? hcd->core_if->hfnum_other_frrem_accum /
40757 + hcd->core_if->hfnum_other_samples : 0);
40758 +
40759 + DWC_PRINTF("\n");
40760 + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
40761 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40762 + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
40763 + (hcd->hfnum_7_samples_a > 0) ?
40764 + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
40765 + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
40766 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40767 + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
40768 + (hcd->hfnum_0_samples_a > 0) ?
40769 + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
40770 + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
40771 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40772 + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
40773 + (hcd->hfnum_other_samples_a > 0) ?
40774 + hcd->hfnum_other_frrem_accum_a /
40775 + hcd->hfnum_other_samples_a : 0);
40776 +
40777 + DWC_PRINTF("\n");
40778 + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
40779 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40780 + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
40781 + (hcd->hfnum_7_samples_b > 0) ?
40782 + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
40783 + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
40784 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40785 + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
40786 + (hcd->hfnum_0_samples_b > 0) ?
40787 + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
40788 + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
40789 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40790 + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
40791 + (hcd->hfnum_other_samples_b > 0) ?
40792 + hcd->hfnum_other_frrem_accum_b /
40793 + hcd->hfnum_other_samples_b : 0);
40794 +#endif
40795 +}
40796 +
40797 +#endif /* DWC_DEVICE_ONLY */
40798 --- /dev/null
40799 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
40800 @@ -0,0 +1,870 @@
40801 +/* ==========================================================================
40802 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
40803 + * $Revision: #58 $
40804 + * $Date: 2011/09/15 $
40805 + * $Change: 1846647 $
40806 + *
40807 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
40808 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
40809 + * otherwise expressly agreed to in writing between Synopsys and you.
40810 + *
40811 + * The Software IS NOT an item of Licensed Software or Licensed Product under
40812 + * any End User Software License Agreement or Agreement for Licensed Product
40813 + * with Synopsys or any supplement thereto. You are permitted to use and
40814 + * redistribute this Software in source and binary forms, with or without
40815 + * modification, provided that redistributions of source code must retain this
40816 + * notice. You may not view, use, disclose, copy or distribute this file or
40817 + * any information contained herein except pursuant to this license grant from
40818 + * Synopsys. If you do not agree with this notice, including the disclaimer
40819 + * below, then you are not authorized to use the Software.
40820 + *
40821 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
40822 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40823 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40824 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
40825 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40826 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40827 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40828 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40829 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40830 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40831 + * DAMAGE.
40832 + * ========================================================================== */
40833 +#ifndef DWC_DEVICE_ONLY
40834 +#ifndef __DWC_HCD_H__
40835 +#define __DWC_HCD_H__
40836 +
40837 +#include "dwc_otg_os_dep.h"
40838 +#include "usb.h"
40839 +#include "dwc_otg_hcd_if.h"
40840 +#include "dwc_otg_core_if.h"
40841 +#include "dwc_list.h"
40842 +#include "dwc_otg_cil.h"
40843 +#include "dwc_otg_fiq_fsm.h"
40844 +#include "dwc_otg_driver.h"
40845 +
40846 +
40847 +/**
40848 + * @file
40849 + *
40850 + * This file contains the structures, constants, and interfaces for
40851 + * the Host Contoller Driver (HCD).
40852 + *
40853 + * The Host Controller Driver (HCD) is responsible for translating requests
40854 + * from the USB Driver into the appropriate actions on the DWC_otg controller.
40855 + * It isolates the USBD from the specifics of the controller by providing an
40856 + * API to the USBD.
40857 + */
40858 +
40859 +struct dwc_otg_hcd_pipe_info {
40860 + uint8_t dev_addr;
40861 + uint8_t ep_num;
40862 + uint8_t pipe_type;
40863 + uint8_t pipe_dir;
40864 + uint16_t mps;
40865 +};
40866 +
40867 +struct dwc_otg_hcd_iso_packet_desc {
40868 + uint32_t offset;
40869 + uint32_t length;
40870 + uint32_t actual_length;
40871 + uint32_t status;
40872 +};
40873 +
40874 +struct dwc_otg_qtd;
40875 +
40876 +struct dwc_otg_hcd_urb {
40877 + void *priv;
40878 + struct dwc_otg_qtd *qtd;
40879 + void *buf;
40880 + dwc_dma_t dma;
40881 + void *setup_packet;
40882 + dwc_dma_t setup_dma;
40883 + uint32_t length;
40884 + uint32_t actual_length;
40885 + uint32_t status;
40886 + uint32_t error_count;
40887 + uint32_t packet_count;
40888 + uint32_t flags;
40889 + uint16_t interval;
40890 + struct dwc_otg_hcd_pipe_info pipe_info;
40891 + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
40892 +};
40893 +
40894 +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
40895 +{
40896 + return pipe->ep_num;
40897 +}
40898 +
40899 +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
40900 + *pipe)
40901 +{
40902 + return pipe->pipe_type;
40903 +}
40904 +
40905 +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
40906 +{
40907 + return pipe->mps;
40908 +}
40909 +
40910 +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
40911 + *pipe)
40912 +{
40913 + return pipe->dev_addr;
40914 +}
40915 +
40916 +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
40917 + *pipe)
40918 +{
40919 + return (pipe->pipe_type == UE_ISOCHRONOUS);
40920 +}
40921 +
40922 +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
40923 + *pipe)
40924 +{
40925 + return (pipe->pipe_type == UE_INTERRUPT);
40926 +}
40927 +
40928 +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
40929 + *pipe)
40930 +{
40931 + return (pipe->pipe_type == UE_BULK);
40932 +}
40933 +
40934 +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
40935 + *pipe)
40936 +{
40937 + return (pipe->pipe_type == UE_CONTROL);
40938 +}
40939 +
40940 +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
40941 +{
40942 + return (pipe->pipe_dir == UE_DIR_IN);
40943 +}
40944 +
40945 +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
40946 + *pipe)
40947 +{
40948 + return (!dwc_otg_hcd_is_pipe_in(pipe));
40949 +}
40950 +
40951 +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
40952 + uint8_t devaddr, uint8_t ep_num,
40953 + uint8_t pipe_type, uint8_t pipe_dir,
40954 + uint16_t mps)
40955 +{
40956 + pipe->dev_addr = devaddr;
40957 + pipe->ep_num = ep_num;
40958 + pipe->pipe_type = pipe_type;
40959 + pipe->pipe_dir = pipe_dir;
40960 + pipe->mps = mps;
40961 +}
40962 +
40963 +/**
40964 + * Phases for control transfers.
40965 + */
40966 +typedef enum dwc_otg_control_phase {
40967 + DWC_OTG_CONTROL_SETUP,
40968 + DWC_OTG_CONTROL_DATA,
40969 + DWC_OTG_CONTROL_STATUS
40970 +} dwc_otg_control_phase_e;
40971 +
40972 +/** Transaction types. */
40973 +typedef enum dwc_otg_transaction_type {
40974 + DWC_OTG_TRANSACTION_NONE = 0,
40975 + DWC_OTG_TRANSACTION_PERIODIC = 1,
40976 + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
40977 + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
40978 +} dwc_otg_transaction_type_e;
40979 +
40980 +struct dwc_otg_qh;
40981 +
40982 +/**
40983 + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
40984 + * interrupt, or isochronous transfer. A single QTD is created for each URB
40985 + * (of one of these types) submitted to the HCD. The transfer associated with
40986 + * a QTD may require one or multiple transactions.
40987 + *
40988 + * A QTD is linked to a Queue Head, which is entered in either the
40989 + * non-periodic or periodic schedule for execution. When a QTD is chosen for
40990 + * execution, some or all of its transactions may be executed. After
40991 + * execution, the state of the QTD is updated. The QTD may be retired if all
40992 + * its transactions are complete or if an error occurred. Otherwise, it
40993 + * remains in the schedule so more transactions can be executed later.
40994 + */
40995 +typedef struct dwc_otg_qtd {
40996 + /**
40997 + * Determines the PID of the next data packet for the data phase of
40998 + * control transfers. Ignored for other transfer types.<br>
40999 + * One of the following values:
41000 + * - DWC_OTG_HC_PID_DATA0
41001 + * - DWC_OTG_HC_PID_DATA1
41002 + */
41003 + uint8_t data_toggle;
41004 +
41005 + /** Current phase for control transfers (Setup, Data, or Status). */
41006 + dwc_otg_control_phase_e control_phase;
41007 +
41008 + /** Keep track of the current split type
41009 + * for FS/LS endpoints on a HS Hub */
41010 + uint8_t complete_split;
41011 +
41012 + /** How many bytes transferred during SSPLIT OUT */
41013 + uint32_t ssplit_out_xfer_count;
41014 +
41015 + /**
41016 + * Holds the number of bus errors that have occurred for a transaction
41017 + * within this transfer.
41018 + */
41019 + uint8_t error_count;
41020 +
41021 + /**
41022 + * Index of the next frame descriptor for an isochronous transfer. A
41023 + * frame descriptor describes the buffer position and length of the
41024 + * data to be transferred in the next scheduled (micro)frame of an
41025 + * isochronous transfer. It also holds status for that transaction.
41026 + * The frame index starts at 0.
41027 + */
41028 + uint16_t isoc_frame_index;
41029 +
41030 + /** Position of the ISOC split on full/low speed */
41031 + uint8_t isoc_split_pos;
41032 +
41033 + /** Position of the ISOC split in the buffer for the current frame */
41034 + uint16_t isoc_split_offset;
41035 +
41036 + /** URB for this transfer */
41037 + struct dwc_otg_hcd_urb *urb;
41038 +
41039 + struct dwc_otg_qh *qh;
41040 +
41041 + /** This list of QTDs */
41042 + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
41043 +
41044 + /** Indicates if this QTD is currently processed by HW. */
41045 + uint8_t in_process;
41046 +
41047 + /** Number of DMA descriptors for this QTD */
41048 + uint8_t n_desc;
41049 +
41050 + /**
41051 + * Last activated frame(packet) index.
41052 + * Used in Descriptor DMA mode only.
41053 + */
41054 + uint16_t isoc_frame_index_last;
41055 +
41056 +} dwc_otg_qtd_t;
41057 +
41058 +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
41059 +
41060 +/**
41061 + * A Queue Head (QH) holds the static characteristics of an endpoint and
41062 + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
41063 + * be entered in either the non-periodic or periodic schedule.
41064 + */
41065 +typedef struct dwc_otg_qh {
41066 + /**
41067 + * Endpoint type.
41068 + * One of the following values:
41069 + * - UE_CONTROL
41070 + * - UE_BULK
41071 + * - UE_INTERRUPT
41072 + * - UE_ISOCHRONOUS
41073 + */
41074 + uint8_t ep_type;
41075 + uint8_t ep_is_in;
41076 +
41077 + /** wMaxPacketSize Field of Endpoint Descriptor. */
41078 + uint16_t maxp;
41079 +
41080 + /**
41081 + * Device speed.
41082 + * One of the following values:
41083 + * - DWC_OTG_EP_SPEED_LOW
41084 + * - DWC_OTG_EP_SPEED_FULL
41085 + * - DWC_OTG_EP_SPEED_HIGH
41086 + */
41087 + uint8_t dev_speed;
41088 +
41089 + /**
41090 + * Determines the PID of the next data packet for non-control
41091 + * transfers. Ignored for control transfers.<br>
41092 + * One of the following values:
41093 + * - DWC_OTG_HC_PID_DATA0
41094 + * - DWC_OTG_HC_PID_DATA1
41095 + */
41096 + uint8_t data_toggle;
41097 +
41098 + /** Ping state if 1. */
41099 + uint8_t ping_state;
41100 +
41101 + /**
41102 + * List of QTDs for this QH.
41103 + */
41104 + struct dwc_otg_qtd_list qtd_list;
41105 +
41106 + /** Host channel currently processing transfers for this QH. */
41107 + struct dwc_hc *channel;
41108 +
41109 + /** Full/low speed endpoint on high-speed hub requires split. */
41110 + uint8_t do_split;
41111 +
41112 + /** @name Periodic schedule information */
41113 + /** @{ */
41114 +
41115 + /** Bandwidth in microseconds per (micro)frame. */
41116 + uint16_t usecs;
41117 +
41118 + /** Interval between transfers in (micro)frames. */
41119 + uint16_t interval;
41120 +
41121 + /**
41122 + * (micro)frame to initialize a periodic transfer. The transfer
41123 + * executes in the following (micro)frame.
41124 + */
41125 + uint16_t sched_frame;
41126 +
41127 + /*
41128 + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
41129 + */
41130 + uint16_t nak_frame;
41131 +
41132 + /** (micro)frame at which last start split was initialized. */
41133 + uint16_t start_split_frame;
41134 +
41135 + /** @} */
41136 +
41137 + /**
41138 + * Used instead of original buffer if
41139 + * it(physical address) is not dword-aligned.
41140 + */
41141 + uint8_t *dw_align_buf;
41142 + dwc_dma_t dw_align_buf_dma;
41143 +
41144 + /** Entry for QH in either the periodic or non-periodic schedule. */
41145 + dwc_list_link_t qh_list_entry;
41146 +
41147 + /** @name Descriptor DMA support */
41148 + /** @{ */
41149 +
41150 + /** Descriptor List. */
41151 + dwc_otg_host_dma_desc_t *desc_list;
41152 +
41153 + /** Descriptor List physical address. */
41154 + dwc_dma_t desc_list_dma;
41155 +
41156 + /**
41157 + * Xfer Bytes array.
41158 + * Each element corresponds to a descriptor and indicates
41159 + * original XferSize size value for the descriptor.
41160 + */
41161 + uint32_t *n_bytes;
41162 +
41163 + /** Actual number of transfer descriptors in a list. */
41164 + uint16_t ntd;
41165 +
41166 + /** First activated isochronous transfer descriptor index. */
41167 + uint8_t td_first;
41168 + /** Last activated isochronous transfer descriptor index. */
41169 + uint8_t td_last;
41170 +
41171 + /** @} */
41172 +
41173 +
41174 + uint16_t speed;
41175 + uint16_t frame_usecs[8];
41176 +
41177 + uint32_t skip_count;
41178 +} dwc_otg_qh_t;
41179 +
41180 +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
41181 +
41182 +typedef struct urb_tq_entry {
41183 + struct urb *urb;
41184 + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
41185 +} urb_tq_entry_t;
41186 +
41187 +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
41188 +
41189 +/**
41190 + * This structure holds the state of the HCD, including the non-periodic and
41191 + * periodic schedules.
41192 + */
41193 +struct dwc_otg_hcd {
41194 + /** The DWC otg device pointer */
41195 + struct dwc_otg_device *otg_dev;
41196 + /** DWC OTG Core Interface Layer */
41197 + dwc_otg_core_if_t *core_if;
41198 +
41199 + /** Function HCD driver callbacks */
41200 + struct dwc_otg_hcd_function_ops *fops;
41201 +
41202 + /** Internal DWC HCD Flags */
41203 + volatile union dwc_otg_hcd_internal_flags {
41204 + uint32_t d32;
41205 + struct {
41206 + unsigned port_connect_status_change:1;
41207 + unsigned port_connect_status:1;
41208 + unsigned port_reset_change:1;
41209 + unsigned port_enable_change:1;
41210 + unsigned port_suspend_change:1;
41211 + unsigned port_over_current_change:1;
41212 + unsigned port_l1_change:1;
41213 + unsigned port_speed:2;
41214 + unsigned reserved:24;
41215 + } b;
41216 + } flags;
41217 +
41218 + /**
41219 + * Inactive items in the non-periodic schedule. This is a list of
41220 + * Queue Heads. Transfers associated with these Queue Heads are not
41221 + * currently assigned to a host channel.
41222 + */
41223 + dwc_list_link_t non_periodic_sched_inactive;
41224 +
41225 + /**
41226 + * Active items in the non-periodic schedule. This is a list of
41227 + * Queue Heads. Transfers associated with these Queue Heads are
41228 + * currently assigned to a host channel.
41229 + */
41230 + dwc_list_link_t non_periodic_sched_active;
41231 +
41232 + /**
41233 + * Pointer to the next Queue Head to process in the active
41234 + * non-periodic schedule.
41235 + */
41236 + dwc_list_link_t *non_periodic_qh_ptr;
41237 +
41238 + /**
41239 + * Inactive items in the periodic schedule. This is a list of QHs for
41240 + * periodic transfers that are _not_ scheduled for the next frame.
41241 + * Each QH in the list has an interval counter that determines when it
41242 + * needs to be scheduled for execution. This scheduling mechanism
41243 + * allows only a simple calculation for periodic bandwidth used (i.e.
41244 + * must assume that all periodic transfers may need to execute in the
41245 + * same frame). However, it greatly simplifies scheduling and should
41246 + * be sufficient for the vast majority of OTG hosts, which need to
41247 + * connect to a small number of peripherals at one time.
41248 + *
41249 + * Items move from this list to periodic_sched_ready when the QH
41250 + * interval counter is 0 at SOF.
41251 + */
41252 + dwc_list_link_t periodic_sched_inactive;
41253 +
41254 + /**
41255 + * List of periodic QHs that are ready for execution in the next
41256 + * frame, but have not yet been assigned to host channels.
41257 + *
41258 + * Items move from this list to periodic_sched_assigned as host
41259 + * channels become available during the current frame.
41260 + */
41261 + dwc_list_link_t periodic_sched_ready;
41262 +
41263 + /**
41264 + * List of periodic QHs to be executed in the next frame that are
41265 + * assigned to host channels.
41266 + *
41267 + * Items move from this list to periodic_sched_queued as the
41268 + * transactions for the QH are queued to the DWC_otg controller.
41269 + */
41270 + dwc_list_link_t periodic_sched_assigned;
41271 +
41272 + /**
41273 + * List of periodic QHs that have been queued for execution.
41274 + *
41275 + * Items move from this list to either periodic_sched_inactive or
41276 + * periodic_sched_ready when the channel associated with the transfer
41277 + * is released. If the interval for the QH is 1, the item moves to
41278 + * periodic_sched_ready because it must be rescheduled for the next
41279 + * frame. Otherwise, the item moves to periodic_sched_inactive.
41280 + */
41281 + dwc_list_link_t periodic_sched_queued;
41282 +
41283 + /**
41284 + * Total bandwidth claimed so far for periodic transfers. This value
41285 + * is in microseconds per (micro)frame. The assumption is that all
41286 + * periodic transfers may occur in the same (micro)frame.
41287 + */
41288 + uint16_t periodic_usecs;
41289 +
41290 + /**
41291 + * Total bandwidth claimed so far for all periodic transfers
41292 + * in a frame.
41293 + * This will include a mixture of HS and FS transfers.
41294 + * Units are microseconds per (micro)frame.
41295 + * We have a budget per frame and have to schedule
41296 + * transactions accordingly.
41297 + * Watch out for the fact that things are actually scheduled for the
41298 + * "next frame".
41299 + */
41300 + uint16_t frame_usecs[8];
41301 +
41302 +
41303 + /**
41304 + * Frame number read from the core at SOF. The value ranges from 0 to
41305 + * DWC_HFNUM_MAX_FRNUM.
41306 + */
41307 + uint16_t frame_number;
41308 +
41309 + /**
41310 + * Count of periodic QHs, if using several eps. For SOF enable/disable.
41311 + */
41312 + uint16_t periodic_qh_count;
41313 +
41314 + /**
41315 + * Free host channels in the controller. This is a list of
41316 + * dwc_hc_t items.
41317 + */
41318 + struct hc_list free_hc_list;
41319 + /**
41320 + * Number of host channels assigned to periodic transfers. Currently
41321 + * assuming that there is a dedicated host channel for each periodic
41322 + * transaction and at least one host channel available for
41323 + * non-periodic transactions.
41324 + */
41325 + int periodic_channels; /* microframe_schedule==0 */
41326 +
41327 + /**
41328 + * Number of host channels assigned to non-periodic transfers.
41329 + */
41330 + int non_periodic_channels; /* microframe_schedule==0 */
41331 +
41332 + /**
41333 + * Number of host channels assigned to non-periodic transfers.
41334 + */
41335 + int available_host_channels;
41336 +
41337 + /**
41338 + * Array of pointers to the host channel descriptors. Allows accessing
41339 + * a host channel descriptor given the host channel number. This is
41340 + * useful in interrupt handlers.
41341 + */
41342 + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
41343 +
41344 + /**
41345 + * Buffer to use for any data received during the status phase of a
41346 + * control transfer. Normally no data is transferred during the status
41347 + * phase. This buffer is used as a bit bucket.
41348 + */
41349 + uint8_t *status_buf;
41350 +
41351 + /**
41352 + * DMA address for status_buf.
41353 + */
41354 + dma_addr_t status_buf_dma;
41355 +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
41356 +
41357 + /**
41358 + * Connection timer. An OTG host must display a message if the device
41359 + * does not connect. Started when the VBus power is turned on via
41360 + * sysfs attribute "buspower".
41361 + */
41362 + dwc_timer_t *conn_timer;
41363 +
41364 + /* Tasket to do a reset */
41365 + dwc_tasklet_t *reset_tasklet;
41366 +
41367 + dwc_tasklet_t *completion_tasklet;
41368 + struct urb_list completed_urb_list;
41369 +
41370 + /* */
41371 + dwc_spinlock_t *lock;
41372 + /**
41373 + * Private data that could be used by OS wrapper.
41374 + */
41375 + void *priv;
41376 +
41377 + uint8_t otg_port;
41378 +
41379 + /** Frame List */
41380 + uint32_t *frame_list;
41381 +
41382 + /** Hub - Port assignment */
41383 + int hub_port[128];
41384 +#ifdef FIQ_DEBUG
41385 + int hub_port_alloc[2048];
41386 +#endif
41387 +
41388 + /** Frame List DMA address */
41389 + dma_addr_t frame_list_dma;
41390 +
41391 + struct fiq_stack *fiq_stack;
41392 + struct fiq_state *fiq_state;
41393 +
41394 + /** Virtual address for split transaction DMA bounce buffers */
41395 + struct fiq_dma_blob *fiq_dmab;
41396 +
41397 +#ifdef DEBUG
41398 + uint32_t frrem_samples;
41399 + uint64_t frrem_accum;
41400 +
41401 + uint32_t hfnum_7_samples_a;
41402 + uint64_t hfnum_7_frrem_accum_a;
41403 + uint32_t hfnum_0_samples_a;
41404 + uint64_t hfnum_0_frrem_accum_a;
41405 + uint32_t hfnum_other_samples_a;
41406 + uint64_t hfnum_other_frrem_accum_a;
41407 +
41408 + uint32_t hfnum_7_samples_b;
41409 + uint64_t hfnum_7_frrem_accum_b;
41410 + uint32_t hfnum_0_samples_b;
41411 + uint64_t hfnum_0_frrem_accum_b;
41412 + uint32_t hfnum_other_samples_b;
41413 + uint64_t hfnum_other_frrem_accum_b;
41414 +#endif
41415 +};
41416 +
41417 +static inline struct device *dwc_otg_hcd_to_dev(struct dwc_otg_hcd *hcd)
41418 +{
41419 + return &hcd->otg_dev->os_dep.platformdev->dev;
41420 +}
41421 +
41422 +/** @name Transaction Execution Functions */
41423 +/** @{ */
41424 +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
41425 + * hcd);
41426 +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
41427 + dwc_otg_transaction_type_e tr_type);
41428 +
41429 +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
41430 +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
41431 +
41432 +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
41433 +extern int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
41434 +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
41435 +
41436 +/** @} */
41437 +
41438 +/** @name Interrupt Handler Functions */
41439 +/** @{ */
41440 +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41441 +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41442 +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
41443 + dwc_otg_hcd);
41444 +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
41445 + dwc_otg_hcd);
41446 +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
41447 + dwc_otg_hcd);
41448 +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
41449 + dwc_otg_hcd);
41450 +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41451 +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
41452 + dwc_otg_hcd);
41453 +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41454 +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41455 +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
41456 + uint32_t num);
41457 +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41458 +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
41459 + dwc_otg_hcd);
41460 +/** @} */
41461 +
41462 +/** @name Schedule Queue Functions */
41463 +/** @{ */
41464 +
41465 +/* Implemented in dwc_otg_hcd_queue.c */
41466 +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
41467 + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
41468 +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41469 +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41470 +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41471 +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41472 + int sched_csplit);
41473 +
41474 +/** Remove and free a QH */
41475 +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
41476 + dwc_otg_qh_t * qh)
41477 +{
41478 + dwc_irqflags_t flags;
41479 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
41480 + dwc_otg_hcd_qh_remove(hcd, qh);
41481 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
41482 + dwc_otg_hcd_qh_free(hcd, qh);
41483 +}
41484 +
41485 +/** Allocates memory for a QH structure.
41486 + * @return Returns the memory allocate or NULL on error. */
41487 +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
41488 +{
41489 + if (atomic_alloc)
41490 + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
41491 + else
41492 + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
41493 +}
41494 +
41495 +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
41496 + int atomic_alloc);
41497 +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
41498 +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
41499 + dwc_otg_qh_t ** qh, int atomic_alloc);
41500 +
41501 +/** Allocates memory for a QTD structure.
41502 + * @return Returns the memory allocate or NULL on error. */
41503 +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
41504 +{
41505 + if (atomic_alloc)
41506 + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
41507 + else
41508 + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
41509 +}
41510 +
41511 +/** Frees the memory for a QTD structure. QTD should already be removed from
41512 + * list.
41513 + * @param qtd QTD to free.*/
41514 +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
41515 +{
41516 + DWC_FREE(qtd);
41517 +}
41518 +
41519 +/** Removes a QTD from list.
41520 + * @param hcd HCD instance.
41521 + * @param qtd QTD to remove from list.
41522 + * @param qh QTD belongs to.
41523 + */
41524 +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
41525 + dwc_otg_qtd_t * qtd,
41526 + dwc_otg_qh_t * qh)
41527 +{
41528 + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
41529 +}
41530 +
41531 +/** Remove and free a QTD
41532 + * Need to disable IRQ and hold hcd lock while calling this function out of
41533 + * interrupt servicing chain */
41534 +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
41535 + dwc_otg_qtd_t * qtd,
41536 + dwc_otg_qh_t * qh)
41537 +{
41538 + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
41539 + dwc_otg_hcd_qtd_free(qtd);
41540 +}
41541 +
41542 +/** @} */
41543 +
41544 +/** @name Descriptor DMA Supporting Functions */
41545 +/** @{ */
41546 +
41547 +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41548 +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
41549 + dwc_hc_t * hc,
41550 + dwc_otg_hc_regs_t * hc_regs,
41551 + dwc_otg_halt_status_e halt_status);
41552 +
41553 +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41554 +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41555 +
41556 +/** @} */
41557 +
41558 +/** @name Internal Functions */
41559 +/** @{ */
41560 +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
41561 +/** @} */
41562 +
41563 +#ifdef CONFIG_USB_DWC_OTG_LPM
41564 +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
41565 + uint8_t devaddr);
41566 +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
41567 +#endif
41568 +
41569 +/** Gets the QH that contains the list_head */
41570 +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
41571 +
41572 +/** Gets the QTD that contains the list_head */
41573 +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
41574 +
41575 +/** Check if QH is non-periodic */
41576 +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
41577 + (_qh_ptr_->ep_type == UE_CONTROL))
41578 +
41579 +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
41580 +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
41581 +
41582 +/** Packet size for any kind of endpoint descriptor */
41583 +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
41584 +
41585 +/**
41586 + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
41587 + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
41588 + * frame number when the max frame number is reached.
41589 + */
41590 +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
41591 +{
41592 + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
41593 + (DWC_HFNUM_MAX_FRNUM >> 1);
41594 +}
41595 +
41596 +/**
41597 + * Returns true if _frame1 is greater than _frame2. The comparison is done
41598 + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
41599 + * number when the max frame number is reached.
41600 + */
41601 +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
41602 +{
41603 + return (frame1 != frame2) &&
41604 + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
41605 + (DWC_HFNUM_MAX_FRNUM >> 1));
41606 +}
41607 +
41608 +/**
41609 + * Increments _frame by the amount specified by _inc. The addition is done
41610 + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
41611 + */
41612 +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
41613 +{
41614 + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
41615 +}
41616 +
41617 +static inline uint16_t dwc_full_frame_num(uint16_t frame)
41618 +{
41619 + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
41620 +}
41621 +
41622 +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
41623 +{
41624 + return frame & 0x7;
41625 +}
41626 +
41627 +extern void init_hcd_usecs(dwc_otg_hcd_t *_hcd);
41628 +
41629 +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
41630 + dwc_otg_hc_regs_t * hc_regs,
41631 + dwc_otg_qtd_t * qtd);
41632 +
41633 +#ifdef DEBUG
41634 +/**
41635 + * Macro to sample the remaining PHY clocks left in the current frame. This
41636 + * may be used during debugging to determine the average time it takes to
41637 + * execute sections of code. There are two possible sample points, "a" and
41638 + * "b", so the _letter argument must be one of these values.
41639 + *
41640 + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
41641 + * example, "cat /sys/devices/lm0/hcd_frrem".
41642 + */
41643 +#define dwc_sample_frrem(_hcd, _qh, _letter) \
41644 +{ \
41645 + hfnum_data_t hfnum; \
41646 + dwc_otg_qtd_t *qtd; \
41647 + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
41648 + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
41649 + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
41650 + switch (hfnum.b.frnum & 0x7) { \
41651 + case 7: \
41652 + _hcd->hfnum_7_samples_##_letter++; \
41653 + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
41654 + break; \
41655 + case 0: \
41656 + _hcd->hfnum_0_samples_##_letter++; \
41657 + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
41658 + break; \
41659 + default: \
41660 + _hcd->hfnum_other_samples_##_letter++; \
41661 + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
41662 + break; \
41663 + } \
41664 + } \
41665 +}
41666 +#else
41667 +#define dwc_sample_frrem(_hcd, _qh, _letter)
41668 +#endif
41669 +#endif
41670 +#endif /* DWC_DEVICE_ONLY */
41671 --- /dev/null
41672 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
41673 @@ -0,0 +1,1134 @@
41674 +/*==========================================================================
41675 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
41676 + * $Revision: #10 $
41677 + * $Date: 2011/10/20 $
41678 + * $Change: 1869464 $
41679 + *
41680 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
41681 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
41682 + * otherwise expressly agreed to in writing between Synopsys and you.
41683 + *
41684 + * The Software IS NOT an item of Licensed Software or Licensed Product under
41685 + * any End User Software License Agreement or Agreement for Licensed Product
41686 + * with Synopsys or any supplement thereto. You are permitted to use and
41687 + * redistribute this Software in source and binary forms, with or without
41688 + * modification, provided that redistributions of source code must retain this
41689 + * notice. You may not view, use, disclose, copy or distribute this file or
41690 + * any information contained herein except pursuant to this license grant from
41691 + * Synopsys. If you do not agree with this notice, including the disclaimer
41692 + * below, then you are not authorized to use the Software.
41693 + *
41694 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
41695 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41696 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41697 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
41698 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41699 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41700 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41701 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41702 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41703 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41704 + * DAMAGE.
41705 + * ========================================================================== */
41706 +#ifndef DWC_DEVICE_ONLY
41707 +
41708 +/** @file
41709 + * This file contains Descriptor DMA support implementation for host mode.
41710 + */
41711 +
41712 +#include "dwc_otg_hcd.h"
41713 +#include "dwc_otg_regs.h"
41714 +
41715 +extern bool microframe_schedule;
41716 +
41717 +static inline uint8_t frame_list_idx(uint16_t frame)
41718 +{
41719 + return (frame & (MAX_FRLIST_EN_NUM - 1));
41720 +}
41721 +
41722 +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
41723 +{
41724 + return (idx + inc) &
41725 + (((speed ==
41726 + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
41727 + MAX_DMA_DESC_NUM_GENERIC) - 1);
41728 +}
41729 +
41730 +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
41731 +{
41732 + return (idx - inc) &
41733 + (((speed ==
41734 + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
41735 + MAX_DMA_DESC_NUM_GENERIC) - 1);
41736 +}
41737 +
41738 +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
41739 +{
41740 + return (((qh->ep_type == UE_ISOCHRONOUS)
41741 + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
41742 + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
41743 +}
41744 +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
41745 +{
41746 + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
41747 + ? ((qh->interval + 8 - 1) / 8)
41748 + : qh->interval);
41749 +}
41750 +
41751 +static int desc_list_alloc(struct device *dev, dwc_otg_qh_t * qh)
41752 +{
41753 + int retval = 0;
41754 +
41755 + qh->desc_list = (dwc_otg_host_dma_desc_t *)
41756 + DWC_DMA_ALLOC(dev, sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
41757 + &qh->desc_list_dma);
41758 +
41759 + if (!qh->desc_list) {
41760 + retval = -DWC_E_NO_MEMORY;
41761 + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
41762 +
41763 + }
41764 +
41765 + dwc_memset(qh->desc_list, 0x00,
41766 + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
41767 +
41768 + qh->n_bytes =
41769 + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
41770 +
41771 + if (!qh->n_bytes) {
41772 + retval = -DWC_E_NO_MEMORY;
41773 + DWC_ERROR
41774 + ("%s: Failed to allocate array for descriptors' size actual values\n",
41775 + __func__);
41776 +
41777 + }
41778 + return retval;
41779 +
41780 +}
41781 +
41782 +static void desc_list_free(struct device *dev, dwc_otg_qh_t * qh)
41783 +{
41784 + if (qh->desc_list) {
41785 + DWC_DMA_FREE(dev, max_desc_num(qh), qh->desc_list,
41786 + qh->desc_list_dma);
41787 + qh->desc_list = NULL;
41788 + }
41789 +
41790 + if (qh->n_bytes) {
41791 + DWC_FREE(qh->n_bytes);
41792 + qh->n_bytes = NULL;
41793 + }
41794 +}
41795 +
41796 +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
41797 +{
41798 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41799 + int retval = 0;
41800 +
41801 + if (hcd->frame_list)
41802 + return 0;
41803 +
41804 + hcd->frame_list = DWC_DMA_ALLOC(dev, 4 * MAX_FRLIST_EN_NUM,
41805 + &hcd->frame_list_dma);
41806 + if (!hcd->frame_list) {
41807 + retval = -DWC_E_NO_MEMORY;
41808 + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
41809 + }
41810 +
41811 + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
41812 +
41813 + return retval;
41814 +}
41815 +
41816 +static void frame_list_free(dwc_otg_hcd_t * hcd)
41817 +{
41818 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41819 +
41820 + if (!hcd->frame_list)
41821 + return;
41822 +
41823 + DWC_DMA_FREE(dev, 4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
41824 + hcd->frame_list = NULL;
41825 +}
41826 +
41827 +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
41828 +{
41829 +
41830 + hcfg_data_t hcfg;
41831 +
41832 + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
41833 +
41834 + if (hcfg.b.perschedena) {
41835 + /* already enabled */
41836 + return;
41837 + }
41838 +
41839 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
41840 + hcd->frame_list_dma);
41841 +
41842 + switch (fr_list_en) {
41843 + case 64:
41844 + hcfg.b.frlisten = 3;
41845 + break;
41846 + case 32:
41847 + hcfg.b.frlisten = 2;
41848 + break;
41849 + case 16:
41850 + hcfg.b.frlisten = 1;
41851 + break;
41852 + case 8:
41853 + hcfg.b.frlisten = 0;
41854 + break;
41855 + default:
41856 + break;
41857 + }
41858 +
41859 + hcfg.b.perschedena = 1;
41860 +
41861 + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
41862 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
41863 +
41864 +}
41865 +
41866 +static void per_sched_disable(dwc_otg_hcd_t * hcd)
41867 +{
41868 + hcfg_data_t hcfg;
41869 +
41870 + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
41871 +
41872 + if (!hcfg.b.perschedena) {
41873 + /* already disabled */
41874 + return;
41875 + }
41876 + hcfg.b.perschedena = 0;
41877 +
41878 + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
41879 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
41880 +}
41881 +
41882 +/*
41883 + * Activates/Deactivates FrameList entries for the channel
41884 + * based on endpoint servicing period.
41885 + */
41886 +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
41887 +{
41888 + uint16_t i, j, inc;
41889 + dwc_hc_t *hc = NULL;
41890 +
41891 + if (!qh->channel) {
41892 + DWC_ERROR("qh->channel = %p", qh->channel);
41893 + return;
41894 + }
41895 +
41896 + if (!hcd) {
41897 + DWC_ERROR("------hcd = %p", hcd);
41898 + return;
41899 + }
41900 +
41901 + if (!hcd->frame_list) {
41902 + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
41903 + return;
41904 + }
41905 +
41906 + hc = qh->channel;
41907 + inc = frame_incr_val(qh);
41908 + if (qh->ep_type == UE_ISOCHRONOUS)
41909 + i = frame_list_idx(qh->sched_frame);
41910 + else
41911 + i = 0;
41912 +
41913 + j = i;
41914 + do {
41915 + if (enable)
41916 + hcd->frame_list[j] |= (1 << hc->hc_num);
41917 + else
41918 + hcd->frame_list[j] &= ~(1 << hc->hc_num);
41919 + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
41920 + }
41921 + while (j != i);
41922 + if (!enable)
41923 + return;
41924 + hc->schinfo = 0;
41925 + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
41926 + j = 1;
41927 + /* TODO - check this */
41928 + inc = (8 + qh->interval - 1) / qh->interval;
41929 + for (i = 0; i < inc; i++) {
41930 + hc->schinfo |= j;
41931 + j = j << qh->interval;
41932 + }
41933 + } else {
41934 + hc->schinfo = 0xff;
41935 + }
41936 +}
41937 +
41938 +#if 1
41939 +void dump_frame_list(dwc_otg_hcd_t * hcd)
41940 +{
41941 + int i = 0;
41942 + DWC_PRINTF("--FRAME LIST (hex) --\n");
41943 + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
41944 + DWC_PRINTF("%x\t", hcd->frame_list[i]);
41945 + if (!(i % 8) && i)
41946 + DWC_PRINTF("\n");
41947 + }
41948 + DWC_PRINTF("\n----\n");
41949 +
41950 +}
41951 +#endif
41952 +
41953 +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41954 +{
41955 + dwc_hc_t *hc = qh->channel;
41956 + if (dwc_qh_is_non_per(qh)) {
41957 + if (!microframe_schedule)
41958 + hcd->non_periodic_channels--;
41959 + else
41960 + hcd->available_host_channels++;
41961 + } else
41962 + update_frame_list(hcd, qh, 0);
41963 +
41964 + /*
41965 + * The condition is added to prevent double cleanup try in case of device
41966 + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
41967 + */
41968 + if (hc->qh) {
41969 + dwc_otg_hc_cleanup(hcd->core_if, hc);
41970 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
41971 + hc->qh = NULL;
41972 + }
41973 +
41974 + qh->channel = NULL;
41975 + qh->ntd = 0;
41976 +
41977 + if (qh->desc_list) {
41978 + dwc_memset(qh->desc_list, 0x00,
41979 + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
41980 + }
41981 +}
41982 +
41983 +/**
41984 + * Initializes a QH structure's Descriptor DMA related members.
41985 + * Allocates memory for descriptor list.
41986 + * On first periodic QH, allocates memory for FrameList
41987 + * and enables periodic scheduling.
41988 + *
41989 + * @param hcd The HCD state structure for the DWC OTG controller.
41990 + * @param qh The QH to init.
41991 + *
41992 + * @return 0 if successful, negative error code otherwise.
41993 + */
41994 +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41995 +{
41996 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41997 + int retval = 0;
41998 +
41999 + if (qh->do_split) {
42000 + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
42001 + return -1;
42002 + }
42003 +
42004 + retval = desc_list_alloc(dev, qh);
42005 +
42006 + if ((retval == 0)
42007 + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
42008 + if (!hcd->frame_list) {
42009 + retval = frame_list_alloc(hcd);
42010 + /* Enable periodic schedule on first periodic QH */
42011 + if (retval == 0)
42012 + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
42013 + }
42014 + }
42015 +
42016 + qh->ntd = 0;
42017 +
42018 + return retval;
42019 +}
42020 +
42021 +/**
42022 + * Frees descriptor list memory associated with the QH.
42023 + * If QH is periodic and the last, frees FrameList memory
42024 + * and disables periodic scheduling.
42025 + *
42026 + * @param hcd The HCD state structure for the DWC OTG controller.
42027 + * @param qh The QH to init.
42028 + */
42029 +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42030 +{
42031 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
42032 +
42033 + desc_list_free(dev, qh);
42034 +
42035 + /*
42036 + * Channel still assigned due to some reasons.
42037 + * Seen on Isoc URB dequeue. Channel halted but no subsequent
42038 + * ChHalted interrupt to release the channel. Afterwards
42039 + * when it comes here from endpoint disable routine
42040 + * channel remains assigned.
42041 + */
42042 + if (qh->channel)
42043 + release_channel_ddma(hcd, qh);
42044 +
42045 + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
42046 + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
42047 +
42048 + per_sched_disable(hcd);
42049 + frame_list_free(hcd);
42050 + }
42051 +}
42052 +
42053 +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
42054 +{
42055 + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
42056 + /*
42057 + * Descriptor set(8 descriptors) index
42058 + * which is 8-aligned.
42059 + */
42060 + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
42061 + } else {
42062 + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
42063 + }
42064 +}
42065 +
42066 +/*
42067 + * Determine starting frame for Isochronous transfer.
42068 + * Few frames skipped to prevent race condition with HC.
42069 + */
42070 +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
42071 + uint8_t * skip_frames)
42072 +{
42073 + uint16_t frame = 0;
42074 + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
42075 +
42076 + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
42077 +
42078 + /*
42079 + * skip_frames is used to limit activated descriptors number
42080 + * to avoid the situation when HC services the last activated
42081 + * descriptor firstly.
42082 + * Example for FS:
42083 + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
42084 + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
42085 + * will be fetched. If the number of descriptors is max=64 (or greather) the
42086 + * list will be fully programmed with Active descriptors and it is possible
42087 + * case(rare) that the latest descriptor(considering rollback) corresponding
42088 + * to frame 2 will be serviced first. HS case is more probable because, in fact,
42089 + * up to 11 uframes(16 in the code) may be skipped.
42090 + */
42091 + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
42092 + /*
42093 + * Consider uframe counter also, to start xfer asap.
42094 + * If half of the frame elapsed skip 2 frames otherwise
42095 + * just 1 frame.
42096 + * Starting descriptor index must be 8-aligned, so
42097 + * if the current frame is near to complete the next one
42098 + * is skipped as well.
42099 + */
42100 +
42101 + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
42102 + *skip_frames = 2 * 8;
42103 + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
42104 + } else {
42105 + *skip_frames = 1 * 8;
42106 + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
42107 + }
42108 +
42109 + frame = dwc_full_frame_num(frame);
42110 + } else {
42111 + /*
42112 + * Two frames are skipped for FS - the current and the next.
42113 + * But for descriptor programming, 1 frame(descriptor) is enough,
42114 + * see example above.
42115 + */
42116 + *skip_frames = 1;
42117 + frame = dwc_frame_num_inc(hcd->frame_number, 2);
42118 + }
42119 +
42120 + return frame;
42121 +}
42122 +
42123 +/*
42124 + * Calculate initial descriptor index for isochronous transfer
42125 + * based on scheduled frame.
42126 + */
42127 +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42128 +{
42129 + uint16_t frame = 0, fr_idx, fr_idx_tmp;
42130 + uint8_t skip_frames = 0;
42131 + /*
42132 + * With current ISOC processing algorithm the channel is being
42133 + * released when no more QTDs in the list(qh->ntd == 0).
42134 + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
42135 + *
42136 + * So qh->channel != NULL branch is not used and just not removed from the
42137 + * source file. It is required for another possible approach which is,
42138 + * do not disable and release the channel when ISOC session completed,
42139 + * just move QH to inactive schedule until new QTD arrives.
42140 + * On new QTD, the QH moved back to 'ready' schedule,
42141 + * starting frame and therefore starting desc_index are recalculated.
42142 + * In this case channel is released only on ep_disable.
42143 + */
42144 +
42145 + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
42146 + if (qh->channel) {
42147 + frame = calc_starting_frame(hcd, qh, &skip_frames);
42148 + /*
42149 + * Calculate initial descriptor index based on FrameList current bitmap
42150 + * and servicing period.
42151 + */
42152 + fr_idx_tmp = frame_list_idx(frame);
42153 + fr_idx =
42154 + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
42155 + fr_idx_tmp)
42156 + % frame_incr_val(qh);
42157 + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
42158 + } else {
42159 + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
42160 + fr_idx = frame_list_idx(qh->sched_frame);
42161 + }
42162 +
42163 + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
42164 +
42165 + return skip_frames;
42166 +}
42167 +
42168 +#define ISOC_URB_GIVEBACK_ASAP
42169 +
42170 +#define MAX_ISOC_XFER_SIZE_FS 1023
42171 +#define MAX_ISOC_XFER_SIZE_HS 3072
42172 +#define DESCNUM_THRESHOLD 4
42173 +
42174 +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
42175 + uint8_t skip_frames)
42176 +{
42177 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
42178 + dwc_otg_qtd_t *qtd;
42179 + dwc_otg_host_dma_desc_t *dma_desc;
42180 + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
42181 +
42182 + idx = qh->td_last;
42183 + inc = qh->interval;
42184 + n_desc = 0;
42185 +
42186 + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
42187 + if (skip_frames && !qh->channel)
42188 + ntd_max = ntd_max - skip_frames / qh->interval;
42189 +
42190 + max_xfer_size =
42191 + (qh->dev_speed ==
42192 + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
42193 + MAX_ISOC_XFER_SIZE_FS;
42194 +
42195 + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
42196 + while ((qh->ntd < ntd_max)
42197 + && (qtd->isoc_frame_index_last <
42198 + qtd->urb->packet_count)) {
42199 +
42200 + dma_desc = &qh->desc_list[idx];
42201 + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
42202 +
42203 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
42204 +
42205 + if (frame_desc->length > max_xfer_size)
42206 + qh->n_bytes[idx] = max_xfer_size;
42207 + else
42208 + qh->n_bytes[idx] = frame_desc->length;
42209 + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
42210 + dma_desc->status.b_isoc.a = 1;
42211 + dma_desc->status.b_isoc.sts = 0;
42212 +
42213 + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
42214 +
42215 + qh->ntd++;
42216 +
42217 + qtd->isoc_frame_index_last++;
42218 +
42219 +#ifdef ISOC_URB_GIVEBACK_ASAP
42220 + /*
42221 + * Set IOC for each descriptor corresponding to the
42222 + * last frame of the URB.
42223 + */
42224 + if (qtd->isoc_frame_index_last ==
42225 + qtd->urb->packet_count)
42226 + dma_desc->status.b_isoc.ioc = 1;
42227 +
42228 +#endif
42229 + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
42230 + n_desc++;
42231 +
42232 + }
42233 + qtd->in_process = 1;
42234 + }
42235 +
42236 + qh->td_last = idx;
42237 +
42238 +#ifdef ISOC_URB_GIVEBACK_ASAP
42239 + /* Set IOC for the last descriptor if descriptor list is full */
42240 + if (qh->ntd == ntd_max) {
42241 + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
42242 + qh->desc_list[idx].status.b_isoc.ioc = 1;
42243 + }
42244 +#else
42245 + /*
42246 + * Set IOC bit only for one descriptor.
42247 + * Always try to be ahead of HW processing,
42248 + * i.e. on IOC generation driver activates next descriptors but
42249 + * core continues to process descriptors followed the one with IOC set.
42250 + */
42251 +
42252 + if (n_desc > DESCNUM_THRESHOLD) {
42253 + /*
42254 + * Move IOC "up". Required even if there is only one QTD
42255 + * in the list, cause QTDs migth continue to be queued,
42256 + * but during the activation it was only one queued.
42257 + * Actually more than one QTD might be in the list if this function called
42258 + * from XferCompletion - QTDs was queued during HW processing of the previous
42259 + * descriptor chunk.
42260 + */
42261 + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
42262 + } else {
42263 + /*
42264 + * Set the IOC for the latest descriptor
42265 + * if either number of descriptor is not greather than threshold
42266 + * or no more new descriptors activated.
42267 + */
42268 + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
42269 + }
42270 +
42271 + qh->desc_list[idx].status.b_isoc.ioc = 1;
42272 +#endif
42273 +}
42274 +
42275 +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42276 +{
42277 +
42278 + dwc_hc_t *hc;
42279 + dwc_otg_host_dma_desc_t *dma_desc;
42280 + dwc_otg_qtd_t *qtd;
42281 + int num_packets, len, n_desc = 0;
42282 +
42283 + hc = qh->channel;
42284 +
42285 + /*
42286 + * Start with hc->xfer_buff initialized in
42287 + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
42288 + * this pointer re-assigned to the buffer of the currently processed QTD.
42289 + * For non-SG request there is always one QTD active.
42290 + */
42291 +
42292 + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
42293 +
42294 + if (n_desc) {
42295 + /* SG request - more than 1 QTDs */
42296 + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
42297 + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
42298 + }
42299 +
42300 + qtd->n_desc = 0;
42301 +
42302 + do {
42303 + dma_desc = &qh->desc_list[n_desc];
42304 + len = hc->xfer_len;
42305 +
42306 + if (len > MAX_DMA_DESC_SIZE)
42307 + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
42308 +
42309 + if (hc->ep_is_in) {
42310 + if (len > 0) {
42311 + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
42312 + } else {
42313 + /* Need 1 packet for transfer length of 0. */
42314 + num_packets = 1;
42315 + }
42316 + /* Always program an integral # of max packets for IN transfers. */
42317 + len = num_packets * hc->max_packet;
42318 + }
42319 +
42320 + dma_desc->status.b.n_bytes = len;
42321 +
42322 + qh->n_bytes[n_desc] = len;
42323 +
42324 + if ((qh->ep_type == UE_CONTROL)
42325 + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
42326 + dma_desc->status.b.sup = 1; /* Setup Packet */
42327 +
42328 + dma_desc->status.b.a = 1; /* Active descriptor */
42329 + dma_desc->status.b.sts = 0;
42330 +
42331 + dma_desc->buf =
42332 + ((unsigned long)hc->xfer_buff & 0xffffffff);
42333 +
42334 + /*
42335 + * Last descriptor(or single) of IN transfer
42336 + * with actual size less than MaxPacket.
42337 + */
42338 + if (len > hc->xfer_len) {
42339 + hc->xfer_len = 0;
42340 + } else {
42341 + hc->xfer_buff += len;
42342 + hc->xfer_len -= len;
42343 + }
42344 +
42345 + qtd->n_desc++;
42346 + n_desc++;
42347 + }
42348 + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
42349 +
42350 +
42351 + qtd->in_process = 1;
42352 +
42353 + if (qh->ep_type == UE_CONTROL)
42354 + break;
42355 +
42356 + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
42357 + break;
42358 + }
42359 +
42360 + if (n_desc) {
42361 + /* Request Transfer Complete interrupt for the last descriptor */
42362 + qh->desc_list[n_desc - 1].status.b.ioc = 1;
42363 + /* End of List indicator */
42364 + qh->desc_list[n_desc - 1].status.b.eol = 1;
42365 +
42366 + hc->ntd = n_desc;
42367 + }
42368 +}
42369 +
42370 +/**
42371 + * For Control and Bulk endpoints initializes descriptor list
42372 + * and starts the transfer.
42373 + *
42374 + * For Interrupt and Isochronous endpoints initializes descriptor list
42375 + * then updates FrameList, marking appropriate entries as active.
42376 + * In case of Isochronous, the starting descriptor index is calculated based
42377 + * on the scheduled frame, but only on the first transfer descriptor within a session.
42378 + * Then starts the transfer via enabling the channel.
42379 + * For Isochronous endpoint the channel is not halted on XferComplete
42380 + * interrupt so remains assigned to the endpoint(QH) until session is done.
42381 + *
42382 + * @param hcd The HCD state structure for the DWC OTG controller.
42383 + * @param qh The QH to init.
42384 + *
42385 + * @return 0 if successful, negative error code otherwise.
42386 + */
42387 +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42388 +{
42389 + /* Channel is already assigned */
42390 + dwc_hc_t *hc = qh->channel;
42391 + uint8_t skip_frames = 0;
42392 +
42393 + switch (hc->ep_type) {
42394 + case DWC_OTG_EP_TYPE_CONTROL:
42395 + case DWC_OTG_EP_TYPE_BULK:
42396 + init_non_isoc_dma_desc(hcd, qh);
42397 +
42398 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42399 + break;
42400 + case DWC_OTG_EP_TYPE_INTR:
42401 + init_non_isoc_dma_desc(hcd, qh);
42402 +
42403 + update_frame_list(hcd, qh, 1);
42404 +
42405 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42406 + break;
42407 + case DWC_OTG_EP_TYPE_ISOC:
42408 +
42409 + if (!qh->ntd)
42410 + skip_frames = recalc_initial_desc_idx(hcd, qh);
42411 +
42412 + init_isoc_dma_desc(hcd, qh, skip_frames);
42413 +
42414 + if (!hc->xfer_started) {
42415 +
42416 + update_frame_list(hcd, qh, 1);
42417 +
42418 + /*
42419 + * Always set to max, instead of actual size.
42420 + * Otherwise ntd will be changed with
42421 + * channel being enabled. Not recommended.
42422 + *
42423 + */
42424 + hc->ntd = max_desc_num(qh);
42425 + /* Enable channel only once for ISOC */
42426 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42427 + }
42428 +
42429 + break;
42430 + default:
42431 +
42432 + break;
42433 + }
42434 +}
42435 +
42436 +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
42437 + dwc_hc_t * hc,
42438 + dwc_otg_hc_regs_t * hc_regs,
42439 + dwc_otg_halt_status_e halt_status)
42440 +{
42441 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
42442 + dwc_otg_qtd_t *qtd, *qtd_tmp;
42443 + dwc_otg_qh_t *qh;
42444 + dwc_otg_host_dma_desc_t *dma_desc;
42445 + uint16_t idx, remain;
42446 + uint8_t urb_compl;
42447 +
42448 + qh = hc->qh;
42449 + idx = qh->td_first;
42450 +
42451 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42452 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
42453 + qtd->in_process = 0;
42454 + return;
42455 + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
42456 + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
42457 + /*
42458 + * Channel is halted in these error cases.
42459 + * Considered as serious issues.
42460 + * Complete all URBs marking all frames as failed,
42461 + * irrespective whether some of the descriptors(frames) succeeded or no.
42462 + * Pass error code to completion routine as well, to
42463 + * update urb->status, some of class drivers might use it to stop
42464 + * queing transfer requests.
42465 + */
42466 + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
42467 + ? (-DWC_E_IO)
42468 + : (-DWC_E_OVERFLOW);
42469 +
42470 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42471 + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
42472 + frame_desc = &qtd->urb->iso_descs[idx];
42473 + frame_desc->status = err;
42474 + }
42475 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
42476 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42477 + }
42478 + return;
42479 + }
42480 +
42481 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42482 +
42483 + if (!qtd->in_process)
42484 + break;
42485 +
42486 + urb_compl = 0;
42487 +
42488 + do {
42489 +
42490 + dma_desc = &qh->desc_list[idx];
42491 +
42492 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
42493 + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
42494 +
42495 + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
42496 + /*
42497 + * XactError or, unable to complete all the transactions
42498 + * in the scheduled micro-frame/frame,
42499 + * both indicated by DMA_DESC_STS_PKTERR.
42500 + */
42501 + qtd->urb->error_count++;
42502 + frame_desc->actual_length = qh->n_bytes[idx] - remain;
42503 + frame_desc->status = -DWC_E_PROTOCOL;
42504 + } else {
42505 + /* Success */
42506 +
42507 + frame_desc->actual_length = qh->n_bytes[idx] - remain;
42508 + frame_desc->status = 0;
42509 + }
42510 +
42511 + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
42512 + /*
42513 + * urb->status is not used for isoc transfers here.
42514 + * The individual frame_desc status are used instead.
42515 + */
42516 +
42517 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
42518 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42519 +
42520 + /*
42521 + * This check is necessary because urb_dequeue can be called
42522 + * from urb complete callback(sound driver example).
42523 + * All pending URBs are dequeued there, so no need for
42524 + * further processing.
42525 + */
42526 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42527 + return;
42528 + }
42529 +
42530 + urb_compl = 1;
42531 +
42532 + }
42533 +
42534 + qh->ntd--;
42535 +
42536 + /* Stop if IOC requested descriptor reached */
42537 + if (dma_desc->status.b_isoc.ioc) {
42538 + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
42539 + goto stop_scan;
42540 + }
42541 +
42542 + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
42543 +
42544 + if (urb_compl)
42545 + break;
42546 + }
42547 + while (idx != qh->td_first);
42548 + }
42549 +stop_scan:
42550 + qh->td_first = idx;
42551 +}
42552 +
42553 +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
42554 + dwc_hc_t * hc,
42555 + dwc_otg_qtd_t * qtd,
42556 + dwc_otg_host_dma_desc_t * dma_desc,
42557 + dwc_otg_halt_status_e halt_status,
42558 + uint32_t n_bytes, uint8_t * xfer_done)
42559 +{
42560 +
42561 + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
42562 + dwc_otg_hcd_urb_t *urb = qtd->urb;
42563 +
42564 + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
42565 + urb->status = -DWC_E_IO;
42566 + return 1;
42567 + }
42568 + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
42569 + switch (halt_status) {
42570 + case DWC_OTG_HC_XFER_STALL:
42571 + urb->status = -DWC_E_PIPE;
42572 + break;
42573 + case DWC_OTG_HC_XFER_BABBLE_ERR:
42574 + urb->status = -DWC_E_OVERFLOW;
42575 + break;
42576 + case DWC_OTG_HC_XFER_XACT_ERR:
42577 + urb->status = -DWC_E_PROTOCOL;
42578 + break;
42579 + default:
42580 + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
42581 + halt_status);
42582 + break;
42583 + }
42584 + return 1;
42585 + }
42586 +
42587 + if (dma_desc->status.b.a == 1) {
42588 + DWC_DEBUGPL(DBG_HCDV,
42589 + "Active descriptor encountered on channel %d\n",
42590 + hc->hc_num);
42591 + return 0;
42592 + }
42593 +
42594 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
42595 + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
42596 + urb->actual_length += n_bytes - remain;
42597 + if (remain || urb->actual_length == urb->length) {
42598 + /*
42599 + * For Control Data stage do not set urb->status=0 to prevent
42600 + * URB callback. Set it when Status phase done. See below.
42601 + */
42602 + *xfer_done = 1;
42603 + }
42604 +
42605 + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
42606 + urb->status = 0;
42607 + *xfer_done = 1;
42608 + }
42609 + /* No handling for SETUP stage */
42610 + } else {
42611 + /* BULK and INTR */
42612 + urb->actual_length += n_bytes - remain;
42613 + if (remain || urb->actual_length == urb->length) {
42614 + urb->status = 0;
42615 + *xfer_done = 1;
42616 + }
42617 + }
42618 +
42619 + return 0;
42620 +}
42621 +
42622 +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
42623 + dwc_hc_t * hc,
42624 + dwc_otg_hc_regs_t * hc_regs,
42625 + dwc_otg_halt_status_e halt_status)
42626 +{
42627 + dwc_otg_hcd_urb_t *urb = NULL;
42628 + dwc_otg_qtd_t *qtd, *qtd_tmp;
42629 + dwc_otg_qh_t *qh;
42630 + dwc_otg_host_dma_desc_t *dma_desc;
42631 + uint32_t n_bytes, n_desc, i;
42632 + uint8_t failed = 0, xfer_done;
42633 +
42634 + n_desc = 0;
42635 +
42636 + qh = hc->qh;
42637 +
42638 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42639 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42640 + qtd->in_process = 0;
42641 + }
42642 + return;
42643 + }
42644 +
42645 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
42646 +
42647 + urb = qtd->urb;
42648 +
42649 + n_bytes = 0;
42650 + xfer_done = 0;
42651 +
42652 + for (i = 0; i < qtd->n_desc; i++) {
42653 + dma_desc = &qh->desc_list[n_desc];
42654 +
42655 + n_bytes = qh->n_bytes[n_desc];
42656 +
42657 + failed =
42658 + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
42659 + dma_desc,
42660 + halt_status, n_bytes,
42661 + &xfer_done);
42662 +
42663 + if (failed
42664 + || (xfer_done
42665 + && (urb->status != -DWC_E_IN_PROGRESS))) {
42666 +
42667 + hcd->fops->complete(hcd, urb->priv, urb,
42668 + urb->status);
42669 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42670 +
42671 + if (failed)
42672 + goto stop_scan;
42673 + } else if (qh->ep_type == UE_CONTROL) {
42674 + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
42675 + if (urb->length > 0) {
42676 + qtd->control_phase = DWC_OTG_CONTROL_DATA;
42677 + } else {
42678 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
42679 + }
42680 + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
42681 + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
42682 + if (xfer_done) {
42683 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
42684 + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
42685 + } else if (i + 1 == qtd->n_desc) {
42686 + /*
42687 + * Last descriptor for Control data stage which is
42688 + * not completed yet.
42689 + */
42690 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
42691 + }
42692 + }
42693 + }
42694 +
42695 + n_desc++;
42696 + }
42697 +
42698 + }
42699 +
42700 +stop_scan:
42701 +
42702 + if (qh->ep_type != UE_CONTROL) {
42703 + /*
42704 + * Resetting the data toggle for bulk
42705 + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
42706 + */
42707 + if (halt_status == DWC_OTG_HC_XFER_STALL)
42708 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
42709 + else
42710 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
42711 + }
42712 +
42713 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
42714 + hcint_data_t hcint;
42715 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
42716 + if (hcint.b.nyet) {
42717 + /*
42718 + * Got a NYET on the last transaction of the transfer. It
42719 + * means that the endpoint should be in the PING state at the
42720 + * beginning of the next transfer.
42721 + */
42722 + qh->ping_state = 1;
42723 + clear_hc_int(hc_regs, nyet);
42724 + }
42725 +
42726 + }
42727 +
42728 +}
42729 +
42730 +/**
42731 + * This function is called from interrupt handlers.
42732 + * Scans the descriptor list, updates URB's status and
42733 + * calls completion routine for the URB if it's done.
42734 + * Releases the channel to be used by other transfers.
42735 + * In case of Isochronous endpoint the channel is not halted until
42736 + * the end of the session, i.e. QTD list is empty.
42737 + * If periodic channel released the FrameList is updated accordingly.
42738 + *
42739 + * Calls transaction selection routines to activate pending transfers.
42740 + *
42741 + * @param hcd The HCD state structure for the DWC OTG controller.
42742 + * @param hc Host channel, the transfer is completed on.
42743 + * @param hc_regs Host channel registers.
42744 + * @param halt_status Reason the channel is being halted,
42745 + * or just XferComplete for isochronous transfer
42746 + */
42747 +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
42748 + dwc_hc_t * hc,
42749 + dwc_otg_hc_regs_t * hc_regs,
42750 + dwc_otg_halt_status_e halt_status)
42751 +{
42752 + uint8_t continue_isoc_xfer = 0;
42753 + dwc_otg_transaction_type_e tr_type;
42754 + dwc_otg_qh_t *qh = hc->qh;
42755 +
42756 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
42757 +
42758 + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
42759 +
42760 + /* Release the channel if halted or session completed */
42761 + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
42762 + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
42763 +
42764 + /* Halt the channel if session completed */
42765 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
42766 + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
42767 + }
42768 +
42769 + release_channel_ddma(hcd, qh);
42770 + dwc_otg_hcd_qh_remove(hcd, qh);
42771 + } else {
42772 + /* Keep in assigned schedule to continue transfer */
42773 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
42774 + &qh->qh_list_entry);
42775 + continue_isoc_xfer = 1;
42776 +
42777 + }
42778 + /** @todo Consider the case when period exceeds FrameList size.
42779 + * Frame Rollover interrupt should be used.
42780 + */
42781 + } else {
42782 + /* Scan descriptor list to complete the URB(s), then release the channel */
42783 + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
42784 +
42785 + release_channel_ddma(hcd, qh);
42786 + dwc_otg_hcd_qh_remove(hcd, qh);
42787 +
42788 + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
42789 + /* Add back to inactive non-periodic schedule on normal completion */
42790 + dwc_otg_hcd_qh_add(hcd, qh);
42791 + }
42792 +
42793 + }
42794 + tr_type = dwc_otg_hcd_select_transactions(hcd);
42795 + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
42796 + if (continue_isoc_xfer) {
42797 + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
42798 + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
42799 + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
42800 + tr_type = DWC_OTG_TRANSACTION_ALL;
42801 + }
42802 + }
42803 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
42804 + }
42805 +}
42806 +
42807 +#endif /* DWC_DEVICE_ONLY */
42808 --- /dev/null
42809 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
42810 @@ -0,0 +1,417 @@
42811 +/* ==========================================================================
42812 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
42813 + * $Revision: #12 $
42814 + * $Date: 2011/10/26 $
42815 + * $Change: 1873028 $
42816 + *
42817 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
42818 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
42819 + * otherwise expressly agreed to in writing between Synopsys and you.
42820 + *
42821 + * The Software IS NOT an item of Licensed Software or Licensed Product under
42822 + * any End User Software License Agreement or Agreement for Licensed Product
42823 + * with Synopsys or any supplement thereto. You are permitted to use and
42824 + * redistribute this Software in source and binary forms, with or without
42825 + * modification, provided that redistributions of source code must retain this
42826 + * notice. You may not view, use, disclose, copy or distribute this file or
42827 + * any information contained herein except pursuant to this license grant from
42828 + * Synopsys. If you do not agree with this notice, including the disclaimer
42829 + * below, then you are not authorized to use the Software.
42830 + *
42831 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
42832 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42833 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42834 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
42835 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42836 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42837 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42838 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42839 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42840 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42841 + * DAMAGE.
42842 + * ========================================================================== */
42843 +#ifndef DWC_DEVICE_ONLY
42844 +#ifndef __DWC_HCD_IF_H__
42845 +#define __DWC_HCD_IF_H__
42846 +
42847 +#include "dwc_otg_core_if.h"
42848 +
42849 +/** @file
42850 + * This file defines DWC_OTG HCD Core API.
42851 + */
42852 +
42853 +struct dwc_otg_hcd;
42854 +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
42855 +
42856 +struct dwc_otg_hcd_urb;
42857 +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
42858 +
42859 +/** @name HCD Function Driver Callbacks */
42860 +/** @{ */
42861 +
42862 +/** This function is called whenever core switches to host mode. */
42863 +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
42864 +
42865 +/** This function is called when device has been disconnected */
42866 +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
42867 +
42868 +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
42869 +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
42870 + void *urb_handle,
42871 + uint32_t * hub_addr,
42872 + uint32_t * port_addr);
42873 +/** Via this function HCD core gets device speed */
42874 +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
42875 + void *urb_handle);
42876 +
42877 +/** This function is called when urb is completed */
42878 +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
42879 + void *urb_handle,
42880 + dwc_otg_hcd_urb_t * dwc_otg_urb,
42881 + int32_t status);
42882 +
42883 +/** Via this function HCD core gets b_hnp_enable parameter */
42884 +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
42885 +
42886 +struct dwc_otg_hcd_function_ops {
42887 + dwc_otg_hcd_start_cb_t start;
42888 + dwc_otg_hcd_disconnect_cb_t disconnect;
42889 + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
42890 + dwc_otg_hcd_speed_from_urb_cb_t speed;
42891 + dwc_otg_hcd_complete_urb_cb_t complete;
42892 + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
42893 +};
42894 +/** @} */
42895 +
42896 +/** @name HCD Core API */
42897 +/** @{ */
42898 +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
42899 +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
42900 +
42901 +/** This function should be called to initiate HCD Core.
42902 + *
42903 + * @param hcd The HCD
42904 + * @param core_if The DWC_OTG Core
42905 + *
42906 + * Returns -DWC_E_NO_MEMORY if no enough memory.
42907 + * Returns 0 on success
42908 + */
42909 +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
42910 +
42911 +/** Frees HCD
42912 + *
42913 + * @param hcd The HCD
42914 + */
42915 +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
42916 +
42917 +/** This function should be called on every hardware interrupt.
42918 + *
42919 + * @param dwc_otg_hcd The HCD
42920 + *
42921 + * Returns non zero if interrupt is handled
42922 + * Return 0 if interrupt is not handled
42923 + */
42924 +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
42925 +
42926 +/** This function is used to handle the fast interrupt
42927 + *
42928 + */
42929 +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
42930 +
42931 +/**
42932 + * Returns private data set by
42933 + * dwc_otg_hcd_set_priv_data function.
42934 + *
42935 + * @param hcd The HCD
42936 + */
42937 +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
42938 +
42939 +/**
42940 + * Set private data.
42941 + *
42942 + * @param hcd The HCD
42943 + * @param priv_data pointer to be stored in private data
42944 + */
42945 +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
42946 +
42947 +/**
42948 + * This function initializes the HCD Core.
42949 + *
42950 + * @param hcd The HCD
42951 + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
42952 + *
42953 + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
42954 + * Returns 0 on success
42955 + */
42956 +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
42957 + struct dwc_otg_hcd_function_ops *fops);
42958 +
42959 +/**
42960 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
42961 + * stopped.
42962 + *
42963 + * @param hcd The HCD
42964 + */
42965 +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
42966 +
42967 +/**
42968 + * Handles hub class-specific requests.
42969 + *
42970 + * @param dwc_otg_hcd The HCD
42971 + * @param typeReq Request Type
42972 + * @param wValue wValue from control request
42973 + * @param wIndex wIndex from control request
42974 + * @param buf data buffer
42975 + * @param wLength data buffer length
42976 + *
42977 + * Returns -DWC_E_INVALID if invalid argument is passed
42978 + * Returns 0 on success
42979 + */
42980 +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
42981 + uint16_t typeReq, uint16_t wValue,
42982 + uint16_t wIndex, uint8_t * buf,
42983 + uint16_t wLength);
42984 +
42985 +/**
42986 + * Returns otg port number.
42987 + *
42988 + * @param hcd The HCD
42989 + */
42990 +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
42991 +
42992 +/**
42993 + * Returns OTG version - either 1.3 or 2.0.
42994 + *
42995 + * @param core_if The core_if structure pointer
42996 + */
42997 +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
42998 +
42999 +/**
43000 + * Returns 1 if currently core is acting as B host, and 0 otherwise.
43001 + *
43002 + * @param hcd The HCD
43003 + */
43004 +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
43005 +
43006 +/**
43007 + * Returns current frame number.
43008 + *
43009 + * @param hcd The HCD
43010 + */
43011 +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
43012 +
43013 +/**
43014 + * Dumps hcd state.
43015 + *
43016 + * @param hcd The HCD
43017 + */
43018 +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
43019 +
43020 +/**
43021 + * Dump the average frame remaining at SOF. This can be used to
43022 + * determine average interrupt latency. Frame remaining is also shown for
43023 + * start transfer and two additional sample points.
43024 + * Currently this function is not implemented.
43025 + *
43026 + * @param hcd The HCD
43027 + */
43028 +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
43029 +
43030 +/**
43031 + * Sends LPM transaction to the local device.
43032 + *
43033 + * @param hcd The HCD
43034 + * @param devaddr Device Address
43035 + * @param hird Host initiated resume duration
43036 + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
43037 + *
43038 + * Returns negative value if sending LPM transaction was not succeeded.
43039 + * Returns 0 on success.
43040 + */
43041 +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
43042 + uint8_t hird, uint8_t bRemoteWake);
43043 +
43044 +/* URB interface */
43045 +
43046 +/**
43047 + * Allocates memory for dwc_otg_hcd_urb structure.
43048 + * Allocated memory should be freed by call of DWC_FREE.
43049 + *
43050 + * @param hcd The HCD
43051 + * @param iso_desc_count Count of ISOC descriptors
43052 + * @param atomic_alloc Specefies whether to perform atomic allocation.
43053 + */
43054 +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
43055 + int iso_desc_count,
43056 + int atomic_alloc);
43057 +
43058 +/**
43059 + * Set pipe information in URB.
43060 + *
43061 + * @param hcd_urb DWC_OTG URB
43062 + * @param devaddr Device Address
43063 + * @param ep_num Endpoint Number
43064 + * @param ep_type Endpoint Type
43065 + * @param ep_dir Endpoint Direction
43066 + * @param mps Max Packet Size
43067 + */
43068 +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
43069 + uint8_t devaddr, uint8_t ep_num,
43070 + uint8_t ep_type, uint8_t ep_dir,
43071 + uint16_t mps);
43072 +
43073 +/* Transfer flags */
43074 +#define URB_GIVEBACK_ASAP 0x1
43075 +#define URB_SEND_ZERO_PACKET 0x2
43076 +
43077 +/**
43078 + * Sets dwc_otg_hcd_urb parameters.
43079 + *
43080 + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
43081 + * @param urb_handle Unique handle for request, this will be passed back
43082 + * to function driver in completion callback.
43083 + * @param buf The buffer for the data
43084 + * @param dma The DMA buffer for the data
43085 + * @param buflen Transfer length
43086 + * @param sp Buffer for setup data
43087 + * @param sp_dma DMA address of setup data buffer
43088 + * @param flags Transfer flags
43089 + * @param interval Polling interval for interrupt or isochronous transfers.
43090 + */
43091 +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
43092 + void *urb_handle, void *buf,
43093 + dwc_dma_t dma, uint32_t buflen, void *sp,
43094 + dwc_dma_t sp_dma, uint32_t flags,
43095 + uint16_t interval);
43096 +
43097 +/** Gets status from dwc_otg_hcd_urb
43098 + *
43099 + * @param dwc_otg_urb DWC_OTG URB
43100 + */
43101 +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
43102 +
43103 +/** Gets actual length from dwc_otg_hcd_urb
43104 + *
43105 + * @param dwc_otg_urb DWC_OTG URB
43106 + */
43107 +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
43108 + dwc_otg_urb);
43109 +
43110 +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
43111 + *
43112 + * @param dwc_otg_urb DWC_OTG URB
43113 + */
43114 +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
43115 + dwc_otg_urb);
43116 +
43117 +/** Set ISOC descriptor offset and length
43118 + *
43119 + * @param dwc_otg_urb DWC_OTG URB
43120 + * @param desc_num ISOC descriptor number
43121 + * @param offset Offset from beginig of buffer.
43122 + * @param length Transaction length
43123 + */
43124 +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
43125 + int desc_num, uint32_t offset,
43126 + uint32_t length);
43127 +
43128 +/** Get status of ISOC descriptor, specified by desc_num
43129 + *
43130 + * @param dwc_otg_urb DWC_OTG URB
43131 + * @param desc_num ISOC descriptor number
43132 + */
43133 +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
43134 + dwc_otg_urb, int desc_num);
43135 +
43136 +/** Get actual length of ISOC descriptor, specified by desc_num
43137 + *
43138 + * @param dwc_otg_urb DWC_OTG URB
43139 + * @param desc_num ISOC descriptor number
43140 + */
43141 +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
43142 + dwc_otg_urb,
43143 + int desc_num);
43144 +
43145 +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
43146 + *
43147 + * @param dwc_otg_hcd The HCD
43148 + * @param dwc_otg_urb DWC_OTG URB
43149 + * @param ep_handle Out parameter for returning endpoint handle
43150 + * @param atomic_alloc Flag to do atomic allocation if needed
43151 + *
43152 + * Returns -DWC_E_NO_DEVICE if no device is connected.
43153 + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
43154 + * Returns 0 on success.
43155 + */
43156 +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
43157 + dwc_otg_hcd_urb_t * dwc_otg_urb,
43158 + void **ep_handle, int atomic_alloc);
43159 +
43160 +/** De-queue the specified URB
43161 + *
43162 + * @param dwc_otg_hcd The HCD
43163 + * @param dwc_otg_urb DWC_OTG URB
43164 + */
43165 +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
43166 + dwc_otg_hcd_urb_t * dwc_otg_urb);
43167 +
43168 +/** Frees resources in the DWC_otg controller related to a given endpoint.
43169 + * Any URBs for the endpoint must already be dequeued.
43170 + *
43171 + * @param hcd The HCD
43172 + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
43173 + * @param retry Number of retries if there are queued transfers.
43174 + *
43175 + * Returns -DWC_E_INVALID if invalid arguments are passed.
43176 + * Returns 0 on success
43177 + */
43178 +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
43179 + int retry);
43180 +
43181 +/* Resets the data toggle in qh structure. This function can be called from
43182 + * usb_clear_halt routine.
43183 + *
43184 + * @param hcd The HCD
43185 + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
43186 + *
43187 + * Returns -DWC_E_INVALID if invalid arguments are passed.
43188 + * Returns 0 on success
43189 + */
43190 +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
43191 +
43192 +/** Returns 1 if status of specified port is changed and 0 otherwise.
43193 + *
43194 + * @param hcd The HCD
43195 + * @param port Port number
43196 + */
43197 +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
43198 +
43199 +/** Call this function to check if bandwidth was allocated for specified endpoint.
43200 + * Only for ISOC and INTERRUPT endpoints.
43201 + *
43202 + * @param hcd The HCD
43203 + * @param ep_handle Endpoint handle
43204 + */
43205 +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
43206 + void *ep_handle);
43207 +
43208 +/** Call this function to check if bandwidth was freed for specified endpoint.
43209 + *
43210 + * @param hcd The HCD
43211 + * @param ep_handle Endpoint handle
43212 + */
43213 +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
43214 +
43215 +/** Returns bandwidth allocated for specified endpoint in microseconds.
43216 + * Only for ISOC and INTERRUPT endpoints.
43217 + *
43218 + * @param hcd The HCD
43219 + * @param ep_handle Endpoint handle
43220 + */
43221 +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
43222 + void *ep_handle);
43223 +
43224 +/** @} */
43225 +
43226 +#endif /* __DWC_HCD_IF_H__ */
43227 +#endif /* DWC_DEVICE_ONLY */
43228 --- /dev/null
43229 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
43230 @@ -0,0 +1,2752 @@
43231 +/* ==========================================================================
43232 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
43233 + * $Revision: #89 $
43234 + * $Date: 2011/10/20 $
43235 + * $Change: 1869487 $
43236 + *
43237 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
43238 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
43239 + * otherwise expressly agreed to in writing between Synopsys and you.
43240 + *
43241 + * The Software IS NOT an item of Licensed Software or Licensed Product under
43242 + * any End User Software License Agreement or Agreement for Licensed Product
43243 + * with Synopsys or any supplement thereto. You are permitted to use and
43244 + * redistribute this Software in source and binary forms, with or without
43245 + * modification, provided that redistributions of source code must retain this
43246 + * notice. You may not view, use, disclose, copy or distribute this file or
43247 + * any information contained herein except pursuant to this license grant from
43248 + * Synopsys. If you do not agree with this notice, including the disclaimer
43249 + * below, then you are not authorized to use the Software.
43250 + *
43251 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
43252 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43253 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43254 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
43255 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43256 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43257 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
43258 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43259 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43260 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
43261 + * DAMAGE.
43262 + * ========================================================================== */
43263 +#ifndef DWC_DEVICE_ONLY
43264 +
43265 +#include "dwc_otg_hcd.h"
43266 +#include "dwc_otg_regs.h"
43267 +
43268 +#include <linux/jiffies.h>
43269 +#include <asm/fiq.h>
43270 +
43271 +
43272 +extern bool microframe_schedule;
43273 +
43274 +/** @file
43275 + * This file contains the implementation of the HCD Interrupt handlers.
43276 + */
43277 +
43278 +int fiq_done, int_done;
43279 +
43280 +#ifdef FIQ_DEBUG
43281 +char buffer[1000*16];
43282 +int wptr;
43283 +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
43284 +{
43285 + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
43286 + va_list args;
43287 + char text[17];
43288 + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
43289 +
43290 + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
43291 + {
43292 + local_fiq_disable();
43293 + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
43294 + va_start(args, fmt);
43295 + vsnprintf(text+8, 9, fmt, args);
43296 + va_end(args);
43297 +
43298 + memcpy(buffer + wptr, text, 16);
43299 + wptr = (wptr + 16) % sizeof(buffer);
43300 + local_fiq_enable();
43301 + }
43302 +}
43303 +#endif
43304 +
43305 +/** This function handles interrupts for the HCD. */
43306 +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43307 +{
43308 + int retval = 0;
43309 + static int last_time;
43310 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
43311 + gintsts_data_t gintsts;
43312 + gintmsk_data_t gintmsk;
43313 + hfnum_data_t hfnum;
43314 + haintmsk_data_t haintmsk;
43315 +
43316 +#ifdef DEBUG
43317 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
43318 +
43319 +#endif
43320 +
43321 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
43322 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
43323 +
43324 + /* Exit from ISR if core is hibernated */
43325 + if (core_if->hibernation_suspend == 1) {
43326 + goto exit_handler_routine;
43327 + }
43328 + DWC_SPINLOCK(dwc_otg_hcd->lock);
43329 + /* Check if HOST Mode */
43330 + if (dwc_otg_is_host_mode(core_if)) {
43331 + if (fiq_enable) {
43332 + local_fiq_disable();
43333 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43334 + /* Pull in from the FIQ's disabled mask */
43335 + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
43336 + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
43337 + }
43338 +
43339 + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
43340 + gintsts.b.hcintr = 1;
43341 + }
43342 +
43343 + /* Danger will robinson: fake a SOF if necessary */
43344 + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
43345 + gintsts.b.sofintr = 1;
43346 + }
43347 + gintsts.d32 &= gintmsk.d32;
43348 +
43349 + if (fiq_enable) {
43350 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43351 + local_fiq_enable();
43352 + }
43353 +
43354 + if (!gintsts.d32) {
43355 + goto exit_handler_routine;
43356 + }
43357 +
43358 +#ifdef DEBUG
43359 + // We should be OK doing this because the common interrupts should already have been serviced
43360 + /* Don't print debug message in the interrupt handler on SOF */
43361 +#ifndef DEBUG_SOF
43362 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43363 +#endif
43364 + DWC_DEBUGPL(DBG_HCDI, "\n");
43365 +#endif
43366 +
43367 +#ifdef DEBUG
43368 +#ifndef DEBUG_SOF
43369 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43370 +#endif
43371 + DWC_DEBUGPL(DBG_HCDI,
43372 + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
43373 + gintsts.d32, core_if);
43374 +#endif
43375 + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
43376 + if (gintsts.b.sofintr) {
43377 + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
43378 + }
43379 +
43380 + if (gintsts.b.rxstsqlvl) {
43381 + retval |=
43382 + dwc_otg_hcd_handle_rx_status_q_level_intr
43383 + (dwc_otg_hcd);
43384 + }
43385 + if (gintsts.b.nptxfempty) {
43386 + retval |=
43387 + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
43388 + (dwc_otg_hcd);
43389 + }
43390 + if (gintsts.b.i2cintr) {
43391 + /** @todo Implement i2cintr handler. */
43392 + }
43393 + if (gintsts.b.portintr) {
43394 +
43395 + gintmsk_data_t gintmsk = { .b.portintr = 1};
43396 + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
43397 + if (fiq_enable) {
43398 + local_fiq_disable();
43399 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43400 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
43401 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43402 + local_fiq_enable();
43403 + } else {
43404 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
43405 + }
43406 + }
43407 + if (gintsts.b.hcintr) {
43408 + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
43409 + }
43410 + if (gintsts.b.ptxfempty) {
43411 + retval |=
43412 + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
43413 + (dwc_otg_hcd);
43414 + }
43415 +#ifdef DEBUG
43416 +#ifndef DEBUG_SOF
43417 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43418 +#endif
43419 + {
43420 + DWC_DEBUGPL(DBG_HCDI,
43421 + "DWC OTG HCD Finished Servicing Interrupts\n");
43422 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
43423 + DWC_READ_REG32(&global_regs->gintsts));
43424 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
43425 + DWC_READ_REG32(&global_regs->gintmsk));
43426 + }
43427 +#endif
43428 +
43429 +#ifdef DEBUG
43430 +#ifndef DEBUG_SOF
43431 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43432 +#endif
43433 + DWC_DEBUGPL(DBG_HCDI, "\n");
43434 +#endif
43435 +
43436 + }
43437 +
43438 +exit_handler_routine:
43439 + if (fiq_enable) {
43440 + gintmsk_data_t gintmsk_new;
43441 + haintmsk_data_t haintmsk_new;
43442 + local_fiq_disable();
43443 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43444 + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
43445 + if(fiq_fsm_enable)
43446 + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
43447 + else
43448 + haintmsk_new.d32 = 0x0000FFFF;
43449 +
43450 + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
43451 + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
43452 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
43453 + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
43454 + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
43455 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
43456 + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
43457 + ;
43458 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
43459 + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
43460 + }
43461 + int_done++;
43462 + }
43463 + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
43464 + /* Re-enable interrupts that the FIQ masked (first time round) */
43465 + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
43466 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43467 + local_fiq_enable();
43468 +
43469 + if ((jiffies / HZ) > last_time) {
43470 + //dwc_otg_qh_t *qh;
43471 + //dwc_list_link_t *cur;
43472 + /* Once a second output the fiq and irq numbers, useful for debug */
43473 + last_time = jiffies / HZ;
43474 + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
43475 + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
43476 + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
43477 + //printk(KERN_WARNING "Periodic queues:\n");
43478 + }
43479 + }
43480 +
43481 + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
43482 + return retval;
43483 +}
43484 +
43485 +#ifdef DWC_TRACK_MISSED_SOFS
43486 +
43487 +#warning Compiling code to track missed SOFs
43488 +#define FRAME_NUM_ARRAY_SIZE 1000
43489 +/**
43490 + * This function is for debug only.
43491 + */
43492 +static inline void track_missed_sofs(uint16_t curr_frame_number)
43493 +{
43494 + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
43495 + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
43496 + static int frame_num_idx = 0;
43497 + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
43498 + static int dumped_frame_num_array = 0;
43499 +
43500 + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
43501 + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
43502 + curr_frame_number) {
43503 + frame_num_array[frame_num_idx] = curr_frame_number;
43504 + last_frame_num_array[frame_num_idx++] = last_frame_num;
43505 + }
43506 + } else if (!dumped_frame_num_array) {
43507 + int i;
43508 + DWC_PRINTF("Frame Last Frame\n");
43509 + DWC_PRINTF("----- ----------\n");
43510 + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
43511 + DWC_PRINTF("0x%04x 0x%04x\n",
43512 + frame_num_array[i], last_frame_num_array[i]);
43513 + }
43514 + dumped_frame_num_array = 1;
43515 + }
43516 + last_frame_num = curr_frame_number;
43517 +}
43518 +#endif
43519 +
43520 +/**
43521 + * Handles the start-of-frame interrupt in host mode. Non-periodic
43522 + * transactions may be queued to the DWC_otg controller for the current
43523 + * (micro)frame. Periodic transactions may be queued to the controller for the
43524 + * next (micro)frame.
43525 + */
43526 +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
43527 +{
43528 + hfnum_data_t hfnum;
43529 + gintsts_data_t gintsts = { .d32 = 0 };
43530 + dwc_list_link_t *qh_entry;
43531 + dwc_otg_qh_t *qh;
43532 + dwc_otg_transaction_type_e tr_type;
43533 + int did_something = 0;
43534 + int32_t next_sched_frame = -1;
43535 +
43536 + hfnum.d32 =
43537 + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
43538 +
43539 +#ifdef DEBUG_SOF
43540 + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
43541 +#endif
43542 + hcd->frame_number = hfnum.b.frnum;
43543 +
43544 +#ifdef DEBUG
43545 + hcd->frrem_accum += hfnum.b.frrem;
43546 + hcd->frrem_samples++;
43547 +#endif
43548 +
43549 +#ifdef DWC_TRACK_MISSED_SOFS
43550 + track_missed_sofs(hcd->frame_number);
43551 +#endif
43552 + /* Determine whether any periodic QHs should be executed. */
43553 + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
43554 + while (qh_entry != &hcd->periodic_sched_inactive) {
43555 + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
43556 + qh_entry = qh_entry->next;
43557 + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
43558 +
43559 + /*
43560 + * Move QH to the ready list to be executed next
43561 + * (micro)frame.
43562 + */
43563 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
43564 + &qh->qh_list_entry);
43565 +
43566 + did_something = 1;
43567 + }
43568 + else
43569 + {
43570 + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
43571 + {
43572 + next_sched_frame = qh->sched_frame;
43573 + }
43574 + }
43575 + }
43576 + if (fiq_enable)
43577 + hcd->fiq_state->next_sched_frame = next_sched_frame;
43578 +
43579 + tr_type = dwc_otg_hcd_select_transactions(hcd);
43580 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
43581 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
43582 + did_something = 1;
43583 + }
43584 +
43585 + /* Clear interrupt - but do not trample on the FIQ sof */
43586 + if (!fiq_fsm_enable) {
43587 + gintsts.b.sofintr = 1;
43588 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
43589 + }
43590 + return 1;
43591 +}
43592 +
43593 +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
43594 + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
43595 + * memory if the DWC_otg controller is operating in Slave mode. */
43596 +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43597 +{
43598 + host_grxsts_data_t grxsts;
43599 + dwc_hc_t *hc = NULL;
43600 +
43601 + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
43602 +
43603 + grxsts.d32 =
43604 + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
43605 +
43606 + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
43607 + if (!hc) {
43608 + DWC_ERROR("Unable to get corresponding channel\n");
43609 + return 0;
43610 + }
43611 +
43612 + /* Packet Status */
43613 + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
43614 + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
43615 + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
43616 + hc->data_pid_start);
43617 + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
43618 +
43619 + switch (grxsts.b.pktsts) {
43620 + case DWC_GRXSTS_PKTSTS_IN:
43621 + /* Read the data into the host buffer. */
43622 + if (grxsts.b.bcnt > 0) {
43623 + dwc_otg_read_packet(dwc_otg_hcd->core_if,
43624 + hc->xfer_buff, grxsts.b.bcnt);
43625 +
43626 + /* Update the HC fields for the next packet received. */
43627 + hc->xfer_count += grxsts.b.bcnt;
43628 + hc->xfer_buff += grxsts.b.bcnt;
43629 + }
43630 +
43631 + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
43632 + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
43633 + case DWC_GRXSTS_PKTSTS_CH_HALTED:
43634 + /* Handled in interrupt, just ignore data */
43635 + break;
43636 + default:
43637 + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
43638 + grxsts.b.pktsts);
43639 + break;
43640 + }
43641 +
43642 + return 1;
43643 +}
43644 +
43645 +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
43646 + * data packets may be written to the FIFO for OUT transfers. More requests
43647 + * may be written to the non-periodic request queue for IN transfers. This
43648 + * interrupt is enabled only in Slave mode. */
43649 +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43650 +{
43651 + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
43652 + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
43653 + DWC_OTG_TRANSACTION_NON_PERIODIC);
43654 + return 1;
43655 +}
43656 +
43657 +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
43658 + * packets may be written to the FIFO for OUT transfers. More requests may be
43659 + * written to the periodic request queue for IN transfers. This interrupt is
43660 + * enabled only in Slave mode. */
43661 +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43662 +{
43663 + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
43664 + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
43665 + DWC_OTG_TRANSACTION_PERIODIC);
43666 + return 1;
43667 +}
43668 +
43669 +/** There are multiple conditions that can cause a port interrupt. This function
43670 + * determines which interrupt conditions have occurred and handles them
43671 + * appropriately. */
43672 +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43673 +{
43674 + int retval = 0;
43675 + hprt0_data_t hprt0;
43676 + hprt0_data_t hprt0_modify;
43677 +
43678 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43679 + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43680 +
43681 + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
43682 + * GINTSTS */
43683 +
43684 + hprt0_modify.b.prtena = 0;
43685 + hprt0_modify.b.prtconndet = 0;
43686 + hprt0_modify.b.prtenchng = 0;
43687 + hprt0_modify.b.prtovrcurrchng = 0;
43688 +
43689 + /* Port Connect Detected
43690 + * Set flag and clear if detected */
43691 + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
43692 + // Dont modify port status if we are in hibernation state
43693 + hprt0_modify.b.prtconndet = 1;
43694 + hprt0_modify.b.prtenchng = 1;
43695 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
43696 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43697 + return retval;
43698 + }
43699 +
43700 + if (hprt0.b.prtconndet) {
43701 + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
43702 + if (dwc_otg_hcd->core_if->adp_enable &&
43703 + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
43704 + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
43705 + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
43706 + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
43707 + /* TODO - check if this is required, as
43708 + * host initialization was already performed
43709 + * after initial ADP probing
43710 + */
43711 + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
43712 + dwc_otg_core_init(dwc_otg_hcd->core_if);
43713 + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
43714 + cil_hcd_start(dwc_otg_hcd->core_if);*/
43715 + } else {
43716 +
43717 + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
43718 + "Port Connect Detected--\n", hprt0.d32);
43719 + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
43720 + dwc_otg_hcd->flags.b.port_connect_status = 1;
43721 + hprt0_modify.b.prtconndet = 1;
43722 +
43723 + /* B-Device has connected, Delete the connection timer. */
43724 + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
43725 + }
43726 + /* The Hub driver asserts a reset when it sees port connect
43727 + * status change flag */
43728 + retval |= 1;
43729 + }
43730 +
43731 + /* Port Enable Changed
43732 + * Clear if detected - Set internal flag if disabled */
43733 + if (hprt0.b.prtenchng) {
43734 + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
43735 + "Port Enable Changed--\n", hprt0.d32);
43736 + hprt0_modify.b.prtenchng = 1;
43737 + if (hprt0.b.prtena == 1) {
43738 + hfir_data_t hfir;
43739 + int do_reset = 0;
43740 + dwc_otg_core_params_t *params =
43741 + dwc_otg_hcd->core_if->core_params;
43742 + dwc_otg_core_global_regs_t *global_regs =
43743 + dwc_otg_hcd->core_if->core_global_regs;
43744 + dwc_otg_host_if_t *host_if =
43745 + dwc_otg_hcd->core_if->host_if;
43746 +
43747 + dwc_otg_hcd->flags.b.port_speed = hprt0.b.prtspd;
43748 + if (microframe_schedule)
43749 + init_hcd_usecs(dwc_otg_hcd);
43750 +
43751 + /* Every time when port enables calculate
43752 + * HFIR.FrInterval
43753 + */
43754 + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
43755 + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
43756 + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
43757 +
43758 + /* Check if we need to adjust the PHY clock speed for
43759 + * low power and adjust it */
43760 + if (params->host_support_fs_ls_low_power) {
43761 + gusbcfg_data_t usbcfg;
43762 +
43763 + usbcfg.d32 =
43764 + DWC_READ_REG32(&global_regs->gusbcfg);
43765 +
43766 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
43767 + || hprt0.b.prtspd ==
43768 + DWC_HPRT0_PRTSPD_FULL_SPEED) {
43769 + /*
43770 + * Low power
43771 + */
43772 + hcfg_data_t hcfg;
43773 + if (usbcfg.b.phylpwrclksel == 0) {
43774 + /* Set PHY low power clock select for FS/LS devices */
43775 + usbcfg.b.phylpwrclksel = 1;
43776 + DWC_WRITE_REG32
43777 + (&global_regs->gusbcfg,
43778 + usbcfg.d32);
43779 + do_reset = 1;
43780 + }
43781 +
43782 + hcfg.d32 =
43783 + DWC_READ_REG32
43784 + (&host_if->host_global_regs->hcfg);
43785 +
43786 + if (hprt0.b.prtspd ==
43787 + DWC_HPRT0_PRTSPD_LOW_SPEED
43788 + && params->host_ls_low_power_phy_clk
43789 + ==
43790 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
43791 + {
43792 + /* 6 MHZ */
43793 + DWC_DEBUGPL(DBG_CIL,
43794 + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
43795 + if (hcfg.b.fslspclksel !=
43796 + DWC_HCFG_6_MHZ) {
43797 + hcfg.b.fslspclksel =
43798 + DWC_HCFG_6_MHZ;
43799 + DWC_WRITE_REG32
43800 + (&host_if->host_global_regs->hcfg,
43801 + hcfg.d32);
43802 + do_reset = 1;
43803 + }
43804 + } else {
43805 + /* 48 MHZ */
43806 + DWC_DEBUGPL(DBG_CIL,
43807 + "FS_PHY programming HCFG to 48 MHz ()\n");
43808 + if (hcfg.b.fslspclksel !=
43809 + DWC_HCFG_48_MHZ) {
43810 + hcfg.b.fslspclksel =
43811 + DWC_HCFG_48_MHZ;
43812 + DWC_WRITE_REG32
43813 + (&host_if->host_global_regs->hcfg,
43814 + hcfg.d32);
43815 + do_reset = 1;
43816 + }
43817 + }
43818 + } else {
43819 + /*
43820 + * Not low power
43821 + */
43822 + if (usbcfg.b.phylpwrclksel == 1) {
43823 + usbcfg.b.phylpwrclksel = 0;
43824 + DWC_WRITE_REG32
43825 + (&global_regs->gusbcfg,
43826 + usbcfg.d32);
43827 + do_reset = 1;
43828 + }
43829 + }
43830 +
43831 + if (do_reset) {
43832 + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
43833 + }
43834 + }
43835 +
43836 + if (!do_reset) {
43837 + /* Port has been enabled set the reset change flag */
43838 + dwc_otg_hcd->flags.b.port_reset_change = 1;
43839 + }
43840 + } else {
43841 + dwc_otg_hcd->flags.b.port_enable_change = 1;
43842 + }
43843 + retval |= 1;
43844 + }
43845 +
43846 + /** Overcurrent Change Interrupt */
43847 + if (hprt0.b.prtovrcurrchng) {
43848 + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
43849 + "Port Overcurrent Changed--\n", hprt0.d32);
43850 + dwc_otg_hcd->flags.b.port_over_current_change = 1;
43851 + hprt0_modify.b.prtovrcurrchng = 1;
43852 + retval |= 1;
43853 + }
43854 +
43855 + /* Clear Port Interrupts */
43856 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
43857 +
43858 + return retval;
43859 +}
43860 +
43861 +/** This interrupt indicates that one or more host channels has a pending
43862 + * interrupt. There are multiple conditions that can cause each host channel
43863 + * interrupt. This function determines which conditions have occurred for each
43864 + * host channel interrupt and handles them appropriately. */
43865 +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43866 +{
43867 + int i;
43868 + int retval = 0;
43869 + haint_data_t haint = { .d32 = 0 } ;
43870 +
43871 + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
43872 + * GINTSTS */
43873 +
43874 + if (!fiq_fsm_enable)
43875 + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
43876 +
43877 + // Overwrite with saved interrupts from fiq handler
43878 + if(fiq_fsm_enable)
43879 + {
43880 + /* check the mask? */
43881 + local_fiq_disable();
43882 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43883 + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
43884 + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
43885 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43886 + local_fiq_enable();
43887 + }
43888 +
43889 + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
43890 + if (haint.b2.chint & (1 << i)) {
43891 + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
43892 + }
43893 + }
43894 +
43895 + return retval;
43896 +}
43897 +
43898 +/**
43899 + * Gets the actual length of a transfer after the transfer halts. _halt_status
43900 + * holds the reason for the halt.
43901 + *
43902 + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
43903 + * *short_read is set to 1 upon return if less than the requested
43904 + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
43905 + * return. short_read may also be NULL on entry, in which case it remains
43906 + * unchanged.
43907 + */
43908 +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
43909 + dwc_otg_hc_regs_t * hc_regs,
43910 + dwc_otg_qtd_t * qtd,
43911 + dwc_otg_halt_status_e halt_status,
43912 + int *short_read)
43913 +{
43914 + hctsiz_data_t hctsiz;
43915 + uint32_t length;
43916 +
43917 + if (short_read != NULL) {
43918 + *short_read = 0;
43919 + }
43920 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43921 +
43922 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
43923 + if (hc->ep_is_in) {
43924 + length = hc->xfer_len - hctsiz.b.xfersize;
43925 + if (short_read != NULL) {
43926 + *short_read = (hctsiz.b.xfersize != 0);
43927 + }
43928 + } else if (hc->qh->do_split) {
43929 + //length = split_out_xfersize[hc->hc_num];
43930 + length = qtd->ssplit_out_xfer_count;
43931 + } else {
43932 + length = hc->xfer_len;
43933 + }
43934 + } else {
43935 + /*
43936 + * Must use the hctsiz.pktcnt field to determine how much data
43937 + * has been transferred. This field reflects the number of
43938 + * packets that have been transferred via the USB. This is
43939 + * always an integral number of packets if the transfer was
43940 + * halted before its normal completion. (Can't use the
43941 + * hctsiz.xfersize field because that reflects the number of
43942 + * bytes transferred via the AHB, not the USB).
43943 + */
43944 + length =
43945 + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
43946 + }
43947 +
43948 + return length;
43949 +}
43950 +
43951 +/**
43952 + * Updates the state of the URB after a Transfer Complete interrupt on the
43953 + * host channel. Updates the actual_length field of the URB based on the
43954 + * number of bytes transferred via the host channel. Sets the URB status
43955 + * if the data transfer is finished.
43956 + *
43957 + * @return 1 if the data transfer specified by the URB is completely finished,
43958 + * 0 otherwise.
43959 + */
43960 +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
43961 + dwc_otg_hc_regs_t * hc_regs,
43962 + dwc_otg_hcd_urb_t * urb,
43963 + dwc_otg_qtd_t * qtd)
43964 +{
43965 + int xfer_done = 0;
43966 + int short_read = 0;
43967 +
43968 + int xfer_length;
43969 +
43970 + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
43971 + DWC_OTG_HC_XFER_COMPLETE,
43972 + &short_read);
43973 +
43974 + if (urb->actual_length + xfer_length > urb->length) {
43975 + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
43976 + hc->dev_addr, __func__, __LINE__);
43977 + xfer_length = urb->length - urb->actual_length;
43978 + }
43979 +
43980 + /* non DWORD-aligned buffer case handling. */
43981 + if (hc->align_buff && xfer_length && hc->ep_is_in) {
43982 + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
43983 + xfer_length);
43984 + }
43985 +
43986 + urb->actual_length += xfer_length;
43987 +
43988 + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
43989 + (urb->flags & URB_SEND_ZERO_PACKET)
43990 + && (urb->actual_length == urb->length)
43991 + && !(urb->length % hc->max_packet)) {
43992 + xfer_done = 0;
43993 + } else if (short_read || urb->actual_length >= urb->length) {
43994 + xfer_done = 1;
43995 + urb->status = 0;
43996 + }
43997 +
43998 +#ifdef DEBUG
43999 + {
44000 + hctsiz_data_t hctsiz;
44001 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44002 + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
44003 + __func__, (hc->ep_is_in ? "IN" : "OUT"),
44004 + hc->hc_num);
44005 + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
44006 + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
44007 + hctsiz.b.xfersize);
44008 + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
44009 + urb->length);
44010 + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
44011 + urb->actual_length);
44012 + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
44013 + short_read, xfer_done);
44014 + }
44015 +#endif
44016 +
44017 + return xfer_done;
44018 +}
44019 +
44020 +/*
44021 + * Save the starting data toggle for the next transfer. The data toggle is
44022 + * saved in the QH for non-control transfers and it's saved in the QTD for
44023 + * control transfers.
44024 + */
44025 +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
44026 + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
44027 +{
44028 + hctsiz_data_t hctsiz;
44029 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44030 +
44031 + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
44032 + dwc_otg_qh_t *qh = hc->qh;
44033 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
44034 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
44035 + } else {
44036 + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
44037 + }
44038 + } else {
44039 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
44040 + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
44041 + } else {
44042 + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
44043 + }
44044 + }
44045 +}
44046 +
44047 +/**
44048 + * Updates the state of an Isochronous URB when the transfer is stopped for
44049 + * any reason. The fields of the current entry in the frame descriptor array
44050 + * are set based on the transfer state and the input _halt_status. Completes
44051 + * the Isochronous URB if all the URB frames have been completed.
44052 + *
44053 + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
44054 + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
44055 + */
44056 +static dwc_otg_halt_status_e
44057 +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
44058 + dwc_hc_t * hc,
44059 + dwc_otg_hc_regs_t * hc_regs,
44060 + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
44061 +{
44062 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44063 + dwc_otg_halt_status_e ret_val = halt_status;
44064 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
44065 +
44066 + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
44067 + switch (halt_status) {
44068 + case DWC_OTG_HC_XFER_COMPLETE:
44069 + frame_desc->status = 0;
44070 + frame_desc->actual_length =
44071 + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
44072 +
44073 + /* non DWORD-aligned buffer case handling. */
44074 + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
44075 + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
44076 + hc->qh->dw_align_buf, frame_desc->actual_length);
44077 + }
44078 +
44079 + break;
44080 + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
44081 + urb->error_count++;
44082 + if (hc->ep_is_in) {
44083 + frame_desc->status = -DWC_E_NO_STREAM_RES;
44084 + } else {
44085 + frame_desc->status = -DWC_E_COMMUNICATION;
44086 + }
44087 + frame_desc->actual_length = 0;
44088 + break;
44089 + case DWC_OTG_HC_XFER_BABBLE_ERR:
44090 + urb->error_count++;
44091 + frame_desc->status = -DWC_E_OVERFLOW;
44092 + /* Don't need to update actual_length in this case. */
44093 + break;
44094 + case DWC_OTG_HC_XFER_XACT_ERR:
44095 + urb->error_count++;
44096 + frame_desc->status = -DWC_E_PROTOCOL;
44097 + frame_desc->actual_length =
44098 + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
44099 +
44100 + /* non DWORD-aligned buffer case handling. */
44101 + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
44102 + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
44103 + hc->qh->dw_align_buf, frame_desc->actual_length);
44104 + }
44105 + /* Skip whole frame */
44106 + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
44107 + hc->ep_is_in && hcd->core_if->dma_enable) {
44108 + qtd->complete_split = 0;
44109 + qtd->isoc_split_offset = 0;
44110 + }
44111 +
44112 + break;
44113 + default:
44114 + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
44115 + break;
44116 + }
44117 + if (++qtd->isoc_frame_index == urb->packet_count) {
44118 + /*
44119 + * urb->status is not used for isoc transfers.
44120 + * The individual frame_desc statuses are used instead.
44121 + */
44122 + hcd->fops->complete(hcd, urb->priv, urb, 0);
44123 + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
44124 + } else {
44125 + ret_val = DWC_OTG_HC_XFER_COMPLETE;
44126 + }
44127 + return ret_val;
44128 +}
44129 +
44130 +/**
44131 + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
44132 + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
44133 + * still linked to the QH, the QH is added to the end of the inactive
44134 + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
44135 + * schedule if no more QTDs are linked to the QH.
44136 + */
44137 +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
44138 +{
44139 + int continue_split = 0;
44140 + dwc_otg_qtd_t *qtd;
44141 +
44142 + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
44143 +
44144 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
44145 +
44146 + if (qtd->complete_split) {
44147 + continue_split = 1;
44148 + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
44149 + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
44150 + continue_split = 1;
44151 + }
44152 +
44153 + if (free_qtd) {
44154 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
44155 + continue_split = 0;
44156 + }
44157 +
44158 + qh->channel = NULL;
44159 + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
44160 +}
44161 +
44162 +/**
44163 + * Releases a host channel for use by other transfers. Attempts to select and
44164 + * queue more transactions since at least one host channel is available.
44165 + *
44166 + * @param hcd The HCD state structure.
44167 + * @param hc The host channel to release.
44168 + * @param qtd The QTD associated with the host channel. This QTD may be freed
44169 + * if the transfer is complete or an error has occurred.
44170 + * @param halt_status Reason the channel is being released. This status
44171 + * determines the actions taken by this function.
44172 + */
44173 +static void release_channel(dwc_otg_hcd_t * hcd,
44174 + dwc_hc_t * hc,
44175 + dwc_otg_qtd_t * qtd,
44176 + dwc_otg_halt_status_e halt_status)
44177 +{
44178 + dwc_otg_transaction_type_e tr_type;
44179 + int free_qtd;
44180 +
44181 + int hog_port = 0;
44182 +
44183 + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
44184 + __func__, hc->hc_num, halt_status, hc->xfer_len);
44185 +
44186 + if(fiq_fsm_enable && hc->do_split) {
44187 + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
44188 + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
44189 + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
44190 + hog_port = 0;
44191 + }
44192 + }
44193 + }
44194 +
44195 + switch (halt_status) {
44196 + case DWC_OTG_HC_XFER_URB_COMPLETE:
44197 + free_qtd = 1;
44198 + break;
44199 + case DWC_OTG_HC_XFER_AHB_ERR:
44200 + case DWC_OTG_HC_XFER_STALL:
44201 + case DWC_OTG_HC_XFER_BABBLE_ERR:
44202 + free_qtd = 1;
44203 + break;
44204 + case DWC_OTG_HC_XFER_XACT_ERR:
44205 + if (qtd->error_count >= 3) {
44206 + DWC_DEBUGPL(DBG_HCDV,
44207 + " Complete URB with transaction error\n");
44208 + free_qtd = 1;
44209 + qtd->urb->status = -DWC_E_PROTOCOL;
44210 + hcd->fops->complete(hcd, qtd->urb->priv,
44211 + qtd->urb, -DWC_E_PROTOCOL);
44212 + } else {
44213 + free_qtd = 0;
44214 + }
44215 + break;
44216 + case DWC_OTG_HC_XFER_URB_DEQUEUE:
44217 + /*
44218 + * The QTD has already been removed and the QH has been
44219 + * deactivated. Don't want to do anything except release the
44220 + * host channel and try to queue more transfers.
44221 + */
44222 + goto cleanup;
44223 + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
44224 + free_qtd = 0;
44225 + break;
44226 + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
44227 + DWC_DEBUGPL(DBG_HCDV,
44228 + " Complete URB with I/O error\n");
44229 + free_qtd = 1;
44230 + qtd->urb->status = -DWC_E_IO;
44231 + hcd->fops->complete(hcd, qtd->urb->priv,
44232 + qtd->urb, -DWC_E_IO);
44233 + break;
44234 + default:
44235 + free_qtd = 0;
44236 + break;
44237 + }
44238 +
44239 + deactivate_qh(hcd, hc->qh, free_qtd);
44240 +
44241 +cleanup:
44242 + /*
44243 + * Release the host channel for use by other transfers. The cleanup
44244 + * function clears the channel interrupt enables and conditions, so
44245 + * there's no need to clear the Channel Halted interrupt separately.
44246 + */
44247 + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
44248 + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
44249 + dwc_otg_hc_cleanup(hcd->core_if, hc);
44250 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
44251 +
44252 + if (!microframe_schedule) {
44253 + switch (hc->ep_type) {
44254 + case DWC_OTG_EP_TYPE_CONTROL:
44255 + case DWC_OTG_EP_TYPE_BULK:
44256 + hcd->non_periodic_channels--;
44257 + break;
44258 +
44259 + default:
44260 + /*
44261 + * Don't release reservations for periodic channels here.
44262 + * That's done when a periodic transfer is descheduled (i.e.
44263 + * when the QH is removed from the periodic schedule).
44264 + */
44265 + break;
44266 + }
44267 + } else {
44268 + hcd->available_host_channels++;
44269 + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
44270 + }
44271 +
44272 + /* Try to queue more transfers now that there's a free channel. */
44273 + tr_type = dwc_otg_hcd_select_transactions(hcd);
44274 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
44275 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
44276 + }
44277 +}
44278 +
44279 +/**
44280 + * Halts a host channel. If the channel cannot be halted immediately because
44281 + * the request queue is full, this function ensures that the FIFO empty
44282 + * interrupt for the appropriate queue is enabled so that the halt request can
44283 + * be queued when there is space in the request queue.
44284 + *
44285 + * This function may also be called in DMA mode. In that case, the channel is
44286 + * simply released since the core always halts the channel automatically in
44287 + * DMA mode.
44288 + */
44289 +static void halt_channel(dwc_otg_hcd_t * hcd,
44290 + dwc_hc_t * hc,
44291 + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
44292 +{
44293 + if (hcd->core_if->dma_enable) {
44294 + release_channel(hcd, hc, qtd, halt_status);
44295 + return;
44296 + }
44297 +
44298 + /* Slave mode processing... */
44299 + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
44300 +
44301 + if (hc->halt_on_queue) {
44302 + gintmsk_data_t gintmsk = {.d32 = 0 };
44303 + dwc_otg_core_global_regs_t *global_regs;
44304 + global_regs = hcd->core_if->core_global_regs;
44305 +
44306 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
44307 + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
44308 + /*
44309 + * Make sure the Non-periodic Tx FIFO empty interrupt
44310 + * is enabled so that the non-periodic schedule will
44311 + * be processed.
44312 + */
44313 + gintmsk.b.nptxfempty = 1;
44314 + if (fiq_enable) {
44315 + local_fiq_disable();
44316 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
44317 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44318 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
44319 + local_fiq_enable();
44320 + } else {
44321 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44322 + }
44323 + } else {
44324 + /*
44325 + * Move the QH from the periodic queued schedule to
44326 + * the periodic assigned schedule. This allows the
44327 + * halt to be queued when the periodic schedule is
44328 + * processed.
44329 + */
44330 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
44331 + &hc->qh->qh_list_entry);
44332 +
44333 + /*
44334 + * Make sure the Periodic Tx FIFO Empty interrupt is
44335 + * enabled so that the periodic schedule will be
44336 + * processed.
44337 + */
44338 + gintmsk.b.ptxfempty = 1;
44339 + if (fiq_enable) {
44340 + local_fiq_disable();
44341 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
44342 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44343 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
44344 + local_fiq_enable();
44345 + } else {
44346 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44347 + }
44348 + }
44349 + }
44350 +}
44351 +
44352 +/**
44353 + * Performs common cleanup for non-periodic transfers after a Transfer
44354 + * Complete interrupt. This function should be called after any endpoint type
44355 + * specific handling is finished to release the host channel.
44356 + */
44357 +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
44358 + dwc_hc_t * hc,
44359 + dwc_otg_hc_regs_t * hc_regs,
44360 + dwc_otg_qtd_t * qtd,
44361 + dwc_otg_halt_status_e halt_status)
44362 +{
44363 + hcint_data_t hcint;
44364 +
44365 + qtd->error_count = 0;
44366 +
44367 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
44368 + if (hcint.b.nyet) {
44369 + /*
44370 + * Got a NYET on the last transaction of the transfer. This
44371 + * means that the endpoint should be in the PING state at the
44372 + * beginning of the next transfer.
44373 + */
44374 + hc->qh->ping_state = 1;
44375 + clear_hc_int(hc_regs, nyet);
44376 + }
44377 +
44378 + /*
44379 + * Always halt and release the host channel to make it available for
44380 + * more transfers. There may still be more phases for a control
44381 + * transfer or more data packets for a bulk transfer at this point,
44382 + * but the host channel is still halted. A channel will be reassigned
44383 + * to the transfer when the non-periodic schedule is processed after
44384 + * the channel is released. This allows transactions to be queued
44385 + * properly via dwc_otg_hcd_queue_transactions, which also enables the
44386 + * Tx FIFO Empty interrupt if necessary.
44387 + */
44388 + if (hc->ep_is_in) {
44389 + /*
44390 + * IN transfers in Slave mode require an explicit disable to
44391 + * halt the channel. (In DMA mode, this call simply releases
44392 + * the channel.)
44393 + */
44394 + halt_channel(hcd, hc, qtd, halt_status);
44395 + } else {
44396 + /*
44397 + * The channel is automatically disabled by the core for OUT
44398 + * transfers in Slave mode.
44399 + */
44400 + release_channel(hcd, hc, qtd, halt_status);
44401 + }
44402 +}
44403 +
44404 +/**
44405 + * Performs common cleanup for periodic transfers after a Transfer Complete
44406 + * interrupt. This function should be called after any endpoint type specific
44407 + * handling is finished to release the host channel.
44408 + */
44409 +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
44410 + dwc_hc_t * hc,
44411 + dwc_otg_hc_regs_t * hc_regs,
44412 + dwc_otg_qtd_t * qtd,
44413 + dwc_otg_halt_status_e halt_status)
44414 +{
44415 + hctsiz_data_t hctsiz;
44416 + qtd->error_count = 0;
44417 +
44418 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44419 + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
44420 + /* Core halts channel in these cases. */
44421 + release_channel(hcd, hc, qtd, halt_status);
44422 + } else {
44423 + /* Flush any outstanding requests from the Tx queue. */
44424 + halt_channel(hcd, hc, qtd, halt_status);
44425 + }
44426 +}
44427 +
44428 +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
44429 + dwc_hc_t * hc,
44430 + dwc_otg_hc_regs_t * hc_regs,
44431 + dwc_otg_qtd_t * qtd)
44432 +{
44433 + uint32_t len;
44434 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
44435 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
44436 +
44437 + len = get_actual_xfer_length(hc, hc_regs, qtd,
44438 + DWC_OTG_HC_XFER_COMPLETE, NULL);
44439 +
44440 + if (!len) {
44441 + qtd->complete_split = 0;
44442 + qtd->isoc_split_offset = 0;
44443 + return 0;
44444 + }
44445 + frame_desc->actual_length += len;
44446 +
44447 + if (hc->align_buff && len)
44448 + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
44449 + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
44450 + qtd->isoc_split_offset += len;
44451 +
44452 + if (frame_desc->length == frame_desc->actual_length) {
44453 + frame_desc->status = 0;
44454 + qtd->isoc_frame_index++;
44455 + qtd->complete_split = 0;
44456 + qtd->isoc_split_offset = 0;
44457 + }
44458 +
44459 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
44460 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
44461 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
44462 + } else {
44463 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
44464 + }
44465 +
44466 + return 1; /* Indicates that channel released */
44467 +}
44468 +
44469 +/**
44470 + * Handles a host channel Transfer Complete interrupt. This handler may be
44471 + * called in either DMA mode or Slave mode.
44472 + */
44473 +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
44474 + dwc_hc_t * hc,
44475 + dwc_otg_hc_regs_t * hc_regs,
44476 + dwc_otg_qtd_t * qtd)
44477 +{
44478 + int urb_xfer_done;
44479 + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
44480 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44481 + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
44482 +
44483 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44484 + "Transfer Complete--\n", hc->hc_num);
44485 +
44486 + if (hcd->core_if->dma_desc_enable) {
44487 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
44488 + if (pipe_type == UE_ISOCHRONOUS) {
44489 + /* Do not disable the interrupt, just clear it */
44490 + clear_hc_int(hc_regs, xfercomp);
44491 + return 1;
44492 + }
44493 + goto handle_xfercomp_done;
44494 + }
44495 +
44496 + /*
44497 + * Handle xfer complete on CSPLIT.
44498 + */
44499 +
44500 + if (hc->qh->do_split) {
44501 + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
44502 + && hcd->core_if->dma_enable) {
44503 + if (qtd->complete_split
44504 + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
44505 + qtd))
44506 + goto handle_xfercomp_done;
44507 + } else {
44508 + qtd->complete_split = 0;
44509 + }
44510 + }
44511 +
44512 + /* Update the QTD and URB states. */
44513 + switch (pipe_type) {
44514 + case UE_CONTROL:
44515 + switch (qtd->control_phase) {
44516 + case DWC_OTG_CONTROL_SETUP:
44517 + if (urb->length > 0) {
44518 + qtd->control_phase = DWC_OTG_CONTROL_DATA;
44519 + } else {
44520 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
44521 + }
44522 + DWC_DEBUGPL(DBG_HCDV,
44523 + " Control setup transaction done\n");
44524 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44525 + break;
44526 + case DWC_OTG_CONTROL_DATA:{
44527 + urb_xfer_done =
44528 + update_urb_state_xfer_comp(hc, hc_regs, urb,
44529 + qtd);
44530 + if (urb_xfer_done) {
44531 + qtd->control_phase =
44532 + DWC_OTG_CONTROL_STATUS;
44533 + DWC_DEBUGPL(DBG_HCDV,
44534 + " Control data transfer done\n");
44535 + } else {
44536 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44537 + }
44538 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44539 + break;
44540 + }
44541 + case DWC_OTG_CONTROL_STATUS:
44542 + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
44543 + if (urb->status == -DWC_E_IN_PROGRESS) {
44544 + urb->status = 0;
44545 + }
44546 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44547 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44548 + break;
44549 + }
44550 +
44551 + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44552 + break;
44553 + case UE_BULK:
44554 + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
44555 + urb_xfer_done =
44556 + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
44557 + if (urb_xfer_done) {
44558 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44559 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44560 + } else {
44561 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44562 + }
44563 +
44564 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44565 + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44566 + break;
44567 + case UE_INTERRUPT:
44568 + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
44569 + urb_xfer_done =
44570 + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
44571 +
44572 + /*
44573 + * Interrupt URB is done on the first transfer complete
44574 + * interrupt.
44575 + */
44576 + if (urb_xfer_done) {
44577 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44578 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44579 + } else {
44580 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44581 + }
44582 +
44583 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44584 + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44585 + break;
44586 + case UE_ISOCHRONOUS:
44587 + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
44588 + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
44589 + halt_status =
44590 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44591 + DWC_OTG_HC_XFER_COMPLETE);
44592 + }
44593 + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44594 + break;
44595 + }
44596 +
44597 +handle_xfercomp_done:
44598 + disable_hc_int(hc_regs, xfercompl);
44599 +
44600 + return 1;
44601 +}
44602 +
44603 +/**
44604 + * Handles a host channel STALL interrupt. This handler may be called in
44605 + * either DMA mode or Slave mode.
44606 + */
44607 +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
44608 + dwc_hc_t * hc,
44609 + dwc_otg_hc_regs_t * hc_regs,
44610 + dwc_otg_qtd_t * qtd)
44611 +{
44612 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44613 + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
44614 +
44615 + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
44616 + "STALL Received--\n", hc->hc_num);
44617 +
44618 + if (hcd->core_if->dma_desc_enable) {
44619 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
44620 + goto handle_stall_done;
44621 + }
44622 +
44623 + if (pipe_type == UE_CONTROL) {
44624 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
44625 + }
44626 +
44627 + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
44628 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
44629 + /*
44630 + * USB protocol requires resetting the data toggle for bulk
44631 + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
44632 + * setup command is issued to the endpoint. Anticipate the
44633 + * CLEAR_FEATURE command since a STALL has occurred and reset
44634 + * the data toggle now.
44635 + */
44636 + hc->qh->data_toggle = 0;
44637 + }
44638 +
44639 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
44640 +
44641 +handle_stall_done:
44642 + disable_hc_int(hc_regs, stall);
44643 +
44644 + return 1;
44645 +}
44646 +
44647 +/*
44648 + * Updates the state of the URB when a transfer has been stopped due to an
44649 + * abnormal condition before the transfer completes. Modifies the
44650 + * actual_length field of the URB to reflect the number of bytes that have
44651 + * actually been transferred via the host channel.
44652 + */
44653 +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
44654 + dwc_otg_hc_regs_t * hc_regs,
44655 + dwc_otg_hcd_urb_t * urb,
44656 + dwc_otg_qtd_t * qtd,
44657 + dwc_otg_halt_status_e halt_status)
44658 +{
44659 + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
44660 + halt_status, NULL);
44661 +
44662 + if (urb->actual_length + bytes_transferred > urb->length) {
44663 + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
44664 + hc->dev_addr, __func__, __LINE__);
44665 + bytes_transferred = urb->length - urb->actual_length;
44666 + }
44667 +
44668 + /* non DWORD-aligned buffer case handling. */
44669 + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
44670 + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
44671 + bytes_transferred);
44672 + }
44673 +
44674 + urb->actual_length += bytes_transferred;
44675 +
44676 +#ifdef DEBUG
44677 + {
44678 + hctsiz_data_t hctsiz;
44679 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44680 + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
44681 + __func__, (hc->ep_is_in ? "IN" : "OUT"),
44682 + hc->hc_num);
44683 + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
44684 + hc->start_pkt_count);
44685 + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
44686 + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
44687 + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
44688 + bytes_transferred);
44689 + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
44690 + urb->actual_length);
44691 + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
44692 + urb->length);
44693 + }
44694 +#endif
44695 +}
44696 +
44697 +/**
44698 + * Handles a host channel NAK interrupt. This handler may be called in either
44699 + * DMA mode or Slave mode.
44700 + */
44701 +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
44702 + dwc_hc_t * hc,
44703 + dwc_otg_hc_regs_t * hc_regs,
44704 + dwc_otg_qtd_t * qtd)
44705 +{
44706 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44707 + "NAK Received--\n", hc->hc_num);
44708 +
44709 + /*
44710 + * When we get bulk NAKs then remember this so we holdoff on this qh until
44711 + * the beginning of the next frame
44712 + */
44713 + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44714 + case UE_BULK:
44715 + case UE_CONTROL:
44716 + if (nak_holdoff && qtd->qh->do_split)
44717 + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
44718 + }
44719 +
44720 + /*
44721 + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
44722 + * interrupt. Re-start the SSPLIT transfer.
44723 + */
44724 + if (hc->do_split) {
44725 + if (hc->complete_split) {
44726 + qtd->error_count = 0;
44727 + }
44728 + qtd->complete_split = 0;
44729 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44730 + goto handle_nak_done;
44731 + }
44732 +
44733 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44734 + case UE_CONTROL:
44735 + case UE_BULK:
44736 + if (hcd->core_if->dma_enable && hc->ep_is_in) {
44737 + /*
44738 + * NAK interrupts are enabled on bulk/control IN
44739 + * transfers in DMA mode for the sole purpose of
44740 + * resetting the error count after a transaction error
44741 + * occurs. The core will continue transferring data.
44742 + * Disable other interrupts unmasked for the same
44743 + * reason.
44744 + */
44745 + disable_hc_int(hc_regs, datatglerr);
44746 + disable_hc_int(hc_regs, ack);
44747 + qtd->error_count = 0;
44748 + goto handle_nak_done;
44749 + }
44750 +
44751 + /*
44752 + * NAK interrupts normally occur during OUT transfers in DMA
44753 + * or Slave mode. For IN transfers, more requests will be
44754 + * queued as request queue space is available.
44755 + */
44756 + qtd->error_count = 0;
44757 +
44758 + if (!hc->qh->ping_state) {
44759 + update_urb_state_xfer_intr(hc, hc_regs,
44760 + qtd->urb, qtd,
44761 + DWC_OTG_HC_XFER_NAK);
44762 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44763 +
44764 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
44765 + hc->qh->ping_state = 1;
44766 + }
44767 +
44768 + /*
44769 + * Halt the channel so the transfer can be re-started from
44770 + * the appropriate point or the PING protocol will
44771 + * start/continue.
44772 + */
44773 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44774 + break;
44775 + case UE_INTERRUPT:
44776 + qtd->error_count = 0;
44777 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44778 + break;
44779 + case UE_ISOCHRONOUS:
44780 + /* Should never get called for isochronous transfers. */
44781 + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
44782 + break;
44783 + }
44784 +
44785 +handle_nak_done:
44786 + disable_hc_int(hc_regs, nak);
44787 +
44788 + return 1;
44789 +}
44790 +
44791 +/**
44792 + * Handles a host channel ACK interrupt. This interrupt is enabled when
44793 + * performing the PING protocol in Slave mode, when errors occur during
44794 + * either Slave mode or DMA mode, and during Start Split transactions.
44795 + */
44796 +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
44797 + dwc_hc_t * hc,
44798 + dwc_otg_hc_regs_t * hc_regs,
44799 + dwc_otg_qtd_t * qtd)
44800 +{
44801 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44802 + "ACK Received--\n", hc->hc_num);
44803 +
44804 + if (hc->do_split) {
44805 + /*
44806 + * Handle ACK on SSPLIT.
44807 + * ACK should not occur in CSPLIT.
44808 + */
44809 + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
44810 + qtd->ssplit_out_xfer_count = hc->xfer_len;
44811 + }
44812 + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
44813 + /* Don't need complete for isochronous out transfers. */
44814 + qtd->complete_split = 1;
44815 + }
44816 +
44817 + /* ISOC OUT */
44818 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
44819 + switch (hc->xact_pos) {
44820 + case DWC_HCSPLIT_XACTPOS_ALL:
44821 + break;
44822 + case DWC_HCSPLIT_XACTPOS_END:
44823 + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
44824 + qtd->isoc_split_offset = 0;
44825 + break;
44826 + case DWC_HCSPLIT_XACTPOS_BEGIN:
44827 + case DWC_HCSPLIT_XACTPOS_MID:
44828 + /*
44829 + * For BEGIN or MID, calculate the length for
44830 + * the next microframe to determine the correct
44831 + * SSPLIT token, either MID or END.
44832 + */
44833 + {
44834 + struct dwc_otg_hcd_iso_packet_desc
44835 + *frame_desc;
44836 +
44837 + frame_desc =
44838 + &qtd->urb->
44839 + iso_descs[qtd->isoc_frame_index];
44840 + qtd->isoc_split_offset += 188;
44841 +
44842 + if ((frame_desc->length -
44843 + qtd->isoc_split_offset) <= 188) {
44844 + qtd->isoc_split_pos =
44845 + DWC_HCSPLIT_XACTPOS_END;
44846 + } else {
44847 + qtd->isoc_split_pos =
44848 + DWC_HCSPLIT_XACTPOS_MID;
44849 + }
44850 +
44851 + }
44852 + break;
44853 + }
44854 + } else {
44855 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
44856 + }
44857 + } else {
44858 + /*
44859 + * An unmasked ACK on a non-split DMA transaction is
44860 + * for the sole purpose of resetting error counts. Disable other
44861 + * interrupts unmasked for the same reason.
44862 + */
44863 + if(hcd->core_if->dma_enable) {
44864 + disable_hc_int(hc_regs, datatglerr);
44865 + disable_hc_int(hc_regs, nak);
44866 + }
44867 + qtd->error_count = 0;
44868 +
44869 + if (hc->qh->ping_state) {
44870 + hc->qh->ping_state = 0;
44871 + /*
44872 + * Halt the channel so the transfer can be re-started
44873 + * from the appropriate point. This only happens in
44874 + * Slave mode. In DMA mode, the ping_state is cleared
44875 + * when the transfer is started because the core
44876 + * automatically executes the PING, then the transfer.
44877 + */
44878 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
44879 + }
44880 + }
44881 +
44882 + /*
44883 + * If the ACK occurred when _not_ in the PING state, let the channel
44884 + * continue transferring data after clearing the error count.
44885 + */
44886 +
44887 + disable_hc_int(hc_regs, ack);
44888 +
44889 + return 1;
44890 +}
44891 +
44892 +/**
44893 + * Handles a host channel NYET interrupt. This interrupt should only occur on
44894 + * Bulk and Control OUT endpoints and for complete split transactions. If a
44895 + * NYET occurs at the same time as a Transfer Complete interrupt, it is
44896 + * handled in the xfercomp interrupt handler, not here. This handler may be
44897 + * called in either DMA mode or Slave mode.
44898 + */
44899 +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
44900 + dwc_hc_t * hc,
44901 + dwc_otg_hc_regs_t * hc_regs,
44902 + dwc_otg_qtd_t * qtd)
44903 +{
44904 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44905 + "NYET Received--\n", hc->hc_num);
44906 +
44907 + /*
44908 + * NYET on CSPLIT
44909 + * re-do the CSPLIT immediately on non-periodic
44910 + */
44911 + if (hc->do_split && hc->complete_split) {
44912 + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
44913 + && hcd->core_if->dma_enable) {
44914 + qtd->complete_split = 0;
44915 + qtd->isoc_split_offset = 0;
44916 + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
44917 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
44918 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
44919 + }
44920 + else
44921 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
44922 + goto handle_nyet_done;
44923 + }
44924 +
44925 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
44926 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
44927 + int frnum = dwc_otg_hcd_get_frame_number(hcd);
44928 +
44929 + // With the FIQ running we only ever see the failed NYET
44930 + if (dwc_full_frame_num(frnum) !=
44931 + dwc_full_frame_num(hc->qh->sched_frame) ||
44932 + fiq_fsm_enable) {
44933 + /*
44934 + * No longer in the same full speed frame.
44935 + * Treat this as a transaction error.
44936 + */
44937 +#if 0
44938 + /** @todo Fix system performance so this can
44939 + * be treated as an error. Right now complete
44940 + * splits cannot be scheduled precisely enough
44941 + * due to other system activity, so this error
44942 + * occurs regularly in Slave mode.
44943 + */
44944 + qtd->error_count++;
44945 +#endif
44946 + qtd->complete_split = 0;
44947 + halt_channel(hcd, hc, qtd,
44948 + DWC_OTG_HC_XFER_XACT_ERR);
44949 + /** @todo add support for isoc release */
44950 + goto handle_nyet_done;
44951 + }
44952 + }
44953 +
44954 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
44955 + goto handle_nyet_done;
44956 + }
44957 +
44958 + hc->qh->ping_state = 1;
44959 + qtd->error_count = 0;
44960 +
44961 + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
44962 + DWC_OTG_HC_XFER_NYET);
44963 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44964 +
44965 + /*
44966 + * Halt the channel and re-start the transfer so the PING
44967 + * protocol will start.
44968 + */
44969 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
44970 +
44971 +handle_nyet_done:
44972 + disable_hc_int(hc_regs, nyet);
44973 + return 1;
44974 +}
44975 +
44976 +/**
44977 + * Handles a host channel babble interrupt. This handler may be called in
44978 + * either DMA mode or Slave mode.
44979 + */
44980 +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
44981 + dwc_hc_t * hc,
44982 + dwc_otg_hc_regs_t * hc_regs,
44983 + dwc_otg_qtd_t * qtd)
44984 +{
44985 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44986 + "Babble Error--\n", hc->hc_num);
44987 +
44988 + if (hcd->core_if->dma_desc_enable) {
44989 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44990 + DWC_OTG_HC_XFER_BABBLE_ERR);
44991 + goto handle_babble_done;
44992 + }
44993 +
44994 + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
44995 + hcd->fops->complete(hcd, qtd->urb->priv,
44996 + qtd->urb, -DWC_E_OVERFLOW);
44997 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
44998 + } else {
44999 + dwc_otg_halt_status_e halt_status;
45000 + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
45001 + DWC_OTG_HC_XFER_BABBLE_ERR);
45002 + halt_channel(hcd, hc, qtd, halt_status);
45003 + }
45004 +
45005 +handle_babble_done:
45006 + disable_hc_int(hc_regs, bblerr);
45007 + return 1;
45008 +}
45009 +
45010 +/**
45011 + * Handles a host channel AHB error interrupt. This handler is only called in
45012 + * DMA mode.
45013 + */
45014 +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
45015 + dwc_hc_t * hc,
45016 + dwc_otg_hc_regs_t * hc_regs,
45017 + dwc_otg_qtd_t * qtd)
45018 +{
45019 + hcchar_data_t hcchar;
45020 + hcsplt_data_t hcsplt;
45021 + hctsiz_data_t hctsiz;
45022 + uint32_t hcdma;
45023 + char *pipetype, *speed;
45024 +
45025 + dwc_otg_hcd_urb_t *urb = qtd->urb;
45026 +
45027 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45028 + "AHB Error--\n", hc->hc_num);
45029 +
45030 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
45031 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
45032 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
45033 + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
45034 +
45035 + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
45036 + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
45037 + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
45038 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
45039 + DWC_ERROR(" Device address: %d\n",
45040 + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
45041 + DWC_ERROR(" Endpoint: %d, %s\n",
45042 + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
45043 + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
45044 +
45045 + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
45046 + case UE_CONTROL:
45047 + pipetype = "CONTROL";
45048 + break;
45049 + case UE_BULK:
45050 + pipetype = "BULK";
45051 + break;
45052 + case UE_INTERRUPT:
45053 + pipetype = "INTERRUPT";
45054 + break;
45055 + case UE_ISOCHRONOUS:
45056 + pipetype = "ISOCHRONOUS";
45057 + break;
45058 + default:
45059 + pipetype = "UNKNOWN";
45060 + break;
45061 + }
45062 +
45063 + DWC_ERROR(" Endpoint type: %s\n", pipetype);
45064 +
45065 + switch (hc->speed) {
45066 + case DWC_OTG_EP_SPEED_HIGH:
45067 + speed = "HIGH";
45068 + break;
45069 + case DWC_OTG_EP_SPEED_FULL:
45070 + speed = "FULL";
45071 + break;
45072 + case DWC_OTG_EP_SPEED_LOW:
45073 + speed = "LOW";
45074 + break;
45075 + default:
45076 + speed = "UNKNOWN";
45077 + break;
45078 + };
45079 +
45080 + DWC_ERROR(" Speed: %s\n", speed);
45081 +
45082 + DWC_ERROR(" Max packet size: %d\n",
45083 + dwc_otg_hcd_get_mps(&urb->pipe_info));
45084 + DWC_ERROR(" Data buffer length: %d\n", urb->length);
45085 + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
45086 + urb->buf, (void *)urb->dma);
45087 + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
45088 + urb->setup_packet, (void *)urb->setup_dma);
45089 + DWC_ERROR(" Interval: %d\n", urb->interval);
45090 +
45091 + /* Core haltes the channel for Descriptor DMA mode */
45092 + if (hcd->core_if->dma_desc_enable) {
45093 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
45094 + DWC_OTG_HC_XFER_AHB_ERR);
45095 + goto handle_ahberr_done;
45096 + }
45097 +
45098 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
45099 +
45100 + /*
45101 + * Force a channel halt. Don't call halt_channel because that won't
45102 + * write to the HCCHARn register in DMA mode to force the halt.
45103 + */
45104 + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
45105 +handle_ahberr_done:
45106 + disable_hc_int(hc_regs, ahberr);
45107 + return 1;
45108 +}
45109 +
45110 +/**
45111 + * Handles a host channel transaction error interrupt. This handler may be
45112 + * called in either DMA mode or Slave mode.
45113 + */
45114 +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
45115 + dwc_hc_t * hc,
45116 + dwc_otg_hc_regs_t * hc_regs,
45117 + dwc_otg_qtd_t * qtd)
45118 +{
45119 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45120 + "Transaction Error--\n", hc->hc_num);
45121 +
45122 + if (hcd->core_if->dma_desc_enable) {
45123 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
45124 + DWC_OTG_HC_XFER_XACT_ERR);
45125 + goto handle_xacterr_done;
45126 + }
45127 +
45128 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
45129 + case UE_CONTROL:
45130 + case UE_BULK:
45131 + qtd->error_count++;
45132 + if (!hc->qh->ping_state) {
45133 +
45134 + update_urb_state_xfer_intr(hc, hc_regs,
45135 + qtd->urb, qtd,
45136 + DWC_OTG_HC_XFER_XACT_ERR);
45137 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45138 + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
45139 + hc->qh->ping_state = 1;
45140 + }
45141 + }
45142 +
45143 + /*
45144 + * Halt the channel so the transfer can be re-started from
45145 + * the appropriate point or the PING protocol will start.
45146 + */
45147 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45148 + break;
45149 + case UE_INTERRUPT:
45150 + qtd->error_count++;
45151 + if (hc->do_split && hc->complete_split) {
45152 + qtd->complete_split = 0;
45153 + }
45154 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45155 + break;
45156 + case UE_ISOCHRONOUS:
45157 + {
45158 + dwc_otg_halt_status_e halt_status;
45159 + halt_status =
45160 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
45161 + DWC_OTG_HC_XFER_XACT_ERR);
45162 +
45163 + halt_channel(hcd, hc, qtd, halt_status);
45164 + }
45165 + break;
45166 + }
45167 +handle_xacterr_done:
45168 + disable_hc_int(hc_regs, xacterr);
45169 +
45170 + return 1;
45171 +}
45172 +
45173 +/**
45174 + * Handles a host channel frame overrun interrupt. This handler may be called
45175 + * in either DMA mode or Slave mode.
45176 + */
45177 +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
45178 + dwc_hc_t * hc,
45179 + dwc_otg_hc_regs_t * hc_regs,
45180 + dwc_otg_qtd_t * qtd)
45181 +{
45182 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45183 + "Frame Overrun--\n", hc->hc_num);
45184 +
45185 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
45186 + case UE_CONTROL:
45187 + case UE_BULK:
45188 + break;
45189 + case UE_INTERRUPT:
45190 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
45191 + break;
45192 + case UE_ISOCHRONOUS:
45193 + {
45194 + dwc_otg_halt_status_e halt_status;
45195 + halt_status =
45196 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
45197 + DWC_OTG_HC_XFER_FRAME_OVERRUN);
45198 +
45199 + halt_channel(hcd, hc, qtd, halt_status);
45200 + }
45201 + break;
45202 + }
45203 +
45204 + disable_hc_int(hc_regs, frmovrun);
45205 +
45206 + return 1;
45207 +}
45208 +
45209 +/**
45210 + * Handles a host channel data toggle error interrupt. This handler may be
45211 + * called in either DMA mode or Slave mode.
45212 + */
45213 +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
45214 + dwc_hc_t * hc,
45215 + dwc_otg_hc_regs_t * hc_regs,
45216 + dwc_otg_qtd_t * qtd)
45217 +{
45218 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45219 + "Data Toggle Error on %s transfer--\n",
45220 + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
45221 +
45222 + /* Data toggles on split transactions cause the hc to halt.
45223 + * restart transfer */
45224 + if(hc->qh->do_split)
45225 + {
45226 + qtd->error_count++;
45227 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45228 + update_urb_state_xfer_intr(hc, hc_regs,
45229 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45230 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45231 + } else if (hc->ep_is_in) {
45232 + /* An unmasked data toggle error on a non-split DMA transaction is
45233 + * for the sole purpose of resetting error counts. Disable other
45234 + * interrupts unmasked for the same reason.
45235 + */
45236 + if(hcd->core_if->dma_enable) {
45237 + disable_hc_int(hc_regs, ack);
45238 + disable_hc_int(hc_regs, nak);
45239 + }
45240 + qtd->error_count = 0;
45241 + }
45242 +
45243 + disable_hc_int(hc_regs, datatglerr);
45244 +
45245 + return 1;
45246 +}
45247 +
45248 +#ifdef DEBUG
45249 +/**
45250 + * This function is for debug only. It checks that a valid halt status is set
45251 + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
45252 + * taken and a warning is issued.
45253 + * @return 1 if halt status is ok, 0 otherwise.
45254 + */
45255 +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
45256 + dwc_hc_t * hc,
45257 + dwc_otg_hc_regs_t * hc_regs,
45258 + dwc_otg_qtd_t * qtd)
45259 +{
45260 + hcchar_data_t hcchar;
45261 + hctsiz_data_t hctsiz;
45262 + hcint_data_t hcint;
45263 + hcintmsk_data_t hcintmsk;
45264 + hcsplt_data_t hcsplt;
45265 +
45266 + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
45267 + /*
45268 + * This code is here only as a check. This condition should
45269 + * never happen. Ignore the halt if it does occur.
45270 + */
45271 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
45272 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
45273 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45274 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45275 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
45276 + DWC_WARN
45277 + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
45278 + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
45279 + "hcint 0x%08x, hcintmsk 0x%08x, "
45280 + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
45281 + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
45282 + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
45283 +
45284 + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
45285 + __func__, hc->hc_num);
45286 + DWC_WARN("\n");
45287 + clear_hc_int(hc_regs, chhltd);
45288 + return 0;
45289 + }
45290 +
45291 + /*
45292 + * This code is here only as a check. hcchar.chdis should
45293 + * never be set when the halt interrupt occurs. Halt the
45294 + * channel again if it does occur.
45295 + */
45296 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
45297 + if (hcchar.b.chdis) {
45298 + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
45299 + "hcchar 0x%08x, trying to halt again\n",
45300 + __func__, hcchar.d32);
45301 + clear_hc_int(hc_regs, chhltd);
45302 + hc->halt_pending = 0;
45303 + halt_channel(hcd, hc, qtd, hc->halt_status);
45304 + return 0;
45305 + }
45306 +
45307 + return 1;
45308 +}
45309 +#endif
45310 +
45311 +/**
45312 + * Handles a host Channel Halted interrupt in DMA mode. This handler
45313 + * determines the reason the channel halted and proceeds accordingly.
45314 + */
45315 +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
45316 + dwc_hc_t * hc,
45317 + dwc_otg_hc_regs_t * hc_regs,
45318 + dwc_otg_qtd_t * qtd)
45319 +{
45320 + int out_nak_enh = 0;
45321 + hcint_data_t hcint;
45322 + hcintmsk_data_t hcintmsk;
45323 + /* For core with OUT NAK enhancement, the flow for high-
45324 + * speed CONTROL/BULK OUT is handled a little differently.
45325 + */
45326 + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
45327 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
45328 + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
45329 + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
45330 + out_nak_enh = 1;
45331 + }
45332 + }
45333 +
45334 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
45335 + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
45336 + && !hcd->core_if->dma_desc_enable)) {
45337 + /*
45338 + * Just release the channel. A dequeue can happen on a
45339 + * transfer timeout. In the case of an AHB Error, the channel
45340 + * was forced to halt because there's no way to gracefully
45341 + * recover.
45342 + */
45343 + if (hcd->core_if->dma_desc_enable)
45344 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
45345 + hc->halt_status);
45346 + else
45347 + release_channel(hcd, hc, qtd, hc->halt_status);
45348 + return;
45349 + }
45350 +
45351 + /* Read the HCINTn register to determine the cause for the halt. */
45352 +
45353 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45354 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45355 +
45356 + if (hcint.b.xfercomp) {
45357 + /** @todo This is here because of a possible hardware bug. Spec
45358 + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
45359 + * interrupt w/ACK bit set should occur, but I only see the
45360 + * XFERCOMP bit, even with it masked out. This is a workaround
45361 + * for that behavior. Should fix this when hardware is fixed.
45362 + */
45363 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
45364 + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
45365 + }
45366 + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
45367 + } else if (hcint.b.stall) {
45368 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45369 + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
45370 + if (out_nak_enh) {
45371 + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
45372 + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
45373 + qtd->error_count = 0;
45374 + } else {
45375 + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
45376 + }
45377 + }
45378 +
45379 + /*
45380 + * Must handle xacterr before nak or ack. Could get a xacterr
45381 + * at the same time as either of these on a BULK/CONTROL OUT
45382 + * that started with a PING. The xacterr takes precedence.
45383 + */
45384 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45385 + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
45386 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45387 + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
45388 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45389 + } else if (hcint.b.bblerr) {
45390 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45391 + } else if (hcint.b.frmovrun) {
45392 + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
45393 + } else if (hcint.b.datatglerr) {
45394 + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
45395 + } else if (!out_nak_enh) {
45396 + if (hcint.b.nyet) {
45397 + /*
45398 + * Must handle nyet before nak or ack. Could get a nyet at the
45399 + * same time as either of those on a BULK/CONTROL OUT that
45400 + * started with a PING. The nyet takes precedence.
45401 + */
45402 + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
45403 + } else if (hcint.b.nak && !hcintmsk.b.nak) {
45404 + /*
45405 + * If nak is not masked, it's because a non-split IN transfer
45406 + * is in an error state. In that case, the nak is handled by
45407 + * the nak interrupt handler, not here. Handle nak here for
45408 + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
45409 + * rewinding the buffer pointer.
45410 + */
45411 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45412 + } else if (hcint.b.ack && !hcintmsk.b.ack) {
45413 + /*
45414 + * If ack is not masked, it's because a non-split IN transfer
45415 + * is in an error state. In that case, the ack is handled by
45416 + * the ack interrupt handler, not here. Handle ack here for
45417 + * split transfers. Start splits halt on ACK.
45418 + */
45419 + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
45420 + } else {
45421 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
45422 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
45423 + /*
45424 + * A periodic transfer halted with no other channel
45425 + * interrupts set. Assume it was halted by the core
45426 + * because it could not be completed in its scheduled
45427 + * (micro)frame.
45428 + */
45429 +#ifdef DEBUG
45430 + DWC_PRINTF
45431 + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
45432 + __func__, hc->hc_num);
45433 +#endif
45434 + halt_channel(hcd, hc, qtd,
45435 + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
45436 + } else {
45437 + DWC_ERROR
45438 + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
45439 + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
45440 + __func__, hc->hc_num, hcint.d32,
45441 + DWC_READ_REG32(&hcd->
45442 + core_if->core_global_regs->
45443 + gintsts));
45444 + /* Failthrough: use 3-strikes rule */
45445 + qtd->error_count++;
45446 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45447 + update_urb_state_xfer_intr(hc, hc_regs,
45448 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45449 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45450 + }
45451 +
45452 + }
45453 + } else {
45454 + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
45455 + hcint.d32);
45456 + /* Failthrough: use 3-strikes rule */
45457 + qtd->error_count++;
45458 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45459 + update_urb_state_xfer_intr(hc, hc_regs,
45460 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45461 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45462 + }
45463 +}
45464 +
45465 +/**
45466 + * Handles a host channel Channel Halted interrupt.
45467 + *
45468 + * In slave mode, this handler is called only when the driver specifically
45469 + * requests a halt. This occurs during handling other host channel interrupts
45470 + * (e.g. nak, xacterr, stall, nyet, etc.).
45471 + *
45472 + * In DMA mode, this is the interrupt that occurs when the core has finished
45473 + * processing a transfer on a channel. Other host channel interrupts (except
45474 + * ahberr) are disabled in DMA mode.
45475 + */
45476 +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
45477 + dwc_hc_t * hc,
45478 + dwc_otg_hc_regs_t * hc_regs,
45479 + dwc_otg_qtd_t * qtd)
45480 +{
45481 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45482 + "Channel Halted--\n", hc->hc_num);
45483 +
45484 + if (hcd->core_if->dma_enable) {
45485 + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
45486 + } else {
45487 +#ifdef DEBUG
45488 + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
45489 + return 1;
45490 + }
45491 +#endif
45492 + release_channel(hcd, hc, qtd, hc->halt_status);
45493 + }
45494 +
45495 + return 1;
45496 +}
45497 +
45498 +
45499 +/**
45500 + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
45501 + * FIQ transfer completion
45502 + * @hcd: Pointer to dwc_otg_hcd struct
45503 + * @num: Host channel number
45504 + *
45505 + * 1. Un-mangle the status as recorded in each iso_frame_desc status
45506 + * 2. Copy it from the dwc_otg_urb into the real URB
45507 + */
45508 +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
45509 +{
45510 + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
45511 + int nr_frames = dwc_urb->packet_count;
45512 + int i;
45513 + hcint_data_t frame_hcint;
45514 +
45515 + for (i = 0; i < nr_frames; i++) {
45516 + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
45517 + if (frame_hcint.b.xfercomp) {
45518 + dwc_urb->iso_descs[i].status = 0;
45519 + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
45520 + } else if (frame_hcint.b.frmovrun) {
45521 + if (qh->ep_is_in)
45522 + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
45523 + else
45524 + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
45525 + dwc_urb->error_count++;
45526 + dwc_urb->iso_descs[i].actual_length = 0;
45527 + } else if (frame_hcint.b.xacterr) {
45528 + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
45529 + dwc_urb->error_count++;
45530 + dwc_urb->iso_descs[i].actual_length = 0;
45531 + } else if (frame_hcint.b.bblerr) {
45532 + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
45533 + dwc_urb->error_count++;
45534 + dwc_urb->iso_descs[i].actual_length = 0;
45535 + } else {
45536 + /* Something went wrong */
45537 + dwc_urb->iso_descs[i].status = -1;
45538 + dwc_urb->iso_descs[i].actual_length = 0;
45539 + dwc_urb->error_count++;
45540 + }
45541 + }
45542 + qh->sched_frame = dwc_frame_num_inc(qh->sched_frame, qh->interval * (nr_frames - 1));
45543 +
45544 + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
45545 + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
45546 +}
45547 +
45548 +/**
45549 + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
45550 + * @hcd: Pointer to dwc_otg_hcd struct
45551 + * @num: Host channel number
45552 + *
45553 + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
45554 + * Returns total length of data or -1 if the buffers were not used.
45555 + *
45556 + */
45557 +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)
45558 +{
45559 + dwc_hc_t *hc = qh->channel;
45560 + struct fiq_dma_blob *blob = hcd->fiq_dmab;
45561 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
45562 + uint8_t *ptr = NULL;
45563 + int index = 0, len = 0;
45564 + int i = 0;
45565 + if (hc->ep_is_in) {
45566 + /* Copy data out of the DMA bounce buffers to the URB's buffer.
45567 + * The align_buf is ignored as this is ignored on FSM enqueue. */
45568 + ptr = qtd->urb->buf;
45569 + if (qh->ep_type == UE_ISOCHRONOUS) {
45570 + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
45571 + index = qtd->isoc_frame_index;
45572 + ptr += qtd->urb->iso_descs[index].offset;
45573 + } else {
45574 + /* Need to increment by actual_length for interrupt IN */
45575 + ptr += qtd->urb->actual_length;
45576 + }
45577 +
45578 + for (i = 0; i < st->dma_info.index; i++) {
45579 + len += st->dma_info.slot_len[i];
45580 + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
45581 + ptr += st->dma_info.slot_len[i];
45582 + }
45583 + return len;
45584 + } else {
45585 + /* OUT endpoints - nothing to do. */
45586 + return -1;
45587 + }
45588 +
45589 +}
45590 +/**
45591 + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
45592 + * from a channel handled in the FIQ
45593 + * @hcd: Pointer to dwc_otg_hcd struct
45594 + * @num: Host channel number
45595 + *
45596 + * If a host channel interrupt was received by the IRQ and this was a channel
45597 + * used by the FIQ, the execution flow for transfer completion is substantially
45598 + * different from the normal (messy) path. This function and its friends handles
45599 + * channel cleanup and transaction completion from a FIQ transaction.
45600 + */
45601 +void dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
45602 +{
45603 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
45604 + dwc_hc_t *hc = hcd->hc_ptr_array[num];
45605 + dwc_otg_qtd_t *qtd;
45606 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
45607 + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
45608 + hctsiz_data_t hctsiz = hcd->fiq_state->channel[num].hctsiz_copy;
45609 + int hostchannels = 0;
45610 + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
45611 +
45612 + hostchannels = hcd->available_host_channels;
45613 + if (hc->halt_pending) {
45614 + /* Dequeue: The FIQ was allowed to complete the transfer but state has been cleared. */
45615 + if (hc->qh && st->fsm == FIQ_NP_SPLIT_DONE &&
45616 + hcint.b.xfercomp && hc->qh->ep_type == UE_BULK) {
45617 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
45618 + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA1;
45619 + } else {
45620 + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA0;
45621 + }
45622 + }
45623 + release_channel(hcd, hc, NULL, hc->halt_status);
45624 + return;
45625 + }
45626 +
45627 + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
45628 + switch (st->fsm) {
45629 + case FIQ_TEST:
45630 + break;
45631 +
45632 + case FIQ_DEQUEUE_ISSUED:
45633 + /* Handled above, but keep for posterity */
45634 + release_channel(hcd, hc, NULL, hc->halt_status);
45635 + break;
45636 +
45637 + case FIQ_NP_SPLIT_DONE:
45638 + /* Nonperiodic transaction complete. */
45639 + if (!hc->ep_is_in) {
45640 + qtd->ssplit_out_xfer_count = hc->xfer_len;
45641 + }
45642 + if (hcint.b.xfercomp) {
45643 + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
45644 + } else if (hcint.b.nak) {
45645 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45646 + } else {
45647 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45648 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45649 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45650 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45651 + }
45652 + break;
45653 +
45654 + case FIQ_NP_SPLIT_HS_ABORTED:
45655 + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
45656 + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
45657 + * because there's no guarantee which order a non-periodic split happened in.
45658 + * We could end up clearing a perfectly good transaction out of the buffer.
45659 + */
45660 + if (hcint.b.xacterr) {
45661 + qtd->error_count += st->nr_errors;
45662 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45663 + } else if (hcint.b.ahberr) {
45664 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45665 + } else {
45666 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45667 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45668 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45669 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45670 + }
45671 + break;
45672 +
45673 + case FIQ_NP_SPLIT_LS_ABORTED:
45674 + /* A few cases can cause this - either an unknown state on a SSPLIT or
45675 + * STALL/data toggle error response on a CSPLIT */
45676 + if (hcint.b.stall) {
45677 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45678 + } else if (hcint.b.datatglerr) {
45679 + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
45680 + } else if (hcint.b.bblerr) {
45681 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45682 + } else if (hcint.b.ahberr) {
45683 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45684 + } else {
45685 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45686 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45687 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45688 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45689 + }
45690 + break;
45691 +
45692 + case FIQ_PER_SPLIT_DONE:
45693 + /* Isoc IN or Interrupt IN/OUT */
45694 +
45695 + /* Flow control here is different from the normal execution by the driver.
45696 + * We need to completely ignore most of the driver's method of handling
45697 + * split transactions and do it ourselves.
45698 + */
45699 + if (hc->ep_type == UE_INTERRUPT) {
45700 + if (hcint.b.nak) {
45701 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45702 + } else if (hc->ep_is_in) {
45703 + int len;
45704 + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
45705 + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
45706 + qtd->urb->actual_length += len;
45707 + if (qtd->urb->actual_length >= qtd->urb->length) {
45708 + qtd->urb->status = 0;
45709 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45710 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45711 + } else {
45712 + /* Interrupt transfer not complete yet - is it a short read? */
45713 + if (len < hc->max_packet) {
45714 + /* Interrupt transaction complete */
45715 + qtd->urb->status = 0;
45716 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45717 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45718 + } else {
45719 + /* Further transactions required */
45720 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45721 + }
45722 + }
45723 + } else {
45724 + /* Interrupt OUT complete. */
45725 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45726 + qtd->urb->actual_length += hc->xfer_len;
45727 + if (qtd->urb->actual_length >= qtd->urb->length) {
45728 + qtd->urb->status = 0;
45729 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45730 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45731 + } else {
45732 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45733 + }
45734 + }
45735 + } else {
45736 + /* ISOC IN complete. */
45737 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45738 + int len = 0;
45739 + /* Record errors, update qtd. */
45740 + if (st->nr_errors) {
45741 + frame_desc->actual_length = 0;
45742 + frame_desc->status = -DWC_E_PROTOCOL;
45743 + } else {
45744 + frame_desc->status = 0;
45745 + /* Unswizzle dma */
45746 + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
45747 + frame_desc->actual_length = len;
45748 + }
45749 + qtd->isoc_frame_index++;
45750 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45751 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45752 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45753 + } else {
45754 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45755 + }
45756 + }
45757 + break;
45758 +
45759 + case FIQ_PER_ISO_OUT_DONE: {
45760 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45761 + /* Record errors, update qtd. */
45762 + if (st->nr_errors) {
45763 + frame_desc->actual_length = 0;
45764 + frame_desc->status = -DWC_E_PROTOCOL;
45765 + } else {
45766 + frame_desc->status = 0;
45767 + frame_desc->actual_length = frame_desc->length;
45768 + }
45769 + qtd->isoc_frame_index++;
45770 + qtd->isoc_split_offset = 0;
45771 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45772 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45773 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45774 + } else {
45775 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45776 + }
45777 + }
45778 + break;
45779 +
45780 + case FIQ_PER_SPLIT_NYET_ABORTED:
45781 + /* Doh. lost the data. */
45782 + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
45783 + "- FIQ reported NYET. Data may have been lost.\n",
45784 + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
45785 + if (hc->ep_type == UE_ISOCHRONOUS) {
45786 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45787 + /* Record errors, update qtd. */
45788 + frame_desc->actual_length = 0;
45789 + frame_desc->status = -DWC_E_PROTOCOL;
45790 + qtd->isoc_frame_index++;
45791 + qtd->isoc_split_offset = 0;
45792 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45793 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45794 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45795 + } else {
45796 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45797 + }
45798 + } else {
45799 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45800 + }
45801 + break;
45802 +
45803 + case FIQ_HS_ISOC_DONE:
45804 + /* The FIQ has performed a whole pile of isochronous transactions.
45805 + * The status is recorded as the interrupt state should the transaction
45806 + * fail.
45807 + */
45808 + dwc_otg_fiq_unmangle_isoc(hcd, hc->qh, qtd, num);
45809 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45810 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45811 + break;
45812 +
45813 + case FIQ_PER_SPLIT_LS_ABORTED:
45814 + if (hcint.b.xacterr) {
45815 + /* Hub has responded with an ERR packet. Device
45816 + * has been unplugged or the port has been disabled.
45817 + * TODO: need to issue a reset to the hub port. */
45818 + qtd->error_count += 3;
45819 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45820 + } else if (hcint.b.stall) {
45821 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45822 + } else if (hcint.b.bblerr) {
45823 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45824 + } else {
45825 + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
45826 + "- FIQ reported FSM=%d. Data may have been lost.\n",
45827 + st->fsm, hc->dev_addr, hc->ep_num);
45828 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45829 + }
45830 + break;
45831 +
45832 + case FIQ_PER_SPLIT_HS_ABORTED:
45833 + /* Either the SSPLIT phase suffered transaction errors or something
45834 + * unexpected happened.
45835 + */
45836 + qtd->error_count += 3;
45837 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45838 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45839 + break;
45840 +
45841 + case FIQ_PER_SPLIT_TIMEOUT:
45842 + /* Couldn't complete in the nominated frame */
45843 + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
45844 + "- FIQ timed out. Data may have been lost.\n",
45845 + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
45846 + if (hc->ep_type == UE_ISOCHRONOUS) {
45847 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45848 + /* Record errors, update qtd. */
45849 + frame_desc->actual_length = 0;
45850 + if (hc->ep_is_in) {
45851 + frame_desc->status = -DWC_E_NO_STREAM_RES;
45852 + } else {
45853 + frame_desc->status = -DWC_E_COMMUNICATION;
45854 + }
45855 + qtd->isoc_frame_index++;
45856 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45857 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45858 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45859 + } else {
45860 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45861 + }
45862 + } else {
45863 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45864 + }
45865 + break;
45866 +
45867 + default:
45868 + DWC_WARN("Unexpected state received on hc=%d fsm=%d on transfer to device %d ep 0x%x",
45869 + hc->hc_num, st->fsm, hc->dev_addr, hc->ep_num);
45870 + qtd->error_count++;
45871 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45872 + }
45873 + return;
45874 +}
45875 +
45876 +/** Handles interrupt for a specific Host Channel */
45877 +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
45878 +{
45879 + int retval = 0;
45880 + hcint_data_t hcint;
45881 + hcintmsk_data_t hcintmsk;
45882 + dwc_hc_t *hc;
45883 + dwc_otg_hc_regs_t *hc_regs;
45884 + dwc_otg_qtd_t *qtd;
45885 +
45886 + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
45887 +
45888 + hc = dwc_otg_hcd->hc_ptr_array[num];
45889 + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
45890 + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
45891 + /* A dequeue was issued for this transfer. Our QTD has gone away
45892 + * but in the case of a FIQ transfer, the transfer would have run
45893 + * to completion.
45894 + */
45895 + if (fiq_fsm_enable && dwc_otg_hcd->fiq_state->channel[num].fsm != FIQ_PASSTHROUGH) {
45896 + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
45897 + } else {
45898 + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
45899 + }
45900 + return 1;
45901 + }
45902 + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
45903 +
45904 + /*
45905 + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
45906 + * Execution path is fundamentally different for the channels after a FIQ has completed
45907 + * a split transaction.
45908 + */
45909 + if (fiq_fsm_enable) {
45910 + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
45911 + case FIQ_PASSTHROUGH:
45912 + break;
45913 + case FIQ_PASSTHROUGH_ERRORSTATE:
45914 + /* Hook into the error count */
45915 + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
45916 + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
45917 + qtd->error_count = 0;
45918 + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
45919 + }
45920 + break;
45921 + default:
45922 + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
45923 + return 1;
45924 + }
45925 + }
45926 +
45927 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45928 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45929 + hcint.d32 = hcint.d32 & hcintmsk.d32;
45930 + if (!dwc_otg_hcd->core_if->dma_enable) {
45931 + if (hcint.b.chhltd && hcint.d32 != 0x2) {
45932 + hcint.b.chhltd = 0;
45933 + }
45934 + }
45935 +
45936 + if (hcint.b.xfercomp) {
45937 + retval |=
45938 + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45939 + /*
45940 + * If NYET occurred at same time as Xfer Complete, the NYET is
45941 + * handled by the Xfer Complete interrupt handler. Don't want
45942 + * to call the NYET interrupt handler in this case.
45943 + */
45944 + hcint.b.nyet = 0;
45945 + }
45946 + if (hcint.b.chhltd) {
45947 + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45948 + }
45949 + if (hcint.b.ahberr) {
45950 + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45951 + }
45952 + if (hcint.b.stall) {
45953 + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45954 + }
45955 + if (hcint.b.nak) {
45956 + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45957 + }
45958 + if (hcint.b.ack) {
45959 + if(!hcint.b.chhltd)
45960 + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45961 + }
45962 + if (hcint.b.nyet) {
45963 + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45964 + }
45965 + if (hcint.b.xacterr) {
45966 + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45967 + }
45968 + if (hcint.b.bblerr) {
45969 + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45970 + }
45971 + if (hcint.b.frmovrun) {
45972 + retval |=
45973 + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45974 + }
45975 + if (hcint.b.datatglerr) {
45976 + retval |=
45977 + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45978 + }
45979 +
45980 + return retval;
45981 +}
45982 +#endif /* DWC_DEVICE_ONLY */
45983 --- /dev/null
45984 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
45985 @@ -0,0 +1,1002 @@
45986 +
45987 +/* ==========================================================================
45988 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
45989 + * $Revision: #20 $
45990 + * $Date: 2011/10/26 $
45991 + * $Change: 1872981 $
45992 + *
45993 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
45994 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
45995 + * otherwise expressly agreed to in writing between Synopsys and you.
45996 + *
45997 + * The Software IS NOT an item of Licensed Software or Licensed Product under
45998 + * any End User Software License Agreement or Agreement for Licensed Product
45999 + * with Synopsys or any supplement thereto. You are permitted to use and
46000 + * redistribute this Software in source and binary forms, with or without
46001 + * modification, provided that redistributions of source code must retain this
46002 + * notice. You may not view, use, disclose, copy or distribute this file or
46003 + * any information contained herein except pursuant to this license grant from
46004 + * Synopsys. If you do not agree with this notice, including the disclaimer
46005 + * below, then you are not authorized to use the Software.
46006 + *
46007 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
46008 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46009 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46010 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
46011 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46012 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46013 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46014 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46015 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46016 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46017 + * DAMAGE.
46018 + * ========================================================================== */
46019 +#ifndef DWC_DEVICE_ONLY
46020 +
46021 +/**
46022 + * @file
46023 + *
46024 + * This file contains the implementation of the HCD. In Linux, the HCD
46025 + * implements the hc_driver API.
46026 + */
46027 +#include <linux/kernel.h>
46028 +#include <linux/module.h>
46029 +#include <linux/moduleparam.h>
46030 +#include <linux/init.h>
46031 +#include <linux/device.h>
46032 +#include <linux/errno.h>
46033 +#include <linux/list.h>
46034 +#include <linux/interrupt.h>
46035 +#include <linux/string.h>
46036 +#include <linux/dma-mapping.h>
46037 +#include <linux/version.h>
46038 +#include <asm/io.h>
46039 +#include <asm/fiq.h>
46040 +#include <linux/usb.h>
46041 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
46042 +#include <../drivers/usb/core/hcd.h>
46043 +#else
46044 +#include <linux/usb/hcd.h>
46045 +#endif
46046 +#include <asm/bug.h>
46047 +
46048 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
46049 +#define USB_URB_EP_LINKING 1
46050 +#else
46051 +#define USB_URB_EP_LINKING 0
46052 +#endif
46053 +
46054 +#include "dwc_otg_hcd_if.h"
46055 +#include "dwc_otg_dbg.h"
46056 +#include "dwc_otg_driver.h"
46057 +#include "dwc_otg_hcd.h"
46058 +
46059 +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
46060 +
46061 +/**
46062 + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
46063 + * qualified with its direction (possible 32 endpoints per device).
46064 + */
46065 +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
46066 + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
46067 +
46068 +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
46069 +
46070 +extern bool fiq_enable;
46071 +
46072 +/** @name Linux HC Driver API Functions */
46073 +/** @{ */
46074 +/* manage i/o requests, device state */
46075 +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
46076 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46077 + struct usb_host_endpoint *ep,
46078 +#endif
46079 + struct urb *urb, gfp_t mem_flags);
46080 +
46081 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
46082 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46083 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
46084 +#endif
46085 +#else /* kernels at or post 2.6.30 */
46086 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
46087 + struct urb *urb, int status);
46088 +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
46089 +
46090 +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
46091 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
46092 +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
46093 +#endif
46094 +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
46095 +extern int hcd_start(struct usb_hcd *hcd);
46096 +extern void hcd_stop(struct usb_hcd *hcd);
46097 +static int get_frame_number(struct usb_hcd *hcd);
46098 +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
46099 +extern int hub_control(struct usb_hcd *hcd,
46100 + u16 typeReq,
46101 + u16 wValue, u16 wIndex, char *buf, u16 wLength);
46102 +
46103 +struct wrapper_priv_data {
46104 + dwc_otg_hcd_t *dwc_otg_hcd;
46105 +};
46106 +
46107 +/** @} */
46108 +
46109 +static struct hc_driver dwc_otg_hc_driver = {
46110 +
46111 + .description = dwc_otg_hcd_name,
46112 + .product_desc = "DWC OTG Controller",
46113 + .hcd_priv_size = sizeof(struct wrapper_priv_data),
46114 +
46115 + .irq = dwc_otg_hcd_irq,
46116 +
46117 + .flags = HCD_MEMORY | HCD_USB2,
46118 +
46119 + //.reset =
46120 + .start = hcd_start,
46121 + //.suspend =
46122 + //.resume =
46123 + .stop = hcd_stop,
46124 +
46125 + .urb_enqueue = dwc_otg_urb_enqueue,
46126 + .urb_dequeue = dwc_otg_urb_dequeue,
46127 + .endpoint_disable = endpoint_disable,
46128 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
46129 + .endpoint_reset = endpoint_reset,
46130 +#endif
46131 + .get_frame_number = get_frame_number,
46132 +
46133 + .hub_status_data = hub_status_data,
46134 + .hub_control = hub_control,
46135 + //.bus_suspend =
46136 + //.bus_resume =
46137 +};
46138 +
46139 +/** Gets the dwc_otg_hcd from a struct usb_hcd */
46140 +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
46141 +{
46142 + struct wrapper_priv_data *p;
46143 + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
46144 + return p->dwc_otg_hcd;
46145 +}
46146 +
46147 +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
46148 +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
46149 +{
46150 + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
46151 +}
46152 +
46153 +/** Gets the usb_host_endpoint associated with an URB. */
46154 +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
46155 +{
46156 + struct usb_device *dev = urb->dev;
46157 + int ep_num = usb_pipeendpoint(urb->pipe);
46158 +
46159 + if (usb_pipein(urb->pipe))
46160 + return dev->ep_in[ep_num];
46161 + else
46162 + return dev->ep_out[ep_num];
46163 +}
46164 +
46165 +static int _disconnect(dwc_otg_hcd_t * hcd)
46166 +{
46167 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
46168 +
46169 + usb_hcd->self.is_b_host = 0;
46170 + return 0;
46171 +}
46172 +
46173 +static int _start(dwc_otg_hcd_t * hcd)
46174 +{
46175 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
46176 +
46177 + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
46178 + hcd_start(usb_hcd);
46179 +
46180 + return 0;
46181 +}
46182 +
46183 +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
46184 + uint32_t * port_addr)
46185 +{
46186 + struct urb *urb = (struct urb *)urb_handle;
46187 + struct usb_bus *bus;
46188 +#if 1 //GRAYG - temporary
46189 + if (NULL == urb_handle)
46190 + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
46191 + if (NULL == urb->dev)
46192 + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
46193 + if (NULL == port_addr)
46194 + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
46195 +#endif
46196 + if (urb->dev->tt) {
46197 + if (NULL == urb->dev->tt->hub) {
46198 + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
46199 + __func__); //GRAYG
46200 + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
46201 + *hub_addr = 0; //GRAYG
46202 + // we probably shouldn't have a transaction translator if
46203 + // there's no associated hub?
46204 + } else {
46205 + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
46206 + if (urb->dev->tt->hub == bus->root_hub)
46207 + *hub_addr = 0;
46208 + else
46209 + *hub_addr = urb->dev->tt->hub->devnum;
46210 + }
46211 + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
46212 + } else {
46213 + *hub_addr = 0;
46214 + *port_addr = urb->dev->ttport;
46215 + }
46216 + return 0;
46217 +}
46218 +
46219 +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
46220 +{
46221 + struct urb *urb = (struct urb *)urb_handle;
46222 + return urb->dev->speed;
46223 +}
46224 +
46225 +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
46226 +{
46227 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
46228 + return usb_hcd->self.b_hnp_enable;
46229 +}
46230 +
46231 +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
46232 + struct urb *urb)
46233 +{
46234 + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
46235 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46236 + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
46237 + } else {
46238 + hcd_to_bus(hcd)->bandwidth_int_reqs++;
46239 + }
46240 +}
46241 +
46242 +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
46243 + struct urb *urb)
46244 +{
46245 + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
46246 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46247 + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
46248 + } else {
46249 + hcd_to_bus(hcd)->bandwidth_int_reqs--;
46250 + }
46251 +}
46252 +
46253 +/**
46254 + * Sets the final status of an URB and returns it to the device driver. Any
46255 + * required cleanup of the URB is performed. The HCD lock should be held on
46256 + * entry.
46257 + */
46258 +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
46259 + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
46260 +{
46261 + struct urb *urb = (struct urb *)urb_handle;
46262 + urb_tq_entry_t *new_entry;
46263 + int rc = 0;
46264 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46265 + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
46266 + __func__, urb, usb_pipedevice(urb->pipe),
46267 + usb_pipeendpoint(urb->pipe),
46268 + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
46269 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46270 + int i;
46271 + for (i = 0; i < urb->number_of_packets; i++) {
46272 + DWC_PRINTF(" ISO Desc %d status: %d\n",
46273 + i, urb->iso_frame_desc[i].status);
46274 + }
46275 + }
46276 + }
46277 + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
46278 + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
46279 + /* Convert status value. */
46280 + switch (status) {
46281 + case -DWC_E_PROTOCOL:
46282 + status = -EPROTO;
46283 + break;
46284 + case -DWC_E_IN_PROGRESS:
46285 + status = -EINPROGRESS;
46286 + break;
46287 + case -DWC_E_PIPE:
46288 + status = -EPIPE;
46289 + break;
46290 + case -DWC_E_IO:
46291 + status = -EIO;
46292 + break;
46293 + case -DWC_E_TIMEOUT:
46294 + status = -ETIMEDOUT;
46295 + break;
46296 + case -DWC_E_OVERFLOW:
46297 + status = -EOVERFLOW;
46298 + break;
46299 + case -DWC_E_SHUTDOWN:
46300 + status = -ESHUTDOWN;
46301 + break;
46302 + default:
46303 + if (status) {
46304 + DWC_PRINTF("Uknown urb status %d\n", status);
46305 +
46306 + }
46307 + }
46308 +
46309 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46310 + int i;
46311 +
46312 + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
46313 + urb->actual_length = 0;
46314 + for (i = 0; i < urb->number_of_packets; ++i) {
46315 + urb->iso_frame_desc[i].actual_length =
46316 + dwc_otg_hcd_urb_get_iso_desc_actual_length
46317 + (dwc_otg_urb, i);
46318 + urb->actual_length += urb->iso_frame_desc[i].actual_length;
46319 + urb->iso_frame_desc[i].status =
46320 + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
46321 + }
46322 + }
46323 +
46324 + urb->status = status;
46325 + urb->hcpriv = NULL;
46326 + if (!status) {
46327 + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
46328 + (urb->actual_length < urb->transfer_buffer_length)) {
46329 + urb->status = -EREMOTEIO;
46330 + }
46331 + }
46332 +
46333 + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
46334 + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
46335 + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
46336 + if (ep) {
46337 + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
46338 + dwc_otg_hcd_get_ep_bandwidth(hcd,
46339 + ep->hcpriv),
46340 + urb);
46341 + }
46342 + }
46343 + DWC_FREE(dwc_otg_urb);
46344 + if (!new_entry) {
46345 + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
46346 + urb->status = -EPROTO;
46347 + /* don't schedule the tasklet -
46348 + * directly return the packet here with error. */
46349 +#if USB_URB_EP_LINKING
46350 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
46351 +#endif
46352 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46353 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
46354 +#else
46355 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
46356 +#endif
46357 + } else {
46358 + new_entry->urb = urb;
46359 +#if USB_URB_EP_LINKING
46360 + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
46361 + if(0 == rc) {
46362 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
46363 + }
46364 +#endif
46365 + if(0 == rc) {
46366 + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
46367 + urb_tq_entries);
46368 + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
46369 + }
46370 + }
46371 + return 0;
46372 +}
46373 +
46374 +static struct dwc_otg_hcd_function_ops hcd_fops = {
46375 + .start = _start,
46376 + .disconnect = _disconnect,
46377 + .hub_info = _hub_info,
46378 + .speed = _speed,
46379 + .complete = _complete,
46380 + .get_b_hnp_enable = _get_b_hnp_enable,
46381 +};
46382 +
46383 +static struct fiq_handler fh = {
46384 + .name = "usb_fiq",
46385 +};
46386 +
46387 +static void hcd_init_fiq(void *cookie)
46388 +{
46389 + dwc_otg_device_t *otg_dev = cookie;
46390 + dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
46391 + struct pt_regs regs;
46392 + int irq;
46393 +
46394 + if (claim_fiq(&fh)) {
46395 + DWC_ERROR("Can't claim FIQ");
46396 + BUG();
46397 + }
46398 + DWC_WARN("FIQ on core %d at 0x%08x",
46399 + smp_processor_id(),
46400 + (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
46401 + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
46402 + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
46403 + memset(&regs,0,sizeof(regs));
46404 +
46405 + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
46406 + if (fiq_fsm_enable) {
46407 + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
46408 + //regs.ARM_r10 = dwc_otg_hcd->dma;
46409 + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
46410 + } else {
46411 + regs.ARM_fp = (long) dwc_otg_fiq_nop;
46412 + }
46413 +
46414 + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
46415 +
46416 +// __show_regs(&regs);
46417 + set_fiq_regs(&regs);
46418 +
46419 + //Set the mphi periph to the required registers
46420 + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
46421 + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
46422 + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
46423 + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
46424 + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
46425 + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
46426 + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
46427 + //Enable mphi peripheral
46428 + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
46429 +#ifdef DEBUG
46430 + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
46431 + DWC_WARN("MPHI periph has been enabled");
46432 + else
46433 + DWC_WARN("MPHI periph has NOT been enabled");
46434 +#endif
46435 + // Enable FIQ interrupt from USB peripheral
46436 +#ifdef CONFIG_GENERIC_IRQ_MULTI_HANDLER
46437 + irq = platform_get_irq(otg_dev->os_dep.platformdev, 1);
46438 +#else
46439 + irq = INTERRUPT_VC_USB;
46440 +#endif
46441 + if (irq < 0) {
46442 + DWC_ERROR("Can't get FIQ irq");
46443 + return;
46444 + }
46445 + /*
46446 + * We could take an interrupt immediately after enabling the FIQ.
46447 + * Ensure coherency of hcd->fiq_state.
46448 + */
46449 + smp_mb();
46450 + enable_fiq(irq);
46451 + local_fiq_enable();
46452 +}
46453 +
46454 +/**
46455 + * Initializes the HCD. This function allocates memory for and initializes the
46456 + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
46457 + * USB bus with the core and calls the hc_driver->start() function. It returns
46458 + * a negative error on failure.
46459 + */
46460 +int hcd_init(dwc_bus_dev_t *_dev)
46461 +{
46462 + struct usb_hcd *hcd = NULL;
46463 + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
46464 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
46465 + int retval = 0;
46466 + u64 dmamask;
46467 +
46468 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
46469 +
46470 + /* Set device flags indicating whether the HCD supports DMA. */
46471 + if (dwc_otg_is_dma_enable(otg_dev->core_if))
46472 + dmamask = DMA_BIT_MASK(32);
46473 + else
46474 + dmamask = 0;
46475 +
46476 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
46477 + dma_set_mask(&_dev->dev, dmamask);
46478 + dma_set_coherent_mask(&_dev->dev, dmamask);
46479 +#elif defined(PCI_INTERFACE)
46480 + pci_set_dma_mask(_dev, dmamask);
46481 + pci_set_consistent_dma_mask(_dev, dmamask);
46482 +#endif
46483 +
46484 + /*
46485 + * Allocate memory for the base HCD plus the DWC OTG HCD.
46486 + * Initialize the base HCD.
46487 + */
46488 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
46489 + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
46490 +#else
46491 + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
46492 + hcd->has_tt = 1;
46493 +// hcd->uses_new_polling = 1;
46494 +// hcd->poll_rh = 0;
46495 +#endif
46496 + if (!hcd) {
46497 + retval = -ENOMEM;
46498 + goto error1;
46499 + }
46500 +
46501 + hcd->regs = otg_dev->os_dep.base;
46502 +
46503 +
46504 + /* Initialize the DWC OTG HCD. */
46505 + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
46506 + if (!dwc_otg_hcd) {
46507 + goto error2;
46508 + }
46509 + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
46510 + dwc_otg_hcd;
46511 + otg_dev->hcd = dwc_otg_hcd;
46512 + otg_dev->hcd->otg_dev = otg_dev;
46513 +
46514 + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
46515 + goto error2;
46516 + }
46517 +
46518 + if (fiq_enable) {
46519 + if (num_online_cpus() > 1) {
46520 + /*
46521 + * bcm2709: can run the FIQ on a separate core to IRQs.
46522 + * Ensure driver state is visible to other cores before setting up the FIQ.
46523 + */
46524 + smp_mb();
46525 + smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
46526 + } else {
46527 + smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
46528 + }
46529 + }
46530 +
46531 + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
46532 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
46533 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
46534 + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
46535 +#endif
46536 + /* Don't support SG list at this point */
46537 + hcd->self.sg_tablesize = 0;
46538 +#endif
46539 + /*
46540 + * Finish generic HCD initialization and start the HCD. This function
46541 + * allocates the DMA buffer pool, registers the USB bus, requests the
46542 + * IRQ line, and calls hcd_start method.
46543 + */
46544 +#ifdef PLATFORM_INTERFACE
46545 + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED);
46546 +#else
46547 + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED);
46548 +#endif
46549 + if (retval < 0) {
46550 + goto error2;
46551 + }
46552 +
46553 + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
46554 + return 0;
46555 +
46556 +error2:
46557 + usb_put_hcd(hcd);
46558 +error1:
46559 + return retval;
46560 +}
46561 +
46562 +/**
46563 + * Removes the HCD.
46564 + * Frees memory and resources associated with the HCD and deregisters the bus.
46565 + */
46566 +void hcd_remove(dwc_bus_dev_t *_dev)
46567 +{
46568 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
46569 + dwc_otg_hcd_t *dwc_otg_hcd;
46570 + struct usb_hcd *hcd;
46571 +
46572 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
46573 +
46574 + if (!otg_dev) {
46575 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
46576 + return;
46577 + }
46578 +
46579 + dwc_otg_hcd = otg_dev->hcd;
46580 +
46581 + if (!dwc_otg_hcd) {
46582 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
46583 + return;
46584 + }
46585 +
46586 + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
46587 +
46588 + if (!hcd) {
46589 + DWC_DEBUGPL(DBG_ANY,
46590 + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
46591 + __func__);
46592 + return;
46593 + }
46594 + usb_remove_hcd(hcd);
46595 + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
46596 + dwc_otg_hcd_remove(dwc_otg_hcd);
46597 + usb_put_hcd(hcd);
46598 +}
46599 +
46600 +/* =========================================================================
46601 + * Linux HC Driver Functions
46602 + * ========================================================================= */
46603 +
46604 +/** Initializes the DWC_otg controller and its root hub and prepares it for host
46605 + * mode operation. Activates the root port. Returns 0 on success and a negative
46606 + * error code on failure. */
46607 +int hcd_start(struct usb_hcd *hcd)
46608 +{
46609 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46610 + struct usb_bus *bus;
46611 +
46612 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
46613 + bus = hcd_to_bus(hcd);
46614 +
46615 + hcd->state = HC_STATE_RUNNING;
46616 + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
46617 + return 0;
46618 + }
46619 +
46620 + /* Initialize and connect root hub if one is not already attached */
46621 + if (bus->root_hub) {
46622 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
46623 + /* Inform the HUB driver to resume. */
46624 + usb_hcd_resume_root_hub(hcd);
46625 + }
46626 +
46627 + return 0;
46628 +}
46629 +
46630 +/**
46631 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
46632 + * stopped.
46633 + */
46634 +void hcd_stop(struct usb_hcd *hcd)
46635 +{
46636 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46637 +
46638 + dwc_otg_hcd_stop(dwc_otg_hcd);
46639 +}
46640 +
46641 +/** Returns the current frame number. */
46642 +static int get_frame_number(struct usb_hcd *hcd)
46643 +{
46644 + hprt0_data_t hprt0;
46645 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46646 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
46647 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
46648 + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
46649 + else
46650 + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
46651 +}
46652 +
46653 +#ifdef DEBUG
46654 +static void dump_urb_info(struct urb *urb, char *fn_name)
46655 +{
46656 + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
46657 + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
46658 + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
46659 + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
46660 + DWC_PRINTF(" Endpoint type: %s\n", ( {
46661 + char *pipetype;
46662 + switch (usb_pipetype(urb->pipe)) {
46663 +case PIPE_CONTROL:
46664 +pipetype = "CONTROL"; break; case PIPE_BULK:
46665 +pipetype = "BULK"; break; case PIPE_INTERRUPT:
46666 +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
46667 +pipetype = "ISOCHRONOUS"; break; default:
46668 + pipetype = "UNKNOWN"; break;};
46669 + pipetype;}
46670 + )) ;
46671 + DWC_PRINTF(" Speed: %s\n", ( {
46672 + char *speed; switch (urb->dev->speed) {
46673 +case USB_SPEED_HIGH:
46674 +speed = "HIGH"; break; case USB_SPEED_FULL:
46675 +speed = "FULL"; break; case USB_SPEED_LOW:
46676 +speed = "LOW"; break; default:
46677 + speed = "UNKNOWN"; break;};
46678 + speed;}
46679 + )) ;
46680 + DWC_PRINTF(" Max packet size: %d\n",
46681 + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
46682 + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
46683 + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
46684 + urb->transfer_buffer, (void *)urb->transfer_dma);
46685 + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
46686 + urb->setup_packet, (void *)urb->setup_dma);
46687 + DWC_PRINTF(" Interval: %d\n", urb->interval);
46688 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46689 + int i;
46690 + for (i = 0; i < urb->number_of_packets; i++) {
46691 + DWC_PRINTF(" ISO Desc %d:\n", i);
46692 + DWC_PRINTF(" offset: %d, length %d\n",
46693 + urb->iso_frame_desc[i].offset,
46694 + urb->iso_frame_desc[i].length);
46695 + }
46696 + }
46697 +}
46698 +#endif
46699 +
46700 +/** Starts processing a USB transfer request specified by a USB Request Block
46701 + * (URB). mem_flags indicates the type of memory allocation to use while
46702 + * processing this URB. */
46703 +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
46704 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46705 + struct usb_host_endpoint *ep,
46706 +#endif
46707 + struct urb *urb, gfp_t mem_flags)
46708 +{
46709 + int retval = 0;
46710 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
46711 + struct usb_host_endpoint *ep = urb->ep;
46712 +#endif
46713 + dwc_irqflags_t irqflags;
46714 + void **ref_ep_hcpriv = &ep->hcpriv;
46715 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46716 + dwc_otg_hcd_urb_t *dwc_otg_urb;
46717 + int i;
46718 + int alloc_bandwidth = 0;
46719 + uint8_t ep_type = 0;
46720 + uint32_t flags = 0;
46721 + void *buf;
46722 +
46723 +#ifdef DEBUG
46724 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46725 + dump_urb_info(urb, "dwc_otg_urb_enqueue");
46726 + }
46727 +#endif
46728 +
46729 + if (!urb->transfer_buffer && urb->transfer_buffer_length)
46730 + return -EINVAL;
46731 +
46732 + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
46733 + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
46734 + if (!dwc_otg_hcd_is_bandwidth_allocated
46735 + (dwc_otg_hcd, ref_ep_hcpriv)) {
46736 + alloc_bandwidth = 1;
46737 + }
46738 + }
46739 +
46740 + switch (usb_pipetype(urb->pipe)) {
46741 + case PIPE_CONTROL:
46742 + ep_type = USB_ENDPOINT_XFER_CONTROL;
46743 + break;
46744 + case PIPE_ISOCHRONOUS:
46745 + ep_type = USB_ENDPOINT_XFER_ISOC;
46746 + break;
46747 + case PIPE_BULK:
46748 + ep_type = USB_ENDPOINT_XFER_BULK;
46749 + break;
46750 + case PIPE_INTERRUPT:
46751 + ep_type = USB_ENDPOINT_XFER_INT;
46752 + break;
46753 + default:
46754 + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
46755 + }
46756 +
46757 + /* # of packets is often 0 - do we really need to call this then? */
46758 + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
46759 + urb->number_of_packets,
46760 + mem_flags == GFP_ATOMIC ? 1 : 0);
46761 +
46762 + if(dwc_otg_urb == NULL)
46763 + return -ENOMEM;
46764 +
46765 + if (!dwc_otg_urb && urb->number_of_packets)
46766 + return -ENOMEM;
46767 +
46768 + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
46769 + usb_pipeendpoint(urb->pipe), ep_type,
46770 + usb_pipein(urb->pipe),
46771 + usb_maxpacket(urb->dev, urb->pipe,
46772 + !(usb_pipein(urb->pipe))));
46773 +
46774 + buf = urb->transfer_buffer;
46775 + if (hcd->self.uses_dma && !buf && urb->transfer_buffer_length) {
46776 + /*
46777 + * Calculate virtual address from physical address,
46778 + * because some class driver may not fill transfer_buffer.
46779 + * In Buffer DMA mode virual address is used,
46780 + * when handling non DWORD aligned buffers.
46781 + */
46782 + buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
46783 + dev_warn_once(&urb->dev->dev,
46784 + "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
46785 + &urb->transfer_dma, buf);
46786 + }
46787 +
46788 + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
46789 + flags |= URB_GIVEBACK_ASAP;
46790 + if (urb->transfer_flags & URB_ZERO_PACKET)
46791 + flags |= URB_SEND_ZERO_PACKET;
46792 +
46793 + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
46794 + urb->transfer_dma,
46795 + urb->transfer_buffer_length,
46796 + urb->setup_packet,
46797 + urb->setup_dma, flags, urb->interval);
46798 +
46799 + for (i = 0; i < urb->number_of_packets; ++i) {
46800 + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
46801 + urb->
46802 + iso_frame_desc[i].offset,
46803 + urb->
46804 + iso_frame_desc[i].length);
46805 + }
46806 +
46807 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
46808 + urb->hcpriv = dwc_otg_urb;
46809 +#if USB_URB_EP_LINKING
46810 + retval = usb_hcd_link_urb_to_ep(hcd, urb);
46811 + if (0 == retval)
46812 +#endif
46813 + {
46814 + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
46815 + /*(dwc_otg_qh_t **)*/
46816 + ref_ep_hcpriv, 1);
46817 + if (0 == retval) {
46818 + if (alloc_bandwidth) {
46819 + allocate_bus_bandwidth(hcd,
46820 + dwc_otg_hcd_get_ep_bandwidth(
46821 + dwc_otg_hcd, *ref_ep_hcpriv),
46822 + urb);
46823 + }
46824 + } else {
46825 + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
46826 +#if USB_URB_EP_LINKING
46827 + usb_hcd_unlink_urb_from_ep(hcd, urb);
46828 +#endif
46829 + DWC_FREE(dwc_otg_urb);
46830 + urb->hcpriv = NULL;
46831 + if (retval == -DWC_E_NO_DEVICE)
46832 + retval = -ENODEV;
46833 + }
46834 + }
46835 +#if USB_URB_EP_LINKING
46836 + else
46837 + {
46838 + DWC_FREE(dwc_otg_urb);
46839 + urb->hcpriv = NULL;
46840 + }
46841 +#endif
46842 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
46843 + return retval;
46844 +}
46845 +
46846 +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
46847 + * success. */
46848 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46849 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
46850 +#else
46851 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
46852 +#endif
46853 +{
46854 + dwc_irqflags_t flags;
46855 + dwc_otg_hcd_t *dwc_otg_hcd;
46856 + int rc;
46857 +
46858 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
46859 +
46860 + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46861 +
46862 +#ifdef DEBUG
46863 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46864 + dump_urb_info(urb, "dwc_otg_urb_dequeue");
46865 + }
46866 +#endif
46867 +
46868 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
46869 + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
46870 + if (0 == rc) {
46871 + if(urb->hcpriv != NULL) {
46872 + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
46873 + (dwc_otg_hcd_urb_t *)urb->hcpriv);
46874 +
46875 + DWC_FREE(urb->hcpriv);
46876 + urb->hcpriv = NULL;
46877 + }
46878 + }
46879 +
46880 + if (0 == rc) {
46881 + /* Higher layer software sets URB status. */
46882 +#if USB_URB_EP_LINKING
46883 + usb_hcd_unlink_urb_from_ep(hcd, urb);
46884 +#endif
46885 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46886 +
46887 +
46888 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46889 + usb_hcd_giveback_urb(hcd, urb);
46890 +#else
46891 + usb_hcd_giveback_urb(hcd, urb, status);
46892 +#endif
46893 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46894 + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
46895 + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
46896 + }
46897 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
46898 + } else {
46899 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46900 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
46901 + rc);
46902 + }
46903 +
46904 + return rc;
46905 +}
46906 +
46907 +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
46908 + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
46909 + * must already be dequeued. */
46910 +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
46911 +{
46912 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46913 +
46914 + DWC_DEBUGPL(DBG_HCD,
46915 + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
46916 + "endpoint=%d\n", ep->desc.bEndpointAddress,
46917 + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
46918 + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
46919 + ep->hcpriv = NULL;
46920 +}
46921 +
46922 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
46923 +/* Resets endpoint specific parameter values, in current version used to reset
46924 + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
46925 +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
46926 +{
46927 + dwc_irqflags_t flags;
46928 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46929 +
46930 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
46931 +
46932 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
46933 + if (ep->hcpriv) {
46934 + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
46935 + }
46936 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46937 +}
46938 +#endif
46939 +
46940 +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
46941 + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
46942 + * interrupt.
46943 + *
46944 + * This function is called by the USB core when an interrupt occurs */
46945 +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
46946 +{
46947 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46948 + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
46949 + if (retval != 0) {
46950 + S3C2410X_CLEAR_EINTPEND();
46951 + }
46952 + return IRQ_RETVAL(retval);
46953 +}
46954 +
46955 +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
46956 + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
46957 + * is the status change indicator for the single root port. Returns 1 if either
46958 + * change indicator is 1, otherwise returns 0. */
46959 +int hub_status_data(struct usb_hcd *hcd, char *buf)
46960 +{
46961 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46962 +
46963 + buf[0] = 0;
46964 + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
46965 +
46966 + return (buf[0] != 0);
46967 +}
46968 +
46969 +/** Handles hub class-specific requests. */
46970 +int hub_control(struct usb_hcd *hcd,
46971 + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
46972 +{
46973 + int retval;
46974 +
46975 + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
46976 + typeReq, wValue, wIndex, buf, wLength);
46977 +
46978 + switch (retval) {
46979 + case -DWC_E_INVALID:
46980 + retval = -EINVAL;
46981 + break;
46982 + }
46983 +
46984 + return retval;
46985 +}
46986 +
46987 +#endif /* DWC_DEVICE_ONLY */
46988 --- /dev/null
46989 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
46990 @@ -0,0 +1,971 @@
46991 +/* ==========================================================================
46992 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
46993 + * $Revision: #44 $
46994 + * $Date: 2011/10/26 $
46995 + * $Change: 1873028 $
46996 + *
46997 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
46998 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
46999 + * otherwise expressly agreed to in writing between Synopsys and you.
47000 + *
47001 + * The Software IS NOT an item of Licensed Software or Licensed Product under
47002 + * any End User Software License Agreement or Agreement for Licensed Product
47003 + * with Synopsys or any supplement thereto. You are permitted to use and
47004 + * redistribute this Software in source and binary forms, with or without
47005 + * modification, provided that redistributions of source code must retain this
47006 + * notice. You may not view, use, disclose, copy or distribute this file or
47007 + * any information contained herein except pursuant to this license grant from
47008 + * Synopsys. If you do not agree with this notice, including the disclaimer
47009 + * below, then you are not authorized to use the Software.
47010 + *
47011 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
47012 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47013 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47014 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
47015 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47016 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47017 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47018 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47019 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47020 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
47021 + * DAMAGE.
47022 + * ========================================================================== */
47023 +#ifndef DWC_DEVICE_ONLY
47024 +
47025 +/**
47026 + * @file
47027 + *
47028 + * This file contains the functions to manage Queue Heads and Queue
47029 + * Transfer Descriptors.
47030 + */
47031 +
47032 +#include "dwc_otg_hcd.h"
47033 +#include "dwc_otg_regs.h"
47034 +
47035 +extern bool microframe_schedule;
47036 +extern unsigned short int_ep_interval_min;
47037 +
47038 +/**
47039 + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
47040 + * removed from a list. QTD list should already be empty if called from URB
47041 + * Dequeue.
47042 + *
47043 + * @param hcd HCD instance.
47044 + * @param qh The QH to free.
47045 + */
47046 +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47047 +{
47048 + dwc_otg_qtd_t *qtd, *qtd_tmp;
47049 + dwc_irqflags_t flags;
47050 + uint32_t buf_size = 0;
47051 + uint8_t *align_buf_virt = NULL;
47052 + dwc_dma_t align_buf_dma;
47053 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
47054 +
47055 + /* Free each QTD in the QTD list */
47056 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
47057 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
47058 + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
47059 + dwc_otg_hcd_qtd_free(qtd);
47060 + }
47061 +
47062 + if (hcd->core_if->dma_desc_enable) {
47063 + dwc_otg_hcd_qh_free_ddma(hcd, qh);
47064 + } else if (qh->dw_align_buf) {
47065 + if (qh->ep_type == UE_ISOCHRONOUS) {
47066 + buf_size = 4096;
47067 + } else {
47068 + buf_size = hcd->core_if->core_params->max_transfer_size;
47069 + }
47070 + align_buf_virt = qh->dw_align_buf;
47071 + align_buf_dma = qh->dw_align_buf_dma;
47072 + }
47073 +
47074 + DWC_FREE(qh);
47075 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
47076 + if (align_buf_virt)
47077 + DWC_DMA_FREE(dev, buf_size, align_buf_virt, align_buf_dma);
47078 + return;
47079 +}
47080 +
47081 +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
47082 +#define HS_HOST_DELAY 5 /* nanoseconds */
47083 +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
47084 +#define HUB_LS_SETUP 333 /* nanoseconds */
47085 +#define NS_TO_US(ns) ((ns + 500) / 1000)
47086 + /* convert & round nanoseconds to microseconds */
47087 +
47088 +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
47089 +{
47090 + unsigned long retval;
47091 +
47092 + switch (speed) {
47093 + case USB_SPEED_HIGH:
47094 + if (is_isoc) {
47095 + retval =
47096 + ((38 * 8 * 2083) +
47097 + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
47098 + HS_HOST_DELAY;
47099 + } else {
47100 + retval =
47101 + ((55 * 8 * 2083) +
47102 + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
47103 + HS_HOST_DELAY;
47104 + }
47105 + break;
47106 + case USB_SPEED_FULL:
47107 + if (is_isoc) {
47108 + retval =
47109 + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
47110 + if (is_in) {
47111 + retval = 7268 + FS_LS_HOST_DELAY + retval;
47112 + } else {
47113 + retval = 6265 + FS_LS_HOST_DELAY + retval;
47114 + }
47115 + } else {
47116 + retval =
47117 + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
47118 + retval = 9107 + FS_LS_HOST_DELAY + retval;
47119 + }
47120 + break;
47121 + case USB_SPEED_LOW:
47122 + if (is_in) {
47123 + retval =
47124 + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
47125 + 1000;
47126 + retval =
47127 + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
47128 + retval;
47129 + } else {
47130 + retval =
47131 + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
47132 + 1000;
47133 + retval =
47134 + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
47135 + retval;
47136 + }
47137 + break;
47138 + default:
47139 + DWC_WARN("Unknown device speed\n");
47140 + retval = -1;
47141 + }
47142 +
47143 + return NS_TO_US(retval);
47144 +}
47145 +
47146 +/**
47147 + * Initializes a QH structure.
47148 + *
47149 + * @param hcd The HCD state structure for the DWC OTG controller.
47150 + * @param qh The QH to init.
47151 + * @param urb Holds the information about the device/endpoint that we need
47152 + * to initialize the QH.
47153 + */
47154 +#define SCHEDULE_SLOP 10
47155 +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
47156 +{
47157 + char *speed, *type;
47158 + int dev_speed;
47159 + uint32_t hub_addr, hub_port;
47160 +
47161 + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
47162 +
47163 + /* Initialize QH */
47164 + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
47165 + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
47166 +
47167 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
47168 + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
47169 + DWC_CIRCLEQ_INIT(&qh->qtd_list);
47170 + DWC_LIST_INIT(&qh->qh_list_entry);
47171 + qh->channel = NULL;
47172 +
47173 + /* FS/LS Enpoint on HS Hub
47174 + * NOT virtual root hub */
47175 + dev_speed = hcd->fops->speed(hcd, urb->priv);
47176 +
47177 + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
47178 + qh->do_split = 0;
47179 + if (microframe_schedule)
47180 + qh->speed = dev_speed;
47181 +
47182 + qh->nak_frame = 0xffff;
47183 +
47184 + if (((dev_speed == USB_SPEED_LOW) ||
47185 + (dev_speed == USB_SPEED_FULL)) &&
47186 + (hub_addr != 0 && hub_addr != 1)) {
47187 + DWC_DEBUGPL(DBG_HCD,
47188 + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
47189 + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
47190 + hub_port);
47191 + qh->do_split = 1;
47192 + qh->skip_count = 0;
47193 + }
47194 +
47195 + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
47196 + /* Compute scheduling parameters once and save them. */
47197 + hprt0_data_t hprt;
47198 +
47199 + /** @todo Account for split transfers in the bus time. */
47200 + int bytecount =
47201 + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
47202 +
47203 + qh->usecs =
47204 + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
47205 + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
47206 + bytecount);
47207 + /* Start in a slightly future (micro)frame. */
47208 + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
47209 + SCHEDULE_SLOP);
47210 + qh->interval = urb->interval;
47211 +
47212 + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
47213 + if (hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) {
47214 + if (dev_speed == USB_SPEED_LOW ||
47215 + dev_speed == USB_SPEED_FULL) {
47216 + qh->interval *= 8;
47217 + qh->sched_frame |= 0x7;
47218 + qh->start_split_frame = qh->sched_frame;
47219 + } else if (int_ep_interval_min >= 2 &&
47220 + qh->interval < int_ep_interval_min &&
47221 + qh->ep_type == UE_INTERRUPT) {
47222 + qh->interval = int_ep_interval_min;
47223 + }
47224 + }
47225 + }
47226 +
47227 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
47228 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
47229 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
47230 + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
47231 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
47232 + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
47233 + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
47234 + switch (dev_speed) {
47235 + case USB_SPEED_LOW:
47236 + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
47237 + speed = "low";
47238 + break;
47239 + case USB_SPEED_FULL:
47240 + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
47241 + speed = "full";
47242 + break;
47243 + case USB_SPEED_HIGH:
47244 + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
47245 + speed = "high";
47246 + break;
47247 + default:
47248 + speed = "?";
47249 + break;
47250 + }
47251 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
47252 +
47253 + switch (qh->ep_type) {
47254 + case UE_ISOCHRONOUS:
47255 + type = "isochronous";
47256 + break;
47257 + case UE_INTERRUPT:
47258 + type = "interrupt";
47259 + break;
47260 + case UE_CONTROL:
47261 + type = "control";
47262 + break;
47263 + case UE_BULK:
47264 + type = "bulk";
47265 + break;
47266 + default:
47267 + type = "?";
47268 + break;
47269 + }
47270 +
47271 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
47272 +
47273 +#ifdef DEBUG
47274 + if (qh->ep_type == UE_INTERRUPT) {
47275 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
47276 + qh->usecs);
47277 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
47278 + qh->interval);
47279 + }
47280 +#endif
47281 +
47282 +}
47283 +
47284 +/**
47285 + * This function allocates and initializes a QH.
47286 + *
47287 + * @param hcd The HCD state structure for the DWC OTG controller.
47288 + * @param urb Holds the information about the device/endpoint that we need
47289 + * to initialize the QH.
47290 + * @param atomic_alloc Flag to do atomic allocation if needed
47291 + *
47292 + * @return Returns pointer to the newly allocated QH, or NULL on error. */
47293 +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
47294 + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
47295 +{
47296 + dwc_otg_qh_t *qh;
47297 +
47298 + /* Allocate memory */
47299 + /** @todo add memflags argument */
47300 + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
47301 + if (qh == NULL) {
47302 + DWC_ERROR("qh allocation failed");
47303 + return NULL;
47304 + }
47305 +
47306 + qh_init(hcd, qh, urb);
47307 +
47308 + if (hcd->core_if->dma_desc_enable
47309 + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
47310 + dwc_otg_hcd_qh_free(hcd, qh);
47311 + return NULL;
47312 + }
47313 +
47314 + return qh;
47315 +}
47316 +
47317 +/* microframe_schedule=0 start */
47318 +
47319 +/**
47320 + * Checks that a channel is available for a periodic transfer.
47321 + *
47322 + * @return 0 if successful, negative error code otherise.
47323 + */
47324 +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
47325 +{
47326 + /*
47327 + * Currently assuming that there is a dedicated host channnel for each
47328 + * periodic transaction plus at least one host channel for
47329 + * non-periodic transactions.
47330 + */
47331 + int status;
47332 + int num_channels;
47333 +
47334 + num_channels = hcd->core_if->core_params->host_channels;
47335 + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
47336 + && (hcd->periodic_channels < num_channels - 1)) {
47337 + status = 0;
47338 + } else {
47339 + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
47340 + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
47341 + status = -DWC_E_NO_SPACE;
47342 + }
47343 +
47344 + return status;
47345 +}
47346 +
47347 +/**
47348 + * Checks that there is sufficient bandwidth for the specified QH in the
47349 + * periodic schedule. For simplicity, this calculation assumes that all the
47350 + * transfers in the periodic schedule may occur in the same (micro)frame.
47351 + *
47352 + * @param hcd The HCD state structure for the DWC OTG controller.
47353 + * @param qh QH containing periodic bandwidth required.
47354 + *
47355 + * @return 0 if successful, negative error code otherwise.
47356 + */
47357 +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47358 +{
47359 + int status;
47360 + int16_t max_claimed_usecs;
47361 +
47362 + status = 0;
47363 +
47364 + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
47365 + /*
47366 + * High speed mode.
47367 + * Max periodic usecs is 80% x 125 usec = 100 usec.
47368 + */
47369 +
47370 + max_claimed_usecs = 100 - qh->usecs;
47371 + } else {
47372 + /*
47373 + * Full speed mode.
47374 + * Max periodic usecs is 90% x 1000 usec = 900 usec.
47375 + */
47376 + max_claimed_usecs = 900 - qh->usecs;
47377 + }
47378 +
47379 + if (hcd->periodic_usecs > max_claimed_usecs) {
47380 + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
47381 + status = -DWC_E_NO_SPACE;
47382 + }
47383 +
47384 + return status;
47385 +}
47386 +
47387 +/* microframe_schedule=0 end */
47388 +
47389 +/**
47390 + * Microframe scheduler
47391 + * track the total use in hcd->frame_usecs
47392 + * keep each qh use in qh->frame_usecs
47393 + * when surrendering the qh then donate the time back
47394 + */
47395 +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
47396 +
47397 +/*
47398 + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
47399 + */
47400 +void init_hcd_usecs(dwc_otg_hcd_t *_hcd)
47401 +{
47402 + int i;
47403 + if (_hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
47404 + _hcd->frame_usecs[0] = 900;
47405 + for (i = 1; i < 8; i++)
47406 + _hcd->frame_usecs[i] = 0;
47407 + } else {
47408 + for (i = 0; i < 8; i++)
47409 + _hcd->frame_usecs[i] = max_uframe_usecs[i];
47410 + }
47411 +}
47412 +
47413 +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47414 +{
47415 + int i;
47416 + unsigned short utime;
47417 + int t_left;
47418 + int ret;
47419 + int done;
47420 +
47421 + ret = -1;
47422 + utime = _qh->usecs;
47423 + t_left = utime;
47424 + i = 0;
47425 + done = 0;
47426 + while (done == 0) {
47427 + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
47428 + if (utime <= _hcd->frame_usecs[i]) {
47429 + _hcd->frame_usecs[i] -= utime;
47430 + _qh->frame_usecs[i] += utime;
47431 + t_left -= utime;
47432 + ret = i;
47433 + done = 1;
47434 + return ret;
47435 + } else {
47436 + i++;
47437 + if (i == 8) {
47438 + done = 1;
47439 + ret = -1;
47440 + }
47441 + }
47442 + }
47443 + return ret;
47444 + }
47445 +
47446 +/*
47447 + * use this for FS apps that can span multiple uframes
47448 + */
47449 +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47450 +{
47451 + int i;
47452 + int j;
47453 + unsigned short utime;
47454 + int t_left;
47455 + int ret;
47456 + int done;
47457 + unsigned short xtime;
47458 +
47459 + ret = -1;
47460 + utime = _qh->usecs;
47461 + t_left = utime;
47462 + i = 0;
47463 + done = 0;
47464 +loop:
47465 + while (done == 0) {
47466 + if(_hcd->frame_usecs[i] <= 0) {
47467 + i++;
47468 + if (i == 8) {
47469 + done = 1;
47470 + ret = -1;
47471 + }
47472 + goto loop;
47473 + }
47474 +
47475 + /*
47476 + * we need n consecutive slots
47477 + * so use j as a start slot j plus j+1 must be enough time (for now)
47478 + */
47479 + xtime= _hcd->frame_usecs[i];
47480 + for (j = i+1 ; j < 8 ; j++ ) {
47481 + /*
47482 + * if we add this frame remaining time to xtime we may
47483 + * be OK, if not we need to test j for a complete frame
47484 + */
47485 + if ((xtime+_hcd->frame_usecs[j]) < utime) {
47486 + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
47487 + j = 8;
47488 + ret = -1;
47489 + continue;
47490 + }
47491 + }
47492 + if (xtime >= utime) {
47493 + ret = i;
47494 + j = 8; /* stop loop with a good value ret */
47495 + continue;
47496 + }
47497 + /* add the frame time to x time */
47498 + xtime += _hcd->frame_usecs[j];
47499 + /* we must have a fully available next frame or break */
47500 + if ((xtime < utime)
47501 + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
47502 + ret = -1;
47503 + j = 8; /* stop loop with a bad value ret */
47504 + continue;
47505 + }
47506 + }
47507 + if (ret >= 0) {
47508 + t_left = utime;
47509 + for (j = i; (t_left>0) && (j < 8); j++ ) {
47510 + t_left -= _hcd->frame_usecs[j];
47511 + if ( t_left <= 0 ) {
47512 + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
47513 + _hcd->frame_usecs[j]= -t_left;
47514 + ret = i;
47515 + done = 1;
47516 + } else {
47517 + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
47518 + _hcd->frame_usecs[j] = 0;
47519 + }
47520 + }
47521 + } else {
47522 + i++;
47523 + if (i == 8) {
47524 + done = 1;
47525 + ret = -1;
47526 + }
47527 + }
47528 + }
47529 + return ret;
47530 +}
47531 +
47532 +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47533 +{
47534 + int ret;
47535 + ret = -1;
47536 +
47537 + if (_qh->speed == USB_SPEED_HIGH ||
47538 + _hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
47539 + /* if this is a hs transaction we need a full frame - or account for FS usecs */
47540 + ret = find_single_uframe(_hcd, _qh);
47541 + } else {
47542 + /* if this is a fs transaction we may need a sequence of frames */
47543 + ret = find_multi_uframe(_hcd, _qh);
47544 + }
47545 + return ret;
47546 +}
47547 +
47548 +/**
47549 + * Checks that the max transfer size allowed in a host channel is large enough
47550 + * to handle the maximum data transfer in a single (micro)frame for a periodic
47551 + * transfer.
47552 + *
47553 + * @param hcd The HCD state structure for the DWC OTG controller.
47554 + * @param qh QH for a periodic endpoint.
47555 + *
47556 + * @return 0 if successful, negative error code otherwise.
47557 + */
47558 +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47559 +{
47560 + int status;
47561 + uint32_t max_xfer_size;
47562 + uint32_t max_channel_xfer_size;
47563 +
47564 + status = 0;
47565 +
47566 + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
47567 + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
47568 +
47569 + if (max_xfer_size > max_channel_xfer_size) {
47570 + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
47571 + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
47572 + status = -DWC_E_NO_SPACE;
47573 + }
47574 +
47575 + return status;
47576 +}
47577 +
47578 +
47579 +
47580 +/**
47581 + * Schedules an interrupt or isochronous transfer in the periodic schedule.
47582 + *
47583 + * @param hcd The HCD state structure for the DWC OTG controller.
47584 + * @param qh QH for the periodic transfer. The QH should already contain the
47585 + * scheduling information.
47586 + *
47587 + * @return 0 if successful, negative error code otherwise.
47588 + */
47589 +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47590 +{
47591 + int status = 0;
47592 +
47593 + if (microframe_schedule) {
47594 + int frame;
47595 + status = find_uframe(hcd, qh);
47596 + frame = -1;
47597 + if (status == 0) {
47598 + frame = 7;
47599 + } else {
47600 + if (status > 0 )
47601 + frame = status-1;
47602 + }
47603 +
47604 + /* Set the new frame up */
47605 + if (frame > -1) {
47606 + qh->sched_frame &= ~0x7;
47607 + qh->sched_frame |= (frame & 7);
47608 + }
47609 +
47610 + if (status != -1)
47611 + status = 0;
47612 + } else {
47613 + status = periodic_channel_available(hcd);
47614 + if (status) {
47615 + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
47616 + return status;
47617 + }
47618 +
47619 + status = check_periodic_bandwidth(hcd, qh);
47620 + }
47621 + if (status) {
47622 + DWC_INFO("%s: Insufficient periodic bandwidth for "
47623 + "periodic transfer.\n", __func__);
47624 + return -DWC_E_NO_SPACE;
47625 + }
47626 + status = check_max_xfer_size(hcd, qh);
47627 + if (status) {
47628 + DWC_INFO("%s: Channel max transfer size too small "
47629 + "for periodic transfer.\n", __func__);
47630 + return status;
47631 + }
47632 +
47633 + if (hcd->core_if->dma_desc_enable) {
47634 + /* Don't rely on SOF and start in ready schedule */
47635 + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
47636 + }
47637 + else {
47638 + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
47639 + {
47640 + hcd->fiq_state->next_sched_frame = qh->sched_frame;
47641 +
47642 + }
47643 + /* Always start in the inactive schedule. */
47644 + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
47645 + }
47646 +
47647 + if (!microframe_schedule) {
47648 + /* Reserve the periodic channel. */
47649 + hcd->periodic_channels++;
47650 + }
47651 +
47652 + /* Update claimed usecs per (micro)frame. */
47653 + hcd->periodic_usecs += qh->usecs;
47654 +
47655 + return status;
47656 +}
47657 +
47658 +
47659 +/**
47660 + * This function adds a QH to either the non periodic or periodic schedule if
47661 + * it is not already in the schedule. If the QH is already in the schedule, no
47662 + * action is taken.
47663 + *
47664 + * @return 0 if successful, negative error code otherwise.
47665 + */
47666 +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47667 +{
47668 + int status = 0;
47669 + gintmsk_data_t intr_mask = {.d32 = 0 };
47670 +
47671 + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
47672 + /* QH already in a schedule. */
47673 + return status;
47674 + }
47675 +
47676 + /* Add the new QH to the appropriate schedule */
47677 + if (dwc_qh_is_non_per(qh)) {
47678 + /* Always start in the inactive schedule. */
47679 + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
47680 + &qh->qh_list_entry);
47681 + //hcd->fiq_state->kick_np_queues = 1;
47682 + } else {
47683 + status = schedule_periodic(hcd, qh);
47684 + if ( !hcd->periodic_qh_count ) {
47685 + intr_mask.b.sofintr = 1;
47686 + if (fiq_enable) {
47687 + local_fiq_disable();
47688 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
47689 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
47690 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
47691 + local_fiq_enable();
47692 + } else {
47693 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
47694 + }
47695 + }
47696 + hcd->periodic_qh_count++;
47697 + }
47698 +
47699 + return status;
47700 +}
47701 +
47702 +/**
47703 + * Removes an interrupt or isochronous transfer from the periodic schedule.
47704 + *
47705 + * @param hcd The HCD state structure for the DWC OTG controller.
47706 + * @param qh QH for the periodic transfer.
47707 + */
47708 +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47709 +{
47710 + int i;
47711 + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
47712 +
47713 + /* Update claimed usecs per (micro)frame. */
47714 + hcd->periodic_usecs -= qh->usecs;
47715 +
47716 + if (!microframe_schedule) {
47717 + /* Release the periodic channel reservation. */
47718 + hcd->periodic_channels--;
47719 + } else {
47720 + for (i = 0; i < 8; i++) {
47721 + hcd->frame_usecs[i] += qh->frame_usecs[i];
47722 + qh->frame_usecs[i] = 0;
47723 + }
47724 + }
47725 +}
47726 +
47727 +/**
47728 + * Removes a QH from either the non-periodic or periodic schedule. Memory is
47729 + * not freed.
47730 + *
47731 + * @param hcd The HCD state structure.
47732 + * @param qh QH to remove from schedule. */
47733 +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47734 +{
47735 + gintmsk_data_t intr_mask = {.d32 = 0 };
47736 +
47737 + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
47738 + /* QH is not in a schedule. */
47739 + return;
47740 + }
47741 +
47742 + if (dwc_qh_is_non_per(qh)) {
47743 + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
47744 + hcd->non_periodic_qh_ptr =
47745 + hcd->non_periodic_qh_ptr->next;
47746 + }
47747 + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
47748 + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
47749 + // hcd->fiq_state->kick_np_queues = 1;
47750 + } else {
47751 + deschedule_periodic(hcd, qh);
47752 + hcd->periodic_qh_count--;
47753 + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
47754 + intr_mask.b.sofintr = 1;
47755 + if (fiq_enable) {
47756 + local_fiq_disable();
47757 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
47758 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
47759 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
47760 + local_fiq_enable();
47761 + } else {
47762 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
47763 + }
47764 + }
47765 + }
47766 +}
47767 +
47768 +/**
47769 + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
47770 + * non-periodic schedule. The QH is added to the inactive non-periodic
47771 + * schedule if any QTDs are still attached to the QH.
47772 + *
47773 + * For periodic QHs, the QH is removed from the periodic queued schedule. If
47774 + * there are any QTDs still attached to the QH, the QH is added to either the
47775 + * periodic inactive schedule or the periodic ready schedule and its next
47776 + * scheduled frame is calculated. The QH is placed in the ready schedule if
47777 + * the scheduled frame has been reached already. Otherwise it's placed in the
47778 + * inactive schedule. If there are no QTDs attached to the QH, the QH is
47779 + * completely removed from the periodic schedule.
47780 + */
47781 +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
47782 + int sched_next_periodic_split)
47783 +{
47784 + if (dwc_qh_is_non_per(qh)) {
47785 + dwc_otg_hcd_qh_remove(hcd, qh);
47786 + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
47787 + /* Add back to inactive non-periodic schedule. */
47788 + dwc_otg_hcd_qh_add(hcd, qh);
47789 + //hcd->fiq_state->kick_np_queues = 1;
47790 + } else {
47791 + if(nak_holdoff && qh->do_split) {
47792 + qh->nak_frame = 0xFFFF;
47793 + }
47794 + }
47795 + } else {
47796 + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
47797 +
47798 + if (qh->do_split) {
47799 + /* Schedule the next continuing periodic split transfer */
47800 + if (sched_next_periodic_split) {
47801 +
47802 + qh->sched_frame = frame_number;
47803 +
47804 + if (dwc_frame_num_le(frame_number,
47805 + dwc_frame_num_inc
47806 + (qh->start_split_frame,
47807 + 1))) {
47808 + /*
47809 + * Allow one frame to elapse after start
47810 + * split microframe before scheduling
47811 + * complete split, but DONT if we are
47812 + * doing the next start split in the
47813 + * same frame for an ISOC out.
47814 + */
47815 + if ((qh->ep_type != UE_ISOCHRONOUS) ||
47816 + (qh->ep_is_in != 0)) {
47817 + qh->sched_frame =
47818 + dwc_frame_num_inc(qh->sched_frame, 1);
47819 + }
47820 + }
47821 + } else {
47822 + qh->sched_frame =
47823 + dwc_frame_num_inc(qh->start_split_frame,
47824 + qh->interval);
47825 + if (dwc_frame_num_le
47826 + (qh->sched_frame, frame_number)) {
47827 + qh->sched_frame = frame_number;
47828 + }
47829 + qh->sched_frame |= 0x7;
47830 + qh->start_split_frame = qh->sched_frame;
47831 + }
47832 + } else {
47833 + qh->sched_frame =
47834 + dwc_frame_num_inc(qh->sched_frame, qh->interval);
47835 + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
47836 + qh->sched_frame = frame_number;
47837 + }
47838 + }
47839 +
47840 + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
47841 + dwc_otg_hcd_qh_remove(hcd, qh);
47842 + } else {
47843 + /*
47844 + * Remove from periodic_sched_queued and move to
47845 + * appropriate queue.
47846 + */
47847 + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
47848 + (!microframe_schedule && qh->sched_frame == frame_number)) {
47849 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
47850 + &qh->qh_list_entry);
47851 + } else {
47852 + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
47853 + {
47854 + hcd->fiq_state->next_sched_frame = qh->sched_frame;
47855 + }
47856 +
47857 + DWC_LIST_MOVE_HEAD
47858 + (&hcd->periodic_sched_inactive,
47859 + &qh->qh_list_entry);
47860 + }
47861 + }
47862 + }
47863 +}
47864 +
47865 +/**
47866 + * This function allocates and initializes a QTD.
47867 + *
47868 + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
47869 + * pointing to each other so each pair should have a unique correlation.
47870 + * @param atomic_alloc Flag to do atomic alloc if needed
47871 + *
47872 + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
47873 +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
47874 +{
47875 + dwc_otg_qtd_t *qtd;
47876 +
47877 + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
47878 + if (qtd == NULL) {
47879 + return NULL;
47880 + }
47881 +
47882 + dwc_otg_hcd_qtd_init(qtd, urb);
47883 + return qtd;
47884 +}
47885 +
47886 +/**
47887 + * Initializes a QTD structure.
47888 + *
47889 + * @param qtd The QTD to initialize.
47890 + * @param urb The URB to use for initialization. */
47891 +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
47892 +{
47893 + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
47894 + qtd->urb = urb;
47895 + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
47896 + /*
47897 + * The only time the QTD data toggle is used is on the data
47898 + * phase of control transfers. This phase always starts with
47899 + * DATA1.
47900 + */
47901 + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
47902 + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
47903 + }
47904 +
47905 + /* start split */
47906 + qtd->complete_split = 0;
47907 + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
47908 + qtd->isoc_split_offset = 0;
47909 + qtd->in_process = 0;
47910 +
47911 + /* Store the qtd ptr in the urb to reference what QTD. */
47912 + urb->qtd = qtd;
47913 + return;
47914 +}
47915 +
47916 +/**
47917 + * This function adds a QTD to the QTD-list of a QH. It will find the correct
47918 + * QH to place the QTD into. If it does not find a QH, then it will create a
47919 + * new QH. If the QH to which the QTD is added is not currently scheduled, it
47920 + * is placed into the proper schedule based on its EP type.
47921 + * HCD lock must be held and interrupts must be disabled on entry
47922 + *
47923 + * @param[in] qtd The QTD to add
47924 + * @param[in] hcd The DWC HCD structure
47925 + * @param[out] qh out parameter to return queue head
47926 + * @param atomic_alloc Flag to do atomic alloc if needed
47927 + *
47928 + * @return 0 if successful, negative error code otherwise.
47929 + */
47930 +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
47931 + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
47932 +{
47933 + int retval = 0;
47934 + dwc_otg_hcd_urb_t *urb = qtd->urb;
47935 +
47936 + /*
47937 + * Get the QH which holds the QTD-list to insert to. Create QH if it
47938 + * doesn't exist.
47939 + */
47940 + if (*qh == NULL) {
47941 + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
47942 + if (*qh == NULL) {
47943 + retval = -DWC_E_NO_MEMORY;
47944 + goto done;
47945 + } else {
47946 + if (fiq_enable)
47947 + hcd->fiq_state->kick_np_queues = 1;
47948 + }
47949 + }
47950 + retval = dwc_otg_hcd_qh_add(hcd, *qh);
47951 + if (retval == 0) {
47952 + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
47953 + qtd_list_entry);
47954 + qtd->qh = *qh;
47955 + }
47956 +done:
47957 +
47958 + return retval;
47959 +}
47960 +
47961 +#endif /* DWC_DEVICE_ONLY */
47962 --- /dev/null
47963 +++ b/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
47964 @@ -0,0 +1,188 @@
47965 +#ifndef _DWC_OS_DEP_H_
47966 +#define _DWC_OS_DEP_H_
47967 +
47968 +/**
47969 + * @file
47970 + *
47971 + * This file contains OS dependent structures.
47972 + *
47973 + */
47974 +
47975 +#include <linux/kernel.h>
47976 +#include <linux/module.h>
47977 +#include <linux/moduleparam.h>
47978 +#include <linux/init.h>
47979 +#include <linux/device.h>
47980 +#include <linux/errno.h>
47981 +#include <linux/types.h>
47982 +#include <linux/slab.h>
47983 +#include <linux/list.h>
47984 +#include <linux/interrupt.h>
47985 +#include <linux/ctype.h>
47986 +#include <linux/string.h>
47987 +#include <linux/dma-mapping.h>
47988 +#include <linux/jiffies.h>
47989 +#include <linux/delay.h>
47990 +#include <linux/timer.h>
47991 +#include <linux/workqueue.h>
47992 +#include <linux/stat.h>
47993 +#include <linux/pci.h>
47994 +
47995 +#include <linux/version.h>
47996 +
47997 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
47998 +# include <linux/irq.h>
47999 +#endif
48000 +
48001 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
48002 +# include <linux/usb/ch9.h>
48003 +#else
48004 +# include <linux/usb_ch9.h>
48005 +#endif
48006 +
48007 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
48008 +# include <linux/usb/gadget.h>
48009 +#else
48010 +# include <linux/usb_gadget.h>
48011 +#endif
48012 +
48013 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
48014 +# include <asm/irq.h>
48015 +#endif
48016 +
48017 +#ifdef PCI_INTERFACE
48018 +# include <asm/io.h>
48019 +#endif
48020 +
48021 +#ifdef LM_INTERFACE
48022 +# include <asm/unaligned.h>
48023 +# include <asm/sizes.h>
48024 +# include <asm/param.h>
48025 +# include <asm/io.h>
48026 +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
48027 +# include <asm/arch/hardware.h>
48028 +# include <asm/arch/lm.h>
48029 +# include <asm/arch/irqs.h>
48030 +# include <asm/arch/regs-irq.h>
48031 +# else
48032 +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
48033 + here we assume that the machine architecture provides definitions
48034 + in its own header
48035 +*/
48036 +# include <mach/lm.h>
48037 +# include <mach/hardware.h>
48038 +# endif
48039 +#endif
48040 +
48041 +#ifdef PLATFORM_INTERFACE
48042 +#include <linux/platform_device.h>
48043 +#include <asm/mach/map.h>
48044 +#endif
48045 +
48046 +/** The OS page size */
48047 +#define DWC_OS_PAGE_SIZE PAGE_SIZE
48048 +
48049 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
48050 +typedef int gfp_t;
48051 +#endif
48052 +
48053 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
48054 +# define IRQF_SHARED SA_SHIRQ
48055 +#endif
48056 +
48057 +typedef struct os_dependent {
48058 + /** Base address returned from ioremap() */
48059 + void *base;
48060 +
48061 + /** Register offset for Diagnostic API */
48062 + uint32_t reg_offset;
48063 +
48064 + /** Base address for MPHI peripheral */
48065 + void *mphi_base;
48066 +
48067 +#ifdef LM_INTERFACE
48068 + struct lm_device *lmdev;
48069 +#elif defined(PCI_INTERFACE)
48070 + struct pci_dev *pcidev;
48071 +
48072 + /** Start address of a PCI region */
48073 + resource_size_t rsrc_start;
48074 +
48075 + /** Length address of a PCI region */
48076 + resource_size_t rsrc_len;
48077 +#elif defined(PLATFORM_INTERFACE)
48078 + struct platform_device *platformdev;
48079 +#endif
48080 +
48081 +} os_dependent_t;
48082 +
48083 +#ifdef __cplusplus
48084 +}
48085 +#endif
48086 +
48087 +
48088 +
48089 +/* Type for the our device on the chosen bus */
48090 +#if defined(LM_INTERFACE)
48091 +typedef struct lm_device dwc_bus_dev_t;
48092 +#elif defined(PCI_INTERFACE)
48093 +typedef struct pci_dev dwc_bus_dev_t;
48094 +#elif defined(PLATFORM_INTERFACE)
48095 +typedef struct platform_device dwc_bus_dev_t;
48096 +#endif
48097 +
48098 +/* Helper macro to retrieve drvdata from the device on the chosen bus */
48099 +#if defined(LM_INTERFACE)
48100 +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
48101 +#elif defined(PCI_INTERFACE)
48102 +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
48103 +#elif defined(PLATFORM_INTERFACE)
48104 +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
48105 +#endif
48106 +
48107 +/**
48108 + * Helper macro returning the otg_device structure of a given struct device
48109 + *
48110 + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
48111 + */
48112 +#ifdef LM_INTERFACE
48113 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
48114 + struct lm_device *lm_dev = \
48115 + container_of(_dev, struct lm_device, dev); \
48116 + _var = lm_get_drvdata(lm_dev); \
48117 + } while (0)
48118 +
48119 +#elif defined(PCI_INTERFACE)
48120 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
48121 + _var = dev_get_drvdata(_dev); \
48122 + } while (0)
48123 +
48124 +#elif defined(PLATFORM_INTERFACE)
48125 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
48126 + struct platform_device *platform_dev = \
48127 + container_of(_dev, struct platform_device, dev); \
48128 + _var = platform_get_drvdata(platform_dev); \
48129 + } while (0)
48130 +#endif
48131 +
48132 +
48133 +/**
48134 + * Helper macro returning the struct dev of the given struct os_dependent
48135 + *
48136 + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
48137 + */
48138 +#ifdef LM_INTERFACE
48139 +#define DWC_OTG_OS_GETDEV(_osdep) \
48140 + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
48141 +#elif defined(PCI_INTERFACE)
48142 +#define DWC_OTG_OS_GETDEV(_osdep) \
48143 + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
48144 +#elif defined(PLATFORM_INTERFACE)
48145 +#define DWC_OTG_OS_GETDEV(_osdep) \
48146 + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
48147 +#endif
48148 +
48149 +
48150 +
48151 +
48152 +#endif /* _DWC_OS_DEP_H_ */
48153 --- /dev/null
48154 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
48155 @@ -0,0 +1,2725 @@
48156 +/* ==========================================================================
48157 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
48158 + * $Revision: #101 $
48159 + * $Date: 2012/08/10 $
48160 + * $Change: 2047372 $
48161 + *
48162 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
48163 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
48164 + * otherwise expressly agreed to in writing between Synopsys and you.
48165 + *
48166 + * The Software IS NOT an item of Licensed Software or Licensed Product under
48167 + * any End User Software License Agreement or Agreement for Licensed Product
48168 + * with Synopsys or any supplement thereto. You are permitted to use and
48169 + * redistribute this Software in source and binary forms, with or without
48170 + * modification, provided that redistributions of source code must retain this
48171 + * notice. You may not view, use, disclose, copy or distribute this file or
48172 + * any information contained herein except pursuant to this license grant from
48173 + * Synopsys. If you do not agree with this notice, including the disclaimer
48174 + * below, then you are not authorized to use the Software.
48175 + *
48176 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
48177 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48178 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48179 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
48180 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48181 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48182 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48183 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48184 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48185 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
48186 + * DAMAGE.
48187 + * ========================================================================== */
48188 +#ifndef DWC_HOST_ONLY
48189 +
48190 +/** @file
48191 + * This file implements PCD Core. All code in this file is portable and doesn't
48192 + * use any OS specific functions.
48193 + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
48194 + * header file, which can be used to implement OS specific PCD interface.
48195 + *
48196 + * An important function of the PCD is managing interrupts generated
48197 + * by the DWC_otg controller. The implementation of the DWC_otg device
48198 + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
48199 + *
48200 + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
48201 + * @todo Does it work when the request size is greater than DEPTSIZ
48202 + * transfer size
48203 + *
48204 + */
48205 +
48206 +#include "dwc_otg_pcd.h"
48207 +
48208 +#ifdef DWC_UTE_CFI
48209 +#include "dwc_otg_cfi.h"
48210 +
48211 +extern int init_cfi(cfiobject_t * cfiobj);
48212 +#endif
48213 +
48214 +/**
48215 + * Choose endpoint from ep arrays using usb_ep structure.
48216 + */
48217 +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
48218 +{
48219 + int i;
48220 + if (pcd->ep0.priv == handle) {
48221 + return &pcd->ep0;
48222 + }
48223 + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
48224 + if (pcd->in_ep[i].priv == handle)
48225 + return &pcd->in_ep[i];
48226 + if (pcd->out_ep[i].priv == handle)
48227 + return &pcd->out_ep[i];
48228 + }
48229 +
48230 + return NULL;
48231 +}
48232 +
48233 +/**
48234 + * This function completes a request. It call's the request call back.
48235 + */
48236 +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
48237 + int32_t status)
48238 +{
48239 + unsigned stopped = ep->stopped;
48240 +
48241 + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
48242 + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
48243 +
48244 + /* don't modify queue heads during completion callback */
48245 + ep->stopped = 1;
48246 + /* spin_unlock/spin_lock now done in fops->complete() */
48247 + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
48248 + req->actual);
48249 +
48250 + if (ep->pcd->request_pending > 0) {
48251 + --ep->pcd->request_pending;
48252 + }
48253 +
48254 + ep->stopped = stopped;
48255 + DWC_FREE(req);
48256 +}
48257 +
48258 +/**
48259 + * This function terminates all the requsts in the EP request queue.
48260 + */
48261 +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
48262 +{
48263 + dwc_otg_pcd_request_t *req;
48264 +
48265 + ep->stopped = 1;
48266 +
48267 + /* called with irqs blocked?? */
48268 + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
48269 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
48270 + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
48271 + }
48272 +}
48273 +
48274 +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
48275 + const struct dwc_otg_pcd_function_ops *fops)
48276 +{
48277 + pcd->fops = fops;
48278 +}
48279 +
48280 +/**
48281 + * PCD Callback function for initializing the PCD when switching to
48282 + * device mode.
48283 + *
48284 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48285 + */
48286 +static int32_t dwc_otg_pcd_start_cb(void *p)
48287 +{
48288 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48289 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
48290 +
48291 + /*
48292 + * Initialized the Core for Device mode.
48293 + */
48294 + if (dwc_otg_is_device_mode(core_if)) {
48295 + dwc_otg_core_dev_init(core_if);
48296 + /* Set core_if's lock pointer to the pcd->lock */
48297 + core_if->lock = pcd->lock;
48298 + }
48299 + return 1;
48300 +}
48301 +
48302 +/** CFI-specific buffer allocation function for EP */
48303 +#ifdef DWC_UTE_CFI
48304 +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
48305 + size_t buflen, int flags)
48306 +{
48307 + dwc_otg_pcd_ep_t *ep;
48308 + ep = get_ep_from_handle(pcd, pep);
48309 + if (!ep) {
48310 + DWC_WARN("bad ep\n");
48311 + return -DWC_E_INVALID;
48312 + }
48313 +
48314 + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
48315 + flags);
48316 +}
48317 +#else
48318 +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
48319 + size_t buflen, int flags);
48320 +#endif
48321 +
48322 +/**
48323 + * PCD Callback function for notifying the PCD when resuming from
48324 + * suspend.
48325 + *
48326 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48327 + */
48328 +static int32_t dwc_otg_pcd_resume_cb(void *p)
48329 +{
48330 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48331 +
48332 + if (pcd->fops->resume) {
48333 + pcd->fops->resume(pcd);
48334 + }
48335 +
48336 + /* Stop the SRP timeout timer. */
48337 + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
48338 + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
48339 + if (GET_CORE_IF(pcd)->srp_timer_started) {
48340 + GET_CORE_IF(pcd)->srp_timer_started = 0;
48341 + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
48342 + }
48343 + }
48344 + return 1;
48345 +}
48346 +
48347 +/**
48348 + * PCD Callback function for notifying the PCD device is suspended.
48349 + *
48350 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48351 + */
48352 +static int32_t dwc_otg_pcd_suspend_cb(void *p)
48353 +{
48354 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48355 +
48356 + if (pcd->fops->suspend) {
48357 + DWC_SPINUNLOCK(pcd->lock);
48358 + pcd->fops->suspend(pcd);
48359 + DWC_SPINLOCK(pcd->lock);
48360 + }
48361 +
48362 + return 1;
48363 +}
48364 +
48365 +/**
48366 + * PCD Callback function for stopping the PCD when switching to Host
48367 + * mode.
48368 + *
48369 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48370 + */
48371 +static int32_t dwc_otg_pcd_stop_cb(void *p)
48372 +{
48373 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48374 + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
48375 +
48376 + dwc_otg_pcd_stop(pcd);
48377 + return 1;
48378 +}
48379 +
48380 +/**
48381 + * PCD Callback structure for handling mode switching.
48382 + */
48383 +static dwc_otg_cil_callbacks_t pcd_callbacks = {
48384 + .start = dwc_otg_pcd_start_cb,
48385 + .stop = dwc_otg_pcd_stop_cb,
48386 + .suspend = dwc_otg_pcd_suspend_cb,
48387 + .resume_wakeup = dwc_otg_pcd_resume_cb,
48388 + .p = 0, /* Set at registration */
48389 +};
48390 +
48391 +/**
48392 + * This function allocates a DMA Descriptor chain for the Endpoint
48393 + * buffer to be used for a transfer to/from the specified endpoint.
48394 + */
48395 +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(struct device *dev,
48396 + dwc_dma_t * dma_desc_addr,
48397 + uint32_t count)
48398 +{
48399 + return DWC_DMA_ALLOC_ATOMIC(dev, count * sizeof(dwc_otg_dev_dma_desc_t),
48400 + dma_desc_addr);
48401 +}
48402 +
48403 +/**
48404 + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
48405 + */
48406 +void dwc_otg_ep_free_desc_chain(struct device *dev,
48407 + dwc_otg_dev_dma_desc_t * desc_addr,
48408 + uint32_t dma_desc_addr, uint32_t count)
48409 +{
48410 + DWC_DMA_FREE(dev, count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
48411 + dma_desc_addr);
48412 +}
48413 +
48414 +#ifdef DWC_EN_ISOC
48415 +
48416 +/**
48417 + * This function initializes a descriptor chain for Isochronous transfer
48418 + *
48419 + * @param core_if Programming view of DWC_otg controller.
48420 + * @param dwc_ep The EP to start the transfer on.
48421 + *
48422 + */
48423 +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
48424 + dwc_ep_t * dwc_ep)
48425 +{
48426 +
48427 + dsts_data_t dsts = {.d32 = 0 };
48428 + depctl_data_t depctl = {.d32 = 0 };
48429 + volatile uint32_t *addr;
48430 + int i, j;
48431 + uint32_t len;
48432 +
48433 + if (dwc_ep->is_in)
48434 + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
48435 + else
48436 + dwc_ep->desc_cnt =
48437 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48438 + dwc_ep->bInterval;
48439 +
48440 + /** Allocate descriptors for double buffering */
48441 + dwc_ep->iso_desc_addr =
48442 + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
48443 + dwc_ep->desc_cnt * 2);
48444 + if (dwc_ep->desc_addr) {
48445 + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
48446 + return;
48447 + }
48448 +
48449 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48450 +
48451 + /** ISO OUT EP */
48452 + if (dwc_ep->is_in == 0) {
48453 + dev_dma_desc_sts_t sts = {.d32 = 0 };
48454 + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
48455 + dma_addr_t dma_ad;
48456 + uint32_t data_per_desc;
48457 + dwc_otg_dev_out_ep_regs_t *out_regs =
48458 + core_if->dev_if->out_ep_regs[dwc_ep->num];
48459 + int offset;
48460 +
48461 + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
48462 + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
48463 +
48464 + /** Buffer 0 descriptors setup */
48465 + dma_ad = dwc_ep->dma_addr0;
48466 +
48467 + sts.b_iso_out.bs = BS_HOST_READY;
48468 + sts.b_iso_out.rxsts = 0;
48469 + sts.b_iso_out.l = 0;
48470 + sts.b_iso_out.sp = 0;
48471 + sts.b_iso_out.ioc = 0;
48472 + sts.b_iso_out.pid = 0;
48473 + sts.b_iso_out.framenum = 0;
48474 +
48475 + offset = 0;
48476 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48477 + i += dwc_ep->pkt_per_frm) {
48478 +
48479 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
48480 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48481 + if (len > dwc_ep->data_per_frame)
48482 + data_per_desc =
48483 + dwc_ep->data_per_frame -
48484 + j * dwc_ep->maxpacket;
48485 + else
48486 + data_per_desc = dwc_ep->maxpacket;
48487 + len = data_per_desc % 4;
48488 + if (len)
48489 + data_per_desc += 4 - len;
48490 +
48491 + sts.b_iso_out.rxbytes = data_per_desc;
48492 + dma_desc->buf = dma_ad;
48493 + dma_desc->status.d32 = sts.d32;
48494 +
48495 + offset += data_per_desc;
48496 + dma_desc++;
48497 + dma_ad += data_per_desc;
48498 + }
48499 + }
48500 +
48501 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
48502 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48503 + if (len > dwc_ep->data_per_frame)
48504 + data_per_desc =
48505 + dwc_ep->data_per_frame -
48506 + j * dwc_ep->maxpacket;
48507 + else
48508 + data_per_desc = dwc_ep->maxpacket;
48509 + len = data_per_desc % 4;
48510 + if (len)
48511 + data_per_desc += 4 - len;
48512 + sts.b_iso_out.rxbytes = data_per_desc;
48513 + dma_desc->buf = dma_ad;
48514 + dma_desc->status.d32 = sts.d32;
48515 +
48516 + offset += data_per_desc;
48517 + dma_desc++;
48518 + dma_ad += data_per_desc;
48519 + }
48520 +
48521 + sts.b_iso_out.ioc = 1;
48522 + len = (j + 1) * dwc_ep->maxpacket;
48523 + if (len > dwc_ep->data_per_frame)
48524 + data_per_desc =
48525 + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
48526 + else
48527 + data_per_desc = dwc_ep->maxpacket;
48528 + len = data_per_desc % 4;
48529 + if (len)
48530 + data_per_desc += 4 - len;
48531 + sts.b_iso_out.rxbytes = data_per_desc;
48532 +
48533 + dma_desc->buf = dma_ad;
48534 + dma_desc->status.d32 = sts.d32;
48535 + dma_desc++;
48536 +
48537 + /** Buffer 1 descriptors setup */
48538 + sts.b_iso_out.ioc = 0;
48539 + dma_ad = dwc_ep->dma_addr1;
48540 +
48541 + offset = 0;
48542 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48543 + i += dwc_ep->pkt_per_frm) {
48544 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
48545 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48546 + if (len > dwc_ep->data_per_frame)
48547 + data_per_desc =
48548 + dwc_ep->data_per_frame -
48549 + j * dwc_ep->maxpacket;
48550 + else
48551 + data_per_desc = dwc_ep->maxpacket;
48552 + len = data_per_desc % 4;
48553 + if (len)
48554 + data_per_desc += 4 - len;
48555 +
48556 + data_per_desc =
48557 + sts.b_iso_out.rxbytes = data_per_desc;
48558 + dma_desc->buf = dma_ad;
48559 + dma_desc->status.d32 = sts.d32;
48560 +
48561 + offset += data_per_desc;
48562 + dma_desc++;
48563 + dma_ad += data_per_desc;
48564 + }
48565 + }
48566 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
48567 + data_per_desc =
48568 + ((j + 1) * dwc_ep->maxpacket >
48569 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
48570 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
48571 + data_per_desc +=
48572 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
48573 + sts.b_iso_out.rxbytes = data_per_desc;
48574 + dma_desc->buf = dma_ad;
48575 + dma_desc->status.d32 = sts.d32;
48576 +
48577 + offset += data_per_desc;
48578 + dma_desc++;
48579 + dma_ad += data_per_desc;
48580 + }
48581 +
48582 + sts.b_iso_out.ioc = 1;
48583 + sts.b_iso_out.l = 1;
48584 + data_per_desc =
48585 + ((j + 1) * dwc_ep->maxpacket >
48586 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
48587 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
48588 + data_per_desc +=
48589 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
48590 + sts.b_iso_out.rxbytes = data_per_desc;
48591 +
48592 + dma_desc->buf = dma_ad;
48593 + dma_desc->status.d32 = sts.d32;
48594 +
48595 + dwc_ep->next_frame = 0;
48596 +
48597 + /** Write dma_ad into DOEPDMA register */
48598 + DWC_WRITE_REG32(&(out_regs->doepdma),
48599 + (uint32_t) dwc_ep->iso_dma_desc_addr);
48600 +
48601 + }
48602 + /** ISO IN EP */
48603 + else {
48604 + dev_dma_desc_sts_t sts = {.d32 = 0 };
48605 + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
48606 + dma_addr_t dma_ad;
48607 + dwc_otg_dev_in_ep_regs_t *in_regs =
48608 + core_if->dev_if->in_ep_regs[dwc_ep->num];
48609 + unsigned int frmnumber;
48610 + fifosize_data_t txfifosize, rxfifosize;
48611 +
48612 + txfifosize.d32 =
48613 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
48614 + dtxfsts);
48615 + rxfifosize.d32 =
48616 + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
48617 +
48618 + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
48619 +
48620 + dma_ad = dwc_ep->dma_addr0;
48621 +
48622 + dsts.d32 =
48623 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48624 +
48625 + sts.b_iso_in.bs = BS_HOST_READY;
48626 + sts.b_iso_in.txsts = 0;
48627 + sts.b_iso_in.sp =
48628 + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
48629 + sts.b_iso_in.ioc = 0;
48630 + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
48631 +
48632 + frmnumber = dwc_ep->next_frame;
48633 +
48634 + sts.b_iso_in.framenum = frmnumber;
48635 + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
48636 + sts.b_iso_in.l = 0;
48637 +
48638 + /** Buffer 0 descriptors setup */
48639 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
48640 + dma_desc->buf = dma_ad;
48641 + dma_desc->status.d32 = sts.d32;
48642 + dma_desc++;
48643 +
48644 + dma_ad += dwc_ep->data_per_frame;
48645 + sts.b_iso_in.framenum += dwc_ep->bInterval;
48646 + }
48647 +
48648 + sts.b_iso_in.ioc = 1;
48649 + dma_desc->buf = dma_ad;
48650 + dma_desc->status.d32 = sts.d32;
48651 + ++dma_desc;
48652 +
48653 + /** Buffer 1 descriptors setup */
48654 + sts.b_iso_in.ioc = 0;
48655 + dma_ad = dwc_ep->dma_addr1;
48656 +
48657 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48658 + i += dwc_ep->pkt_per_frm) {
48659 + dma_desc->buf = dma_ad;
48660 + dma_desc->status.d32 = sts.d32;
48661 + dma_desc++;
48662 +
48663 + dma_ad += dwc_ep->data_per_frame;
48664 + sts.b_iso_in.framenum += dwc_ep->bInterval;
48665 +
48666 + sts.b_iso_in.ioc = 0;
48667 + }
48668 + sts.b_iso_in.ioc = 1;
48669 + sts.b_iso_in.l = 1;
48670 +
48671 + dma_desc->buf = dma_ad;
48672 + dma_desc->status.d32 = sts.d32;
48673 +
48674 + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
48675 +
48676 + /** Write dma_ad into diepdma register */
48677 + DWC_WRITE_REG32(&(in_regs->diepdma),
48678 + (uint32_t) dwc_ep->iso_dma_desc_addr);
48679 + }
48680 + /** Enable endpoint, clear nak */
48681 + depctl.d32 = 0;
48682 + depctl.b.epena = 1;
48683 + depctl.b.usbactep = 1;
48684 + depctl.b.cnak = 1;
48685 +
48686 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
48687 + depctl.d32 = DWC_READ_REG32(addr);
48688 +}
48689 +
48690 +/**
48691 + * This function initializes a descriptor chain for Isochronous transfer
48692 + *
48693 + * @param core_if Programming view of DWC_otg controller.
48694 + * @param ep The EP to start the transfer on.
48695 + *
48696 + */
48697 +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
48698 + dwc_ep_t * ep)
48699 +{
48700 + depctl_data_t depctl = {.d32 = 0 };
48701 + volatile uint32_t *addr;
48702 +
48703 + if (ep->is_in) {
48704 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
48705 + } else {
48706 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
48707 + }
48708 +
48709 + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
48710 + return;
48711 + } else {
48712 + deptsiz_data_t deptsiz = {.d32 = 0 };
48713 +
48714 + ep->xfer_len =
48715 + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
48716 + ep->pkt_cnt =
48717 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
48718 + ep->xfer_count = 0;
48719 + ep->xfer_buff =
48720 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
48721 + ep->dma_addr =
48722 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
48723 +
48724 + if (ep->is_in) {
48725 + /* Program the transfer size and packet count
48726 + * as follows: xfersize = N * maxpacket +
48727 + * short_packet pktcnt = N + (short_packet
48728 + * exist ? 1 : 0)
48729 + */
48730 + deptsiz.b.mc = ep->pkt_per_frm;
48731 + deptsiz.b.xfersize = ep->xfer_len;
48732 + deptsiz.b.pktcnt =
48733 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
48734 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
48735 + dieptsiz, deptsiz.d32);
48736 +
48737 + /* Write the DMA register */
48738 + DWC_WRITE_REG32(&
48739 + (core_if->dev_if->in_ep_regs[ep->num]->
48740 + diepdma), (uint32_t) ep->dma_addr);
48741 +
48742 + } else {
48743 + deptsiz.b.pktcnt =
48744 + (ep->xfer_len + (ep->maxpacket - 1)) /
48745 + ep->maxpacket;
48746 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
48747 +
48748 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
48749 + doeptsiz, deptsiz.d32);
48750 +
48751 + /* Write the DMA register */
48752 + DWC_WRITE_REG32(&
48753 + (core_if->dev_if->out_ep_regs[ep->num]->
48754 + doepdma), (uint32_t) ep->dma_addr);
48755 +
48756 + }
48757 + /** Enable endpoint, clear nak */
48758 + depctl.d32 = 0;
48759 + depctl.b.epena = 1;
48760 + depctl.b.cnak = 1;
48761 +
48762 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
48763 + }
48764 +}
48765 +
48766 +/**
48767 + * This function does the setup for a data transfer for an EP and
48768 + * starts the transfer. For an IN transfer, the packets will be
48769 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
48770 + * the packets are unloaded from the Rx FIFO in the ISR.
48771 + *
48772 + * @param core_if Programming view of DWC_otg controller.
48773 + * @param ep The EP to start the transfer on.
48774 + */
48775 +
48776 +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
48777 + dwc_ep_t * ep)
48778 +{
48779 + if (core_if->dma_enable) {
48780 + if (core_if->dma_desc_enable) {
48781 + if (ep->is_in) {
48782 + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
48783 + } else {
48784 + ep->desc_cnt = ep->pkt_cnt;
48785 + }
48786 + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
48787 + } else {
48788 + if (core_if->pti_enh_enable) {
48789 + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
48790 + } else {
48791 + ep->cur_pkt_addr =
48792 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
48793 + xfer_buff0;
48794 + ep->cur_pkt_dma_addr =
48795 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
48796 + dma_addr0;
48797 + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
48798 + }
48799 + }
48800 + } else {
48801 + ep->cur_pkt_addr =
48802 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
48803 + ep->cur_pkt_dma_addr =
48804 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
48805 + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
48806 + }
48807 +}
48808 +
48809 +/**
48810 + * This function stops transfer for an EP and
48811 + * resets the ep's variables.
48812 + *
48813 + * @param core_if Programming view of DWC_otg controller.
48814 + * @param ep The EP to start the transfer on.
48815 + */
48816 +
48817 +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
48818 +{
48819 + depctl_data_t depctl = {.d32 = 0 };
48820 + volatile uint32_t *addr;
48821 +
48822 + if (ep->is_in == 1) {
48823 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
48824 + } else {
48825 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
48826 + }
48827 +
48828 + /* disable the ep */
48829 + depctl.d32 = DWC_READ_REG32(addr);
48830 +
48831 + depctl.b.epdis = 1;
48832 + depctl.b.snak = 1;
48833 +
48834 + DWC_WRITE_REG32(addr, depctl.d32);
48835 +
48836 + if (core_if->dma_desc_enable &&
48837 + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
48838 + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
48839 + ep->iso_dma_desc_addr,
48840 + ep->desc_cnt * 2);
48841 + }
48842 +
48843 + /* reset varibales */
48844 + ep->dma_addr0 = 0;
48845 + ep->dma_addr1 = 0;
48846 + ep->xfer_buff0 = 0;
48847 + ep->xfer_buff1 = 0;
48848 + ep->data_per_frame = 0;
48849 + ep->data_pattern_frame = 0;
48850 + ep->sync_frame = 0;
48851 + ep->buf_proc_intrvl = 0;
48852 + ep->bInterval = 0;
48853 + ep->proc_buf_num = 0;
48854 + ep->pkt_per_frm = 0;
48855 + ep->pkt_per_frm = 0;
48856 + ep->desc_cnt = 0;
48857 + ep->iso_desc_addr = 0;
48858 + ep->iso_dma_desc_addr = 0;
48859 +}
48860 +
48861 +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
48862 + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
48863 + dwc_dma_t dma1, int sync_frame, int dp_frame,
48864 + int data_per_frame, int start_frame,
48865 + int buf_proc_intrvl, void *req_handle,
48866 + int atomic_alloc)
48867 +{
48868 + dwc_otg_pcd_ep_t *ep;
48869 + dwc_irqflags_t flags = 0;
48870 + dwc_ep_t *dwc_ep;
48871 + int32_t frm_data;
48872 + dsts_data_t dsts;
48873 + dwc_otg_core_if_t *core_if;
48874 +
48875 + ep = get_ep_from_handle(pcd, ep_handle);
48876 +
48877 + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
48878 + DWC_WARN("bad ep\n");
48879 + return -DWC_E_INVALID;
48880 + }
48881 +
48882 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
48883 + core_if = GET_CORE_IF(pcd);
48884 + dwc_ep = &ep->dwc_ep;
48885 +
48886 + if (ep->iso_req_handle) {
48887 + DWC_WARN("ISO request in progress\n");
48888 + }
48889 +
48890 + dwc_ep->dma_addr0 = dma0;
48891 + dwc_ep->dma_addr1 = dma1;
48892 +
48893 + dwc_ep->xfer_buff0 = buf0;
48894 + dwc_ep->xfer_buff1 = buf1;
48895 +
48896 + dwc_ep->data_per_frame = data_per_frame;
48897 +
48898 + /** @todo - pattern data support is to be implemented in the future */
48899 + dwc_ep->data_pattern_frame = dp_frame;
48900 + dwc_ep->sync_frame = sync_frame;
48901 +
48902 + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
48903 +
48904 + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
48905 +
48906 + dwc_ep->proc_buf_num = 0;
48907 +
48908 + dwc_ep->pkt_per_frm = 0;
48909 + frm_data = ep->dwc_ep.data_per_frame;
48910 + while (frm_data > 0) {
48911 + dwc_ep->pkt_per_frm++;
48912 + frm_data -= ep->dwc_ep.maxpacket;
48913 + }
48914 +
48915 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48916 +
48917 + if (start_frame == -1) {
48918 + dwc_ep->next_frame = dsts.b.soffn + 1;
48919 + if (dwc_ep->bInterval != 1) {
48920 + dwc_ep->next_frame =
48921 + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
48922 + dwc_ep->next_frame %
48923 + dwc_ep->bInterval);
48924 + }
48925 + } else {
48926 + dwc_ep->next_frame = start_frame;
48927 + }
48928 +
48929 + if (!core_if->pti_enh_enable) {
48930 + dwc_ep->pkt_cnt =
48931 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48932 + dwc_ep->bInterval;
48933 + } else {
48934 + dwc_ep->pkt_cnt =
48935 + (dwc_ep->data_per_frame *
48936 + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
48937 + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
48938 + }
48939 +
48940 + if (core_if->dma_desc_enable) {
48941 + dwc_ep->desc_cnt =
48942 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48943 + dwc_ep->bInterval;
48944 + }
48945 +
48946 + if (atomic_alloc) {
48947 + dwc_ep->pkt_info =
48948 + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48949 + } else {
48950 + dwc_ep->pkt_info =
48951 + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48952 + }
48953 + if (!dwc_ep->pkt_info) {
48954 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48955 + return -DWC_E_NO_MEMORY;
48956 + }
48957 + if (core_if->pti_enh_enable) {
48958 + dwc_memset(dwc_ep->pkt_info, 0,
48959 + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48960 + }
48961 +
48962 + dwc_ep->cur_pkt = 0;
48963 + ep->iso_req_handle = req_handle;
48964 +
48965 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48966 + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
48967 + return 0;
48968 +}
48969 +
48970 +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
48971 + void *req_handle)
48972 +{
48973 + dwc_irqflags_t flags = 0;
48974 + dwc_otg_pcd_ep_t *ep;
48975 + dwc_ep_t *dwc_ep;
48976 +
48977 + ep = get_ep_from_handle(pcd, ep_handle);
48978 + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
48979 + DWC_WARN("bad ep\n");
48980 + return -DWC_E_INVALID;
48981 + }
48982 + dwc_ep = &ep->dwc_ep;
48983 +
48984 + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
48985 +
48986 + DWC_FREE(dwc_ep->pkt_info);
48987 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
48988 + if (ep->iso_req_handle != req_handle) {
48989 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48990 + return -DWC_E_INVALID;
48991 + }
48992 +
48993 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48994 +
48995 + ep->iso_req_handle = 0;
48996 + return 0;
48997 +}
48998 +
48999 +/**
49000 + * This function is used for perodical data exchnage between PCD and gadget drivers.
49001 + * for Isochronous EPs
49002 + *
49003 + * - Every time a sync period completes this function is called to
49004 + * perform data exchange between PCD and gadget
49005 + */
49006 +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
49007 + void *req_handle)
49008 +{
49009 + int i;
49010 + dwc_ep_t *dwc_ep;
49011 +
49012 + dwc_ep = &ep->dwc_ep;
49013 +
49014 + DWC_SPINUNLOCK(ep->pcd->lock);
49015 + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
49016 + dwc_ep->proc_buf_num ^ 0x1);
49017 + DWC_SPINLOCK(ep->pcd->lock);
49018 +
49019 + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
49020 + dwc_ep->pkt_info[i].status = 0;
49021 + dwc_ep->pkt_info[i].offset = 0;
49022 + dwc_ep->pkt_info[i].length = 0;
49023 + }
49024 +}
49025 +
49026 +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
49027 + void *iso_req_handle)
49028 +{
49029 + dwc_otg_pcd_ep_t *ep;
49030 + dwc_ep_t *dwc_ep;
49031 +
49032 + ep = get_ep_from_handle(pcd, ep_handle);
49033 + if (!ep->desc || ep->dwc_ep.num == 0) {
49034 + DWC_WARN("bad ep\n");
49035 + return -DWC_E_INVALID;
49036 + }
49037 + dwc_ep = &ep->dwc_ep;
49038 +
49039 + return dwc_ep->pkt_cnt;
49040 +}
49041 +
49042 +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
49043 + void *iso_req_handle, int packet,
49044 + int *status, int *actual, int *offset)
49045 +{
49046 + dwc_otg_pcd_ep_t *ep;
49047 + dwc_ep_t *dwc_ep;
49048 +
49049 + ep = get_ep_from_handle(pcd, ep_handle);
49050 + if (!ep)
49051 + DWC_WARN("bad ep\n");
49052 +
49053 + dwc_ep = &ep->dwc_ep;
49054 +
49055 + *status = dwc_ep->pkt_info[packet].status;
49056 + *actual = dwc_ep->pkt_info[packet].length;
49057 + *offset = dwc_ep->pkt_info[packet].offset;
49058 +}
49059 +
49060 +#endif /* DWC_EN_ISOC */
49061 +
49062 +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
49063 + uint32_t is_in, uint32_t ep_num)
49064 +{
49065 + /* Init EP structure */
49066 + pcd_ep->desc = 0;
49067 + pcd_ep->pcd = pcd;
49068 + pcd_ep->stopped = 1;
49069 + pcd_ep->queue_sof = 0;
49070 +
49071 + /* Init DWC ep structure */
49072 + pcd_ep->dwc_ep.is_in = is_in;
49073 + pcd_ep->dwc_ep.num = ep_num;
49074 + pcd_ep->dwc_ep.active = 0;
49075 + pcd_ep->dwc_ep.tx_fifo_num = 0;
49076 + /* Control until ep is actvated */
49077 + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
49078 + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
49079 + pcd_ep->dwc_ep.dma_addr = 0;
49080 + pcd_ep->dwc_ep.start_xfer_buff = 0;
49081 + pcd_ep->dwc_ep.xfer_buff = 0;
49082 + pcd_ep->dwc_ep.xfer_len = 0;
49083 + pcd_ep->dwc_ep.xfer_count = 0;
49084 + pcd_ep->dwc_ep.sent_zlp = 0;
49085 + pcd_ep->dwc_ep.total_len = 0;
49086 + pcd_ep->dwc_ep.desc_addr = 0;
49087 + pcd_ep->dwc_ep.dma_desc_addr = 0;
49088 + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
49089 +}
49090 +
49091 +/**
49092 + * Initialize ep's
49093 + */
49094 +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
49095 +{
49096 + int i;
49097 + uint32_t hwcfg1;
49098 + dwc_otg_pcd_ep_t *ep;
49099 + int in_ep_cntr, out_ep_cntr;
49100 + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
49101 + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
49102 +
49103 + /**
49104 + * Initialize the EP0 structure.
49105 + */
49106 + ep = &pcd->ep0;
49107 + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
49108 +
49109 + in_ep_cntr = 0;
49110 + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
49111 + for (i = 1; in_ep_cntr < num_in_eps; i++) {
49112 + if ((hwcfg1 & 0x1) == 0) {
49113 + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
49114 + in_ep_cntr++;
49115 + /**
49116 + * @todo NGS: Add direction to EP, based on contents
49117 + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
49118 + * sprintf(";r
49119 + */
49120 + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
49121 +
49122 + DWC_CIRCLEQ_INIT(&ep->queue);
49123 + }
49124 + hwcfg1 >>= 2;
49125 + }
49126 +
49127 + out_ep_cntr = 0;
49128 + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
49129 + for (i = 1; out_ep_cntr < num_out_eps; i++) {
49130 + if ((hwcfg1 & 0x1) == 0) {
49131 + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
49132 + out_ep_cntr++;
49133 + /**
49134 + * @todo NGS: Add direction to EP, based on contents
49135 + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
49136 + * sprintf(";r
49137 + */
49138 + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
49139 + DWC_CIRCLEQ_INIT(&ep->queue);
49140 + }
49141 + hwcfg1 >>= 2;
49142 + }
49143 +
49144 + pcd->ep0state = EP0_DISCONNECT;
49145 + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
49146 + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
49147 +}
49148 +
49149 +/**
49150 + * This function is called when the SRP timer expires. The SRP should
49151 + * complete within 6 seconds.
49152 + */
49153 +static void srp_timeout(void *ptr)
49154 +{
49155 + gotgctl_data_t gotgctl;
49156 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
49157 + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
49158 +
49159 + gotgctl.d32 = DWC_READ_REG32(addr);
49160 +
49161 + core_if->srp_timer_started = 0;
49162 +
49163 + if (core_if->adp_enable) {
49164 + if (gotgctl.b.bsesvld == 0) {
49165 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
49166 + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
49167 + /* Power off the core */
49168 + if (core_if->power_down == 2) {
49169 + gpwrdn.b.pwrdnswtch = 1;
49170 + DWC_MODIFY_REG32(&core_if->
49171 + core_global_regs->gpwrdn,
49172 + gpwrdn.d32, 0);
49173 + }
49174 +
49175 + gpwrdn.d32 = 0;
49176 + gpwrdn.b.pmuintsel = 1;
49177 + gpwrdn.b.pmuactv = 1;
49178 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
49179 + gpwrdn.d32);
49180 + dwc_otg_adp_probe_start(core_if);
49181 + } else {
49182 + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
49183 + core_if->op_state = B_PERIPHERAL;
49184 + dwc_otg_core_init(core_if);
49185 + dwc_otg_enable_global_interrupts(core_if);
49186 + cil_pcd_start(core_if);
49187 + }
49188 + }
49189 +
49190 + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
49191 + (core_if->core_params->i2c_enable)) {
49192 + DWC_PRINTF("SRP Timeout\n");
49193 +
49194 + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
49195 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
49196 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
49197 + }
49198 +
49199 + /* Clear Session Request */
49200 + gotgctl.d32 = 0;
49201 + gotgctl.b.sesreq = 1;
49202 + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
49203 + gotgctl.d32, 0);
49204 +
49205 + core_if->srp_success = 0;
49206 + } else {
49207 + __DWC_ERROR("Device not connected/responding\n");
49208 + gotgctl.b.sesreq = 0;
49209 + DWC_WRITE_REG32(addr, gotgctl.d32);
49210 + }
49211 + } else if (gotgctl.b.sesreq) {
49212 + DWC_PRINTF("SRP Timeout\n");
49213 +
49214 + __DWC_ERROR("Device not connected/responding\n");
49215 + gotgctl.b.sesreq = 0;
49216 + DWC_WRITE_REG32(addr, gotgctl.d32);
49217 + } else {
49218 + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
49219 + }
49220 +}
49221 +
49222 +/**
49223 + * Tasklet
49224 + *
49225 + */
49226 +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
49227 +
49228 +static void start_xfer_tasklet_func(void *data)
49229 +{
49230 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
49231 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49232 +
49233 + int i;
49234 + depctl_data_t diepctl;
49235 +
49236 + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
49237 +
49238 + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
49239 +
49240 + if (pcd->ep0.queue_sof) {
49241 + pcd->ep0.queue_sof = 0;
49242 + start_next_request(&pcd->ep0);
49243 + // break;
49244 + }
49245 +
49246 + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
49247 + depctl_data_t diepctl;
49248 + diepctl.d32 =
49249 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
49250 +
49251 + if (pcd->in_ep[i].queue_sof) {
49252 + pcd->in_ep[i].queue_sof = 0;
49253 + start_next_request(&pcd->in_ep[i]);
49254 + // break;
49255 + }
49256 + }
49257 +
49258 + return;
49259 +}
49260 +
49261 +/**
49262 + * This function initialized the PCD portion of the driver.
49263 + *
49264 + */
49265 +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev)
49266 +{
49267 + struct device *dev = &otg_dev->os_dep.platformdev->dev;
49268 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
49269 + dwc_otg_pcd_t *pcd = NULL;
49270 + dwc_otg_dev_if_t *dev_if;
49271 + int i;
49272 +
49273 + /*
49274 + * Allocate PCD structure
49275 + */
49276 + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
49277 +
49278 + if (pcd == NULL) {
49279 + return NULL;
49280 + }
49281 +
49282 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
49283 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
49284 +#else
49285 + pcd->lock = DWC_SPINLOCK_ALLOC();
49286 +#endif
49287 + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
49288 + pcd, core_if);//GRAYG
49289 + if (!pcd->lock) {
49290 + DWC_ERROR("Could not allocate lock for pcd");
49291 + DWC_FREE(pcd);
49292 + return NULL;
49293 + }
49294 + /* Set core_if's lock pointer to hcd->lock */
49295 + core_if->lock = pcd->lock;
49296 + pcd->core_if = core_if;
49297 +
49298 + dev_if = core_if->dev_if;
49299 + dev_if->isoc_ep = NULL;
49300 +
49301 + if (core_if->hwcfg4.b.ded_fifo_en) {
49302 + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
49303 + } else {
49304 + DWC_PRINTF("Shared Tx FIFO mode\n");
49305 + }
49306 +
49307 + /*
49308 + * Initialized the Core for Device mode here if there is nod ADP support.
49309 + * Otherwise it will be done later in dwc_otg_adp_start routine.
49310 + */
49311 + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
49312 + dwc_otg_core_dev_init(core_if);
49313 + }
49314 +
49315 + /*
49316 + * Register the PCD Callbacks.
49317 + */
49318 + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
49319 +
49320 + /*
49321 + * Initialize the DMA buffer for SETUP packets
49322 + */
49323 + if (GET_CORE_IF(pcd)->dma_enable) {
49324 + pcd->setup_pkt =
49325 + DWC_DMA_ALLOC(dev, sizeof(*pcd->setup_pkt) * 5,
49326 + &pcd->setup_pkt_dma_handle);
49327 + if (pcd->setup_pkt == NULL) {
49328 + DWC_FREE(pcd);
49329 + return NULL;
49330 + }
49331 +
49332 + pcd->status_buf =
49333 + DWC_DMA_ALLOC(dev, sizeof(uint16_t),
49334 + &pcd->status_buf_dma_handle);
49335 + if (pcd->status_buf == NULL) {
49336 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
49337 + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
49338 + DWC_FREE(pcd);
49339 + return NULL;
49340 + }
49341 +
49342 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49343 + dev_if->setup_desc_addr[0] =
49344 + dwc_otg_ep_alloc_desc_chain(dev,
49345 + &dev_if->dma_setup_desc_addr[0], 1);
49346 + dev_if->setup_desc_addr[1] =
49347 + dwc_otg_ep_alloc_desc_chain(dev,
49348 + &dev_if->dma_setup_desc_addr[1], 1);
49349 + dev_if->in_desc_addr =
49350 + dwc_otg_ep_alloc_desc_chain(dev,
49351 + &dev_if->dma_in_desc_addr, 1);
49352 + dev_if->out_desc_addr =
49353 + dwc_otg_ep_alloc_desc_chain(dev,
49354 + &dev_if->dma_out_desc_addr, 1);
49355 + pcd->data_terminated = 0;
49356 +
49357 + if (dev_if->setup_desc_addr[0] == 0
49358 + || dev_if->setup_desc_addr[1] == 0
49359 + || dev_if->in_desc_addr == 0
49360 + || dev_if->out_desc_addr == 0) {
49361 +
49362 + if (dev_if->out_desc_addr)
49363 + dwc_otg_ep_free_desc_chain(dev,
49364 + dev_if->out_desc_addr,
49365 + dev_if->dma_out_desc_addr, 1);
49366 + if (dev_if->in_desc_addr)
49367 + dwc_otg_ep_free_desc_chain(dev,
49368 + dev_if->in_desc_addr,
49369 + dev_if->dma_in_desc_addr, 1);
49370 + if (dev_if->setup_desc_addr[1])
49371 + dwc_otg_ep_free_desc_chain(dev,
49372 + dev_if->setup_desc_addr[1],
49373 + dev_if->dma_setup_desc_addr[1], 1);
49374 + if (dev_if->setup_desc_addr[0])
49375 + dwc_otg_ep_free_desc_chain(dev,
49376 + dev_if->setup_desc_addr[0],
49377 + dev_if->dma_setup_desc_addr[0], 1);
49378 +
49379 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
49380 + pcd->setup_pkt,
49381 + pcd->setup_pkt_dma_handle);
49382 + DWC_DMA_FREE(dev, sizeof(*pcd->status_buf),
49383 + pcd->status_buf,
49384 + pcd->status_buf_dma_handle);
49385 +
49386 + DWC_FREE(pcd);
49387 +
49388 + return NULL;
49389 + }
49390 + }
49391 + } else {
49392 + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
49393 + if (pcd->setup_pkt == NULL) {
49394 + DWC_FREE(pcd);
49395 + return NULL;
49396 + }
49397 +
49398 + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
49399 + if (pcd->status_buf == NULL) {
49400 + DWC_FREE(pcd->setup_pkt);
49401 + DWC_FREE(pcd);
49402 + return NULL;
49403 + }
49404 + }
49405 +
49406 + dwc_otg_pcd_reinit(pcd);
49407 +
49408 + /* Allocate the cfi object for the PCD */
49409 +#ifdef DWC_UTE_CFI
49410 + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
49411 + if (NULL == pcd->cfi)
49412 + goto fail;
49413 + if (init_cfi(pcd->cfi)) {
49414 + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
49415 + goto fail;
49416 + }
49417 +#endif
49418 +
49419 + /* Initialize tasklets */
49420 + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
49421 + start_xfer_tasklet_func, pcd);
49422 + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
49423 + do_test_mode, pcd);
49424 +
49425 + /* Initialize SRP timer */
49426 + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
49427 +
49428 + if (core_if->core_params->dev_out_nak) {
49429 + /**
49430 + * Initialize xfer timeout timer. Implemented for
49431 + * 2.93a feature "Device DDMA OUT NAK Enhancement"
49432 + */
49433 + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
49434 + pcd->core_if->ep_xfer_timer[i] =
49435 + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
49436 + &pcd->core_if->ep_xfer_info[i]);
49437 + }
49438 + }
49439 +
49440 + return pcd;
49441 +#ifdef DWC_UTE_CFI
49442 +fail:
49443 +#endif
49444 + if (pcd->setup_pkt)
49445 + DWC_FREE(pcd->setup_pkt);
49446 + if (pcd->status_buf)
49447 + DWC_FREE(pcd->status_buf);
49448 +#ifdef DWC_UTE_CFI
49449 + if (pcd->cfi)
49450 + DWC_FREE(pcd->cfi);
49451 +#endif
49452 + if (pcd)
49453 + DWC_FREE(pcd);
49454 + return NULL;
49455 +
49456 +}
49457 +
49458 +/**
49459 + * Remove PCD specific data
49460 + */
49461 +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
49462 +{
49463 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
49464 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49465 + int i;
49466 +
49467 + if (pcd->core_if->core_params->dev_out_nak) {
49468 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49469 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
49470 + pcd->core_if->ep_xfer_info[i].state = 0;
49471 + }
49472 + }
49473 +
49474 + if (GET_CORE_IF(pcd)->dma_enable) {
49475 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
49476 + pcd->setup_pkt_dma_handle);
49477 + DWC_DMA_FREE(dev, sizeof(uint16_t), pcd->status_buf,
49478 + pcd->status_buf_dma_handle);
49479 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49480 + dwc_otg_ep_free_desc_chain(dev,
49481 + dev_if->setup_desc_addr[0],
49482 + dev_if->dma_setup_desc_addr
49483 + [0], 1);
49484 + dwc_otg_ep_free_desc_chain(dev,
49485 + dev_if->setup_desc_addr[1],
49486 + dev_if->dma_setup_desc_addr
49487 + [1], 1);
49488 + dwc_otg_ep_free_desc_chain(dev,
49489 + dev_if->in_desc_addr,
49490 + dev_if->dma_in_desc_addr, 1);
49491 + dwc_otg_ep_free_desc_chain(dev,
49492 + dev_if->out_desc_addr,
49493 + dev_if->dma_out_desc_addr,
49494 + 1);
49495 + }
49496 + } else {
49497 + DWC_FREE(pcd->setup_pkt);
49498 + DWC_FREE(pcd->status_buf);
49499 + }
49500 + DWC_SPINLOCK_FREE(pcd->lock);
49501 + /* Set core_if's lock pointer to NULL */
49502 + pcd->core_if->lock = NULL;
49503 +
49504 + DWC_TASK_FREE(pcd->start_xfer_tasklet);
49505 + DWC_TASK_FREE(pcd->test_mode_tasklet);
49506 + if (pcd->core_if->core_params->dev_out_nak) {
49507 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49508 + if (pcd->core_if->ep_xfer_timer[i]) {
49509 + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
49510 + }
49511 + }
49512 + }
49513 +
49514 +/* Release the CFI object's dynamic memory */
49515 +#ifdef DWC_UTE_CFI
49516 + if (pcd->cfi->ops.release) {
49517 + pcd->cfi->ops.release(pcd->cfi);
49518 + }
49519 +#endif
49520 +
49521 + DWC_FREE(pcd);
49522 +}
49523 +
49524 +/**
49525 + * Returns whether registered pcd is dual speed or not
49526 + */
49527 +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
49528 +{
49529 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49530 +
49531 + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
49532 + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
49533 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
49534 + (core_if->core_params->ulpi_fs_ls))) {
49535 + return 0;
49536 + }
49537 +
49538 + return 1;
49539 +}
49540 +
49541 +/**
49542 + * Returns whether registered pcd is OTG capable or not
49543 + */
49544 +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
49545 +{
49546 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49547 + gusbcfg_data_t usbcfg = {.d32 = 0 };
49548 +
49549 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
49550 + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
49551 + return 0;
49552 + }
49553 +
49554 + return 1;
49555 +}
49556 +
49557 +/**
49558 + * This function assigns periodic Tx FIFO to an periodic EP
49559 + * in shared Tx FIFO mode
49560 + */
49561 +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
49562 +{
49563 + uint32_t TxMsk = 1;
49564 + int i;
49565 +
49566 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
49567 + if ((TxMsk & core_if->tx_msk) == 0) {
49568 + core_if->tx_msk |= TxMsk;
49569 + return i + 1;
49570 + }
49571 + TxMsk <<= 1;
49572 + }
49573 + return 0;
49574 +}
49575 +
49576 +/**
49577 + * This function assigns periodic Tx FIFO to an periodic EP
49578 + * in shared Tx FIFO mode
49579 + */
49580 +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
49581 +{
49582 + uint32_t PerTxMsk = 1;
49583 + int i;
49584 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
49585 + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
49586 + core_if->p_tx_msk |= PerTxMsk;
49587 + return i + 1;
49588 + }
49589 + PerTxMsk <<= 1;
49590 + }
49591 + return 0;
49592 +}
49593 +
49594 +/**
49595 + * This function releases periodic Tx FIFO
49596 + * in shared Tx FIFO mode
49597 + */
49598 +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
49599 + uint32_t fifo_num)
49600 +{
49601 + core_if->p_tx_msk =
49602 + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
49603 +}
49604 +
49605 +/**
49606 + * This function releases periodic Tx FIFO
49607 + * in shared Tx FIFO mode
49608 + */
49609 +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
49610 +{
49611 + core_if->tx_msk =
49612 + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
49613 +}
49614 +
49615 +/**
49616 + * This function is being called from gadget
49617 + * to enable PCD endpoint.
49618 + */
49619 +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
49620 + const uint8_t * ep_desc, void *usb_ep)
49621 +{
49622 + int num, dir;
49623 + dwc_otg_pcd_ep_t *ep = NULL;
49624 + const usb_endpoint_descriptor_t *desc;
49625 + dwc_irqflags_t flags;
49626 + fifosize_data_t dptxfsiz = {.d32 = 0 };
49627 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
49628 + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
49629 + int retval = 0;
49630 + int i, epcount;
49631 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49632 +
49633 + desc = (const usb_endpoint_descriptor_t *)ep_desc;
49634 +
49635 + if (!desc) {
49636 + pcd->ep0.priv = usb_ep;
49637 + ep = &pcd->ep0;
49638 + retval = -DWC_E_INVALID;
49639 + goto out;
49640 + }
49641 +
49642 + num = UE_GET_ADDR(desc->bEndpointAddress);
49643 + dir = UE_GET_DIR(desc->bEndpointAddress);
49644 +
49645 + if (!desc->wMaxPacketSize) {
49646 + DWC_WARN("bad maxpacketsize\n");
49647 + retval = -DWC_E_INVALID;
49648 + goto out;
49649 + }
49650 +
49651 + if (dir == UE_DIR_IN) {
49652 + epcount = pcd->core_if->dev_if->num_in_eps;
49653 + for (i = 0; i < epcount; i++) {
49654 + if (num == pcd->in_ep[i].dwc_ep.num) {
49655 + ep = &pcd->in_ep[i];
49656 + break;
49657 + }
49658 + }
49659 + } else {
49660 + epcount = pcd->core_if->dev_if->num_out_eps;
49661 + for (i = 0; i < epcount; i++) {
49662 + if (num == pcd->out_ep[i].dwc_ep.num) {
49663 + ep = &pcd->out_ep[i];
49664 + break;
49665 + }
49666 + }
49667 + }
49668 +
49669 + if (!ep) {
49670 + DWC_WARN("bad address\n");
49671 + retval = -DWC_E_INVALID;
49672 + goto out;
49673 + }
49674 +
49675 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49676 +
49677 + ep->desc = desc;
49678 + ep->priv = usb_ep;
49679 +
49680 + /*
49681 + * Activate the EP
49682 + */
49683 + ep->stopped = 0;
49684 +
49685 + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
49686 + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
49687 +
49688 + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
49689 +
49690 + if (ep->dwc_ep.is_in) {
49691 + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49692 + ep->dwc_ep.tx_fifo_num = 0;
49693 +
49694 + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
49695 + /*
49696 + * if ISOC EP then assign a Periodic Tx FIFO.
49697 + */
49698 + ep->dwc_ep.tx_fifo_num =
49699 + assign_perio_tx_fifo(GET_CORE_IF(pcd));
49700 + }
49701 + } else {
49702 + /*
49703 + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
49704 + */
49705 + ep->dwc_ep.tx_fifo_num =
49706 + assign_tx_fifo(GET_CORE_IF(pcd));
49707 + }
49708 +
49709 + /* Calculating EP info controller base address */
49710 + if (ep->dwc_ep.tx_fifo_num
49711 + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49712 + gdfifocfg.d32 =
49713 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
49714 + core_global_regs->gdfifocfg);
49715 + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
49716 + dptxfsiz.d32 =
49717 + (DWC_READ_REG32
49718 + (&GET_CORE_IF(pcd)->core_global_regs->
49719 + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
49720 + gdfifocfg.b.epinfobase =
49721 + gdfifocfgbase.d32 + dptxfsiz.d32;
49722 + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
49723 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
49724 + core_global_regs->gdfifocfg,
49725 + gdfifocfg.d32);
49726 + }
49727 + }
49728 + }
49729 + /* Set initial data PID. */
49730 + if (ep->dwc_ep.type == UE_BULK) {
49731 + ep->dwc_ep.data_pid_start = 0;
49732 + }
49733 +
49734 + /* Alloc DMA Descriptors */
49735 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49736 +#ifndef DWC_UTE_PER_IO
49737 + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
49738 +#endif
49739 + ep->dwc_ep.desc_addr =
49740 + dwc_otg_ep_alloc_desc_chain(dev,
49741 + &ep->dwc_ep.dma_desc_addr,
49742 + MAX_DMA_DESC_CNT);
49743 + if (!ep->dwc_ep.desc_addr) {
49744 + DWC_WARN("%s, can't allocate DMA descriptor\n",
49745 + __func__);
49746 + retval = -DWC_E_SHUTDOWN;
49747 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49748 + goto out;
49749 + }
49750 +#ifndef DWC_UTE_PER_IO
49751 + }
49752 +#endif
49753 + }
49754 +
49755 + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
49756 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
49757 + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
49758 +#ifdef DWC_UTE_PER_IO
49759 + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
49760 +#endif
49761 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
49762 + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
49763 + ep->dwc_ep.frame_num = 0xFFFFFFFF;
49764 + }
49765 +
49766 + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
49767 +
49768 +#ifdef DWC_UTE_CFI
49769 + if (pcd->cfi->ops.ep_enable) {
49770 + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
49771 + }
49772 +#endif
49773 +
49774 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49775 +
49776 +out:
49777 + return retval;
49778 +}
49779 +
49780 +/**
49781 + * This function is being called from gadget
49782 + * to disable PCD endpoint.
49783 + */
49784 +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
49785 +{
49786 + dwc_otg_pcd_ep_t *ep;
49787 + dwc_irqflags_t flags;
49788 + dwc_otg_dev_dma_desc_t *desc_addr;
49789 + dwc_dma_t dma_desc_addr;
49790 + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
49791 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
49792 + fifosize_data_t dptxfsiz = {.d32 = 0 };
49793 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49794 +
49795 + ep = get_ep_from_handle(pcd, ep_handle);
49796 +
49797 + if (!ep || !ep->desc) {
49798 + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
49799 + return -DWC_E_INVALID;
49800 + }
49801 +
49802 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49803 +
49804 + dwc_otg_request_nuke(ep);
49805 +
49806 + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
49807 + if (pcd->core_if->core_params->dev_out_nak) {
49808 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
49809 + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
49810 + }
49811 + ep->desc = NULL;
49812 + ep->stopped = 1;
49813 +
49814 + gdfifocfg.d32 =
49815 + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
49816 + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
49817 +
49818 + if (ep->dwc_ep.is_in) {
49819 + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49820 + /* Flush the Tx FIFO */
49821 + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
49822 + ep->dwc_ep.tx_fifo_num);
49823 + }
49824 + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
49825 + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
49826 + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49827 + /* Decreasing EPinfo Base Addr */
49828 + dptxfsiz.d32 =
49829 + (DWC_READ_REG32
49830 + (&GET_CORE_IF(pcd)->
49831 + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
49832 + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
49833 + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
49834 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
49835 + gdfifocfg.d32);
49836 + }
49837 + }
49838 + }
49839 +
49840 + /* Free DMA Descriptors */
49841 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49842 + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
49843 + desc_addr = ep->dwc_ep.desc_addr;
49844 + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
49845 +
49846 + /* Cannot call dma_free_coherent() with IRQs disabled */
49847 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49848 + dwc_otg_ep_free_desc_chain(dev, desc_addr, dma_desc_addr,
49849 + MAX_DMA_DESC_CNT);
49850 +
49851 + goto out_unlocked;
49852 + }
49853 + }
49854 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49855 +
49856 +out_unlocked:
49857 + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
49858 + ep->dwc_ep.is_in ? "IN" : "OUT");
49859 + return 0;
49860 +
49861 +}
49862 +
49863 +/******************************************************************************/
49864 +#ifdef DWC_UTE_PER_IO
49865 +
49866 +/**
49867 + * Free the request and its extended parts
49868 + *
49869 + */
49870 +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
49871 +{
49872 + DWC_FREE(req->ext_req.per_io_frame_descs);
49873 + DWC_FREE(req);
49874 +}
49875 +
49876 +/**
49877 + * Start the next request in the endpoint's queue.
49878 + *
49879 + */
49880 +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
49881 + dwc_otg_pcd_ep_t * ep)
49882 +{
49883 + int i;
49884 + dwc_otg_pcd_request_t *req = NULL;
49885 + dwc_ep_t *dwcep = NULL;
49886 + struct dwc_iso_xreq_port *ereq = NULL;
49887 + struct dwc_iso_pkt_desc_port *ddesc_iso;
49888 + uint16_t nat;
49889 + depctl_data_t diepctl;
49890 +
49891 + dwcep = &ep->dwc_ep;
49892 +
49893 + if (dwcep->xiso_active_xfers > 0) {
49894 +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
49895 + DWC_WARN("There are currently active transfers for EP%d \
49896 + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
49897 + dwcep->xiso_queued_xfers);
49898 +#endif
49899 + return 0;
49900 + }
49901 +
49902 + nat = UGETW(ep->desc->wMaxPacketSize);
49903 + nat = (nat >> 11) & 0x03;
49904 +
49905 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
49906 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
49907 + ereq = &req->ext_req;
49908 + ep->stopped = 0;
49909 +
49910 + /* Get the frame number */
49911 + dwcep->xiso_frame_num =
49912 + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
49913 + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
49914 +
49915 + ddesc_iso = ereq->per_io_frame_descs;
49916 +
49917 + if (dwcep->is_in) {
49918 + /* Setup DMA Descriptor chain for IN Isoc request */
49919 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49920 + //if ((i % (nat + 1)) == 0)
49921 + if ( i > 0 )
49922 + dwcep->xiso_frame_num =
49923 + (dwcep->xiso_bInterval +
49924 + dwcep->xiso_frame_num) & 0x3FFF;
49925 + dwcep->desc_addr[i].buf =
49926 + req->dma + ddesc_iso[i].offset;
49927 + dwcep->desc_addr[i].status.b_iso_in.txbytes =
49928 + ddesc_iso[i].length;
49929 + dwcep->desc_addr[i].status.b_iso_in.framenum =
49930 + dwcep->xiso_frame_num;
49931 + dwcep->desc_addr[i].status.b_iso_in.bs =
49932 + BS_HOST_READY;
49933 + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
49934 + dwcep->desc_addr[i].status.b_iso_in.sp =
49935 + (ddesc_iso[i].length %
49936 + dwcep->maxpacket) ? 1 : 0;
49937 + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
49938 + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
49939 + dwcep->desc_addr[i].status.b_iso_in.l = 0;
49940 +
49941 + /* Process the last descriptor */
49942 + if (i == ereq->pio_pkt_count - 1) {
49943 + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
49944 + dwcep->desc_addr[i].status.b_iso_in.l = 1;
49945 + }
49946 + }
49947 +
49948 + /* Setup and start the transfer for this endpoint */
49949 + dwcep->xiso_active_xfers++;
49950 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
49951 + in_ep_regs[dwcep->num]->diepdma,
49952 + dwcep->dma_desc_addr);
49953 + diepctl.d32 = 0;
49954 + diepctl.b.epena = 1;
49955 + diepctl.b.cnak = 1;
49956 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
49957 + in_ep_regs[dwcep->num]->diepctl, 0,
49958 + diepctl.d32);
49959 + } else {
49960 + /* Setup DMA Descriptor chain for OUT Isoc request */
49961 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49962 + //if ((i % (nat + 1)) == 0)
49963 + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
49964 + dwcep->xiso_frame_num) & 0x3FFF;
49965 + dwcep->desc_addr[i].buf =
49966 + req->dma + ddesc_iso[i].offset;
49967 + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
49968 + ddesc_iso[i].length;
49969 + dwcep->desc_addr[i].status.b_iso_out.framenum =
49970 + dwcep->xiso_frame_num;
49971 + dwcep->desc_addr[i].status.b_iso_out.bs =
49972 + BS_HOST_READY;
49973 + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
49974 + dwcep->desc_addr[i].status.b_iso_out.sp =
49975 + (ddesc_iso[i].length %
49976 + dwcep->maxpacket) ? 1 : 0;
49977 + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
49978 + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
49979 + dwcep->desc_addr[i].status.b_iso_out.l = 0;
49980 +
49981 + /* Process the last descriptor */
49982 + if (i == ereq->pio_pkt_count - 1) {
49983 + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
49984 + dwcep->desc_addr[i].status.b_iso_out.l = 1;
49985 + }
49986 + }
49987 +
49988 + /* Setup and start the transfer for this endpoint */
49989 + dwcep->xiso_active_xfers++;
49990 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
49991 + dev_if->out_ep_regs[dwcep->num]->
49992 + doepdma, dwcep->dma_desc_addr);
49993 + diepctl.d32 = 0;
49994 + diepctl.b.epena = 1;
49995 + diepctl.b.cnak = 1;
49996 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
49997 + dev_if->out_ep_regs[dwcep->num]->
49998 + doepctl, 0, diepctl.d32);
49999 + }
50000 +
50001 + } else {
50002 + ep->stopped = 1;
50003 + }
50004 +
50005 + return 0;
50006 +}
50007 +
50008 +/**
50009 + * - Remove the request from the queue
50010 + */
50011 +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
50012 +{
50013 + dwc_otg_pcd_request_t *req = NULL;
50014 + struct dwc_iso_xreq_port *ereq = NULL;
50015 + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
50016 + dwc_ep_t *dwcep = NULL;
50017 + int i;
50018 +
50019 + //DWC_DEBUG();
50020 + dwcep = &ep->dwc_ep;
50021 +
50022 + /* Get the first pending request from the queue */
50023 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50024 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
50025 + if (!req) {
50026 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
50027 + return;
50028 + }
50029 + dwcep->xiso_active_xfers--;
50030 + dwcep->xiso_queued_xfers--;
50031 + /* Remove this request from the queue */
50032 + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
50033 + } else {
50034 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
50035 + return;
50036 + }
50037 +
50038 + ep->stopped = 1;
50039 + ereq = &req->ext_req;
50040 + ddesc_iso = ereq->per_io_frame_descs;
50041 +
50042 + if (dwcep->xiso_active_xfers < 0) {
50043 + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
50044 + dwcep->xiso_active_xfers);
50045 + }
50046 +
50047 + /* Fill the Isoc descs of portable extended req from dma descriptors */
50048 + for (i = 0; i < ereq->pio_pkt_count; i++) {
50049 + if (dwcep->is_in) { /* IN endpoints */
50050 + ddesc_iso[i].actual_length = ddesc_iso[i].length -
50051 + dwcep->desc_addr[i].status.b_iso_in.txbytes;
50052 + ddesc_iso[i].status =
50053 + dwcep->desc_addr[i].status.b_iso_in.txsts;
50054 + } else { /* OUT endpoints */
50055 + ddesc_iso[i].actual_length = ddesc_iso[i].length -
50056 + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
50057 + ddesc_iso[i].status =
50058 + dwcep->desc_addr[i].status.b_iso_out.rxsts;
50059 + }
50060 + }
50061 +
50062 + DWC_SPINUNLOCK(ep->pcd->lock);
50063 +
50064 + /* Call the completion function in the non-portable logic */
50065 + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
50066 + &req->ext_req);
50067 +
50068 + DWC_SPINLOCK(ep->pcd->lock);
50069 +
50070 + /* Free the request - specific freeing needed for extended request object */
50071 + dwc_pcd_xiso_ereq_free(ep, req);
50072 +
50073 + /* Start the next request */
50074 + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
50075 +
50076 + return;
50077 +}
50078 +
50079 +/**
50080 + * Create and initialize the Isoc pkt descriptors of the extended request.
50081 + *
50082 + */
50083 +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
50084 + void *ereq_nonport,
50085 + int atomic_alloc)
50086 +{
50087 + struct dwc_iso_xreq_port *ereq = NULL;
50088 + struct dwc_iso_xreq_port *req_mapped = NULL;
50089 + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
50090 + uint32_t pkt_count;
50091 + int i;
50092 +
50093 + ereq = &req->ext_req;
50094 + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
50095 + pkt_count = req_mapped->pio_pkt_count;
50096 +
50097 + /* Create the isoc descs */
50098 + if (atomic_alloc) {
50099 + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
50100 + } else {
50101 + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
50102 + }
50103 +
50104 + if (!ipds) {
50105 + DWC_ERROR("Failed to allocate isoc descriptors");
50106 + return -DWC_E_NO_MEMORY;
50107 + }
50108 +
50109 + /* Initialize the extended request fields */
50110 + ereq->per_io_frame_descs = ipds;
50111 + ereq->error_count = 0;
50112 + ereq->pio_alloc_pkt_count = pkt_count;
50113 + ereq->pio_pkt_count = pkt_count;
50114 + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
50115 +
50116 + /* Init the Isoc descriptors */
50117 + for (i = 0; i < pkt_count; i++) {
50118 + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
50119 + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
50120 + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
50121 + ipds[i].actual_length =
50122 + req_mapped->per_io_frame_descs[i].actual_length;
50123 + }
50124 +
50125 + return 0;
50126 +}
50127 +
50128 +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
50129 +{
50130 + struct dwc_iso_pkt_desc_port *xfd = NULL;
50131 + int i;
50132 +
50133 + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
50134 + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
50135 + DWC_DEBUG("error_count=%d", ereq->error_count);
50136 + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
50137 + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
50138 + DWC_DEBUG("res=%d", ereq->res);
50139 +
50140 + for (i = 0; i < ereq->pio_pkt_count; i++) {
50141 + xfd = &ereq->per_io_frame_descs[0];
50142 + DWC_DEBUG("FD #%d", i);
50143 +
50144 + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
50145 + DWC_DEBUG("xfd->length=%d", xfd->length);
50146 + DWC_DEBUG("xfd->offset=%d", xfd->offset);
50147 + DWC_DEBUG("xfd->status=%d", xfd->status);
50148 + }
50149 +}
50150 +
50151 +/**
50152 + *
50153 + */
50154 +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
50155 + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
50156 + int zero, void *req_handle, int atomic_alloc,
50157 + void *ereq_nonport)
50158 +{
50159 + dwc_otg_pcd_request_t *req = NULL;
50160 + dwc_otg_pcd_ep_t *ep;
50161 + dwc_irqflags_t flags;
50162 + int res;
50163 +
50164 + ep = get_ep_from_handle(pcd, ep_handle);
50165 + if (!ep) {
50166 + DWC_WARN("bad ep\n");
50167 + return -DWC_E_INVALID;
50168 + }
50169 +
50170 + /* We support this extension only for DDMA mode */
50171 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
50172 + if (!GET_CORE_IF(pcd)->dma_desc_enable)
50173 + return -DWC_E_INVALID;
50174 +
50175 + /* Create a dwc_otg_pcd_request_t object */
50176 + if (atomic_alloc) {
50177 + req = DWC_ALLOC_ATOMIC(sizeof(*req));
50178 + } else {
50179 + req = DWC_ALLOC(sizeof(*req));
50180 + }
50181 +
50182 + if (!req) {
50183 + return -DWC_E_NO_MEMORY;
50184 + }
50185 +
50186 + /* Create the Isoc descs for this request which shall be the exact match
50187 + * of the structure sent to us from the non-portable logic */
50188 + res =
50189 + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
50190 + if (res) {
50191 + DWC_WARN("Failed to init the Isoc descriptors");
50192 + DWC_FREE(req);
50193 + return res;
50194 + }
50195 +
50196 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50197 +
50198 + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
50199 + req->buf = buf;
50200 + req->dma = dma_buf;
50201 + req->length = buflen;
50202 + req->sent_zlp = zero;
50203 + req->priv = req_handle;
50204 +
50205 + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50206 + ep->dwc_ep.dma_addr = dma_buf;
50207 + ep->dwc_ep.start_xfer_buff = buf;
50208 + ep->dwc_ep.xfer_buff = buf;
50209 + ep->dwc_ep.xfer_len = 0;
50210 + ep->dwc_ep.xfer_count = 0;
50211 + ep->dwc_ep.sent_zlp = 0;
50212 + ep->dwc_ep.total_len = buflen;
50213 +
50214 + /* Add this request to the tail */
50215 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50216 + ep->dwc_ep.xiso_queued_xfers++;
50217 +
50218 +//DWC_DEBUG("CP_0");
50219 +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
50220 +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
50221 +//prn_ext_request(&req->ext_req);
50222 +
50223 + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50224 +
50225 + /* If the req->status == ASAP then check if there is any active transfer
50226 + * for this endpoint. If no active transfers, then get the first entry
50227 + * from the queue and start that transfer
50228 + */
50229 + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
50230 + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
50231 + if (res) {
50232 + DWC_WARN("Failed to start the next Isoc transfer");
50233 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50234 + DWC_FREE(req);
50235 + return res;
50236 + }
50237 + }
50238 +
50239 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50240 + return 0;
50241 +}
50242 +
50243 +#endif
50244 +/* END ifdef DWC_UTE_PER_IO ***************************************************/
50245 +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
50246 + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
50247 + int zero, void *req_handle, int atomic_alloc)
50248 +{
50249 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
50250 + dwc_irqflags_t flags;
50251 + dwc_otg_pcd_request_t *req;
50252 + dwc_otg_pcd_ep_t *ep;
50253 + uint32_t max_transfer;
50254 +
50255 + ep = get_ep_from_handle(pcd, ep_handle);
50256 + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
50257 + DWC_WARN("bad ep\n");
50258 + return -DWC_E_INVALID;
50259 + }
50260 +
50261 + if (atomic_alloc) {
50262 + req = DWC_ALLOC_ATOMIC(sizeof(*req));
50263 + } else {
50264 + req = DWC_ALLOC(sizeof(*req));
50265 + }
50266 +
50267 + if (!req) {
50268 + return -DWC_E_NO_MEMORY;
50269 + }
50270 + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
50271 + if (!GET_CORE_IF(pcd)->core_params->opt) {
50272 + if (ep->dwc_ep.num != 0) {
50273 + DWC_ERROR("queue req %p, len %d buf %p\n",
50274 + req_handle, buflen, buf);
50275 + }
50276 + }
50277 +
50278 + req->buf = buf;
50279 + req->dma = dma_buf;
50280 + req->length = buflen;
50281 + req->sent_zlp = zero;
50282 + req->priv = req_handle;
50283 + req->dw_align_buf = NULL;
50284 + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
50285 + && !GET_CORE_IF(pcd)->dma_desc_enable)
50286 + req->dw_align_buf = DWC_DMA_ALLOC(dev, buflen,
50287 + &req->dw_align_buf_dma);
50288 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50289 +
50290 + /*
50291 + * After adding request to the queue for IN ISOC wait for In Token Received
50292 + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
50293 + * Received when EP is disabled interrupt to obtain starting microframe
50294 + * (odd/even) start transfer
50295 + */
50296 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
50297 + if (req != 0) {
50298 + depctl_data_t depctl = {.d32 =
50299 + DWC_READ_REG32(&pcd->core_if->dev_if->
50300 + in_ep_regs[ep->dwc_ep.num]->
50301 + diepctl) };
50302 + ++pcd->request_pending;
50303 +
50304 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50305 + if (ep->dwc_ep.is_in) {
50306 + depctl.b.cnak = 1;
50307 + DWC_WRITE_REG32(&pcd->core_if->dev_if->
50308 + in_ep_regs[ep->dwc_ep.num]->
50309 + diepctl, depctl.d32);
50310 + }
50311 +
50312 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50313 + }
50314 + return 0;
50315 + }
50316 +
50317 + /*
50318 + * For EP0 IN without premature status, zlp is required?
50319 + */
50320 + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
50321 + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
50322 + //_req->zero = 1;
50323 + }
50324 +
50325 + /* Start the transfer */
50326 + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
50327 + /* EP0 Transfer? */
50328 + if (ep->dwc_ep.num == 0) {
50329 + switch (pcd->ep0state) {
50330 + case EP0_IN_DATA_PHASE:
50331 + DWC_DEBUGPL(DBG_PCD,
50332 + "%s ep0: EP0_IN_DATA_PHASE\n",
50333 + __func__);
50334 + break;
50335 +
50336 + case EP0_OUT_DATA_PHASE:
50337 + DWC_DEBUGPL(DBG_PCD,
50338 + "%s ep0: EP0_OUT_DATA_PHASE\n",
50339 + __func__);
50340 + if (pcd->request_config) {
50341 + /* Complete STATUS PHASE */
50342 + ep->dwc_ep.is_in = 1;
50343 + pcd->ep0state = EP0_IN_STATUS_PHASE;
50344 + }
50345 + break;
50346 +
50347 + case EP0_IN_STATUS_PHASE:
50348 + DWC_DEBUGPL(DBG_PCD,
50349 + "%s ep0: EP0_IN_STATUS_PHASE\n",
50350 + __func__);
50351 + break;
50352 +
50353 + default:
50354 + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
50355 + pcd->ep0state);
50356 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50357 + return -DWC_E_SHUTDOWN;
50358 + }
50359 +
50360 + ep->dwc_ep.dma_addr = dma_buf;
50361 + ep->dwc_ep.start_xfer_buff = buf;
50362 + ep->dwc_ep.xfer_buff = buf;
50363 + ep->dwc_ep.xfer_len = buflen;
50364 + ep->dwc_ep.xfer_count = 0;
50365 + ep->dwc_ep.sent_zlp = 0;
50366 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
50367 +
50368 + if (zero) {
50369 + if ((ep->dwc_ep.xfer_len %
50370 + ep->dwc_ep.maxpacket == 0)
50371 + && (ep->dwc_ep.xfer_len != 0)) {
50372 + ep->dwc_ep.sent_zlp = 1;
50373 + }
50374 +
50375 + }
50376 +
50377 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
50378 + &ep->dwc_ep);
50379 + } // non-ep0 endpoints
50380 + else {
50381 +#ifdef DWC_UTE_CFI
50382 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
50383 + /* store the request length */
50384 + ep->dwc_ep.cfi_req_len = buflen;
50385 + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
50386 + ep, req);
50387 + } else {
50388 +#endif
50389 + max_transfer =
50390 + GET_CORE_IF(ep->pcd)->core_params->
50391 + max_transfer_size;
50392 +
50393 + /* Setup and start the Transfer */
50394 + if (req->dw_align_buf){
50395 + if (ep->dwc_ep.is_in)
50396 + dwc_memcpy(req->dw_align_buf,
50397 + buf, buflen);
50398 + ep->dwc_ep.dma_addr =
50399 + req->dw_align_buf_dma;
50400 + ep->dwc_ep.start_xfer_buff =
50401 + req->dw_align_buf;
50402 + ep->dwc_ep.xfer_buff =
50403 + req->dw_align_buf;
50404 + } else {
50405 + ep->dwc_ep.dma_addr = dma_buf;
50406 + ep->dwc_ep.start_xfer_buff = buf;
50407 + ep->dwc_ep.xfer_buff = buf;
50408 + }
50409 + ep->dwc_ep.xfer_len = 0;
50410 + ep->dwc_ep.xfer_count = 0;
50411 + ep->dwc_ep.sent_zlp = 0;
50412 + ep->dwc_ep.total_len = buflen;
50413 +
50414 + ep->dwc_ep.maxxfer = max_transfer;
50415 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
50416 + uint32_t out_max_xfer =
50417 + DDMA_MAX_TRANSFER_SIZE -
50418 + (DDMA_MAX_TRANSFER_SIZE % 4);
50419 + if (ep->dwc_ep.is_in) {
50420 + if (ep->dwc_ep.maxxfer >
50421 + DDMA_MAX_TRANSFER_SIZE) {
50422 + ep->dwc_ep.maxxfer =
50423 + DDMA_MAX_TRANSFER_SIZE;
50424 + }
50425 + } else {
50426 + if (ep->dwc_ep.maxxfer >
50427 + out_max_xfer) {
50428 + ep->dwc_ep.maxxfer =
50429 + out_max_xfer;
50430 + }
50431 + }
50432 + }
50433 + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
50434 + ep->dwc_ep.maxxfer -=
50435 + (ep->dwc_ep.maxxfer %
50436 + ep->dwc_ep.maxpacket);
50437 + }
50438 +
50439 + if (zero) {
50440 + if ((ep->dwc_ep.total_len %
50441 + ep->dwc_ep.maxpacket == 0)
50442 + && (ep->dwc_ep.total_len != 0)) {
50443 + ep->dwc_ep.sent_zlp = 1;
50444 + }
50445 + }
50446 +#ifdef DWC_UTE_CFI
50447 + }
50448 +#endif
50449 + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
50450 + &ep->dwc_ep);
50451 + }
50452 + }
50453 +
50454 + if (req != 0) {
50455 + ++pcd->request_pending;
50456 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50457 + if (ep->dwc_ep.is_in && ep->stopped
50458 + && !(GET_CORE_IF(pcd)->dma_enable)) {
50459 + /** @todo NGS Create a function for this. */
50460 + diepmsk_data_t diepmsk = {.d32 = 0 };
50461 + diepmsk.b.intktxfemp = 1;
50462 + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
50463 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
50464 + dev_if->dev_global_regs->diepeachintmsk
50465 + [ep->dwc_ep.num], 0,
50466 + diepmsk.d32);
50467 + } else {
50468 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
50469 + dev_if->dev_global_regs->
50470 + diepmsk, 0, diepmsk.d32);
50471 + }
50472 +
50473 + }
50474 + }
50475 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50476 +
50477 + return 0;
50478 +}
50479 +
50480 +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
50481 + void *req_handle)
50482 +{
50483 + dwc_irqflags_t flags;
50484 + dwc_otg_pcd_request_t *req;
50485 + dwc_otg_pcd_ep_t *ep;
50486 +
50487 + ep = get_ep_from_handle(pcd, ep_handle);
50488 + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
50489 + DWC_WARN("bad argument\n");
50490 + return -DWC_E_INVALID;
50491 + }
50492 +
50493 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50494 +
50495 + /* make sure it's actually queued on this endpoint */
50496 + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
50497 + if (req->priv == (void *)req_handle) {
50498 + break;
50499 + }
50500 + }
50501 +
50502 + if (req->priv != (void *)req_handle) {
50503 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50504 + return -DWC_E_INVALID;
50505 + }
50506 +
50507 + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
50508 + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
50509 + } else {
50510 + req = NULL;
50511 + }
50512 +
50513 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50514 +
50515 + return req ? 0 : -DWC_E_SHUTDOWN;
50516 +
50517 +}
50518 +
50519 +/**
50520 + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
50521 + *
50522 + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
50523 + * requests. If the gadget driver clears the halt status, it will
50524 + * automatically unwedge the endpoint.
50525 + *
50526 + * Returns zero on success, else negative DWC error code.
50527 + */
50528 +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
50529 +{
50530 + dwc_otg_pcd_ep_t *ep;
50531 + dwc_irqflags_t flags;
50532 + int retval = 0;
50533 +
50534 + ep = get_ep_from_handle(pcd, ep_handle);
50535 +
50536 + if ((!ep->desc && ep != &pcd->ep0) ||
50537 + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
50538 + DWC_WARN("%s, bad ep\n", __func__);
50539 + return -DWC_E_INVALID;
50540 + }
50541 +
50542 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50543 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50544 + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
50545 + ep->dwc_ep.is_in ? "IN" : "OUT");
50546 + retval = -DWC_E_AGAIN;
50547 + } else {
50548 + /* This code needs to be reviewed */
50549 + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
50550 + dtxfsts_data_t txstatus;
50551 + fifosize_data_t txfifosize;
50552 +
50553 + txfifosize.d32 =
50554 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
50555 + core_global_regs->dtxfsiz[ep->dwc_ep.
50556 + tx_fifo_num]);
50557 + txstatus.d32 =
50558 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
50559 + dev_if->in_ep_regs[ep->dwc_ep.num]->
50560 + dtxfsts);
50561 +
50562 + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
50563 + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
50564 + retval = -DWC_E_AGAIN;
50565 + } else {
50566 + if (ep->dwc_ep.num == 0) {
50567 + pcd->ep0state = EP0_STALL;
50568 + }
50569 +
50570 + ep->stopped = 1;
50571 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
50572 + &ep->dwc_ep);
50573 + }
50574 + } else {
50575 + if (ep->dwc_ep.num == 0) {
50576 + pcd->ep0state = EP0_STALL;
50577 + }
50578 +
50579 + ep->stopped = 1;
50580 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50581 + }
50582 + }
50583 +
50584 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50585 +
50586 + return retval;
50587 +}
50588 +
50589 +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
50590 +{
50591 + dwc_otg_pcd_ep_t *ep;
50592 + dwc_irqflags_t flags;
50593 + int retval = 0;
50594 +
50595 + ep = get_ep_from_handle(pcd, ep_handle);
50596 +
50597 + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
50598 + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
50599 + DWC_WARN("%s, bad ep\n", __func__);
50600 + return -DWC_E_INVALID;
50601 + }
50602 +
50603 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50604 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50605 + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
50606 + ep->dwc_ep.is_in ? "IN" : "OUT");
50607 + retval = -DWC_E_AGAIN;
50608 + } else if (value == 0) {
50609 + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50610 + } else if (value == 1) {
50611 + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
50612 + dtxfsts_data_t txstatus;
50613 + fifosize_data_t txfifosize;
50614 +
50615 + txfifosize.d32 =
50616 + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
50617 + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
50618 + txstatus.d32 =
50619 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
50620 + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
50621 +
50622 + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
50623 + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
50624 + retval = -DWC_E_AGAIN;
50625 + } else {
50626 + if (ep->dwc_ep.num == 0) {
50627 + pcd->ep0state = EP0_STALL;
50628 + }
50629 +
50630 + ep->stopped = 1;
50631 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
50632 + &ep->dwc_ep);
50633 + }
50634 + } else {
50635 + if (ep->dwc_ep.num == 0) {
50636 + pcd->ep0state = EP0_STALL;
50637 + }
50638 +
50639 + ep->stopped = 1;
50640 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50641 + }
50642 + } else if (value == 2) {
50643 + ep->dwc_ep.stall_clear_flag = 0;
50644 + } else if (value == 3) {
50645 + ep->dwc_ep.stall_clear_flag = 1;
50646 + }
50647 +
50648 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50649 +
50650 + return retval;
50651 +}
50652 +
50653 +/**
50654 + * This function initiates remote wakeup of the host from suspend state.
50655 + */
50656 +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
50657 +{
50658 + dctl_data_t dctl = { 0 };
50659 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50660 + dsts_data_t dsts;
50661 +
50662 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
50663 + if (!dsts.b.suspsts) {
50664 + DWC_WARN("Remote wakeup while is not in suspend state\n");
50665 + }
50666 + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
50667 + if (pcd->remote_wakeup_enable) {
50668 + if (set) {
50669 +
50670 + if (core_if->adp_enable) {
50671 + gpwrdn_data_t gpwrdn;
50672 +
50673 + dwc_otg_adp_probe_stop(core_if);
50674 +
50675 + /* Mask SRP detected interrupt from Power Down Logic */
50676 + gpwrdn.d32 = 0;
50677 + gpwrdn.b.srp_det_msk = 1;
50678 + DWC_MODIFY_REG32(&core_if->
50679 + core_global_regs->gpwrdn,
50680 + gpwrdn.d32, 0);
50681 +
50682 + /* Disable Power Down Logic */
50683 + gpwrdn.d32 = 0;
50684 + gpwrdn.b.pmuactv = 1;
50685 + DWC_MODIFY_REG32(&core_if->
50686 + core_global_regs->gpwrdn,
50687 + gpwrdn.d32, 0);
50688 +
50689 + /*
50690 + * Initialize the Core for Device mode.
50691 + */
50692 + core_if->op_state = B_PERIPHERAL;
50693 + dwc_otg_core_init(core_if);
50694 + dwc_otg_enable_global_interrupts(core_if);
50695 + cil_pcd_start(core_if);
50696 +
50697 + dwc_otg_initiate_srp(core_if);
50698 + }
50699 +
50700 + dctl.b.rmtwkupsig = 1;
50701 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
50702 + dctl, 0, dctl.d32);
50703 + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
50704 +
50705 + dwc_mdelay(2);
50706 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
50707 + dctl, dctl.d32, 0);
50708 + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
50709 + }
50710 + } else {
50711 + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
50712 + }
50713 +}
50714 +
50715 +#ifdef CONFIG_USB_DWC_OTG_LPM
50716 +/**
50717 + * This function initiates remote wakeup of the host from L1 sleep state.
50718 + */
50719 +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
50720 +{
50721 + glpmcfg_data_t lpmcfg;
50722 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50723 +
50724 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
50725 +
50726 + /* Check if we are in L1 state */
50727 + if (!lpmcfg.b.prt_sleep_sts) {
50728 + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
50729 + return;
50730 + }
50731 +
50732 + /* Check if host allows remote wakeup */
50733 + if (!lpmcfg.b.rem_wkup_en) {
50734 + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
50735 + return;
50736 + }
50737 +
50738 + /* Check if Resume OK */
50739 + if (!lpmcfg.b.sleep_state_resumeok) {
50740 + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
50741 + return;
50742 + }
50743 +
50744 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
50745 + lpmcfg.b.en_utmi_sleep = 0;
50746 + lpmcfg.b.hird_thres &= (~(1 << 4));
50747 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
50748 +
50749 + if (set) {
50750 + dctl_data_t dctl = {.d32 = 0 };
50751 + dctl.b.rmtwkupsig = 1;
50752 + /* Set RmtWkUpSig bit to start remote wakup signaling.
50753 + * Hardware will automatically clear this bit.
50754 + */
50755 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
50756 + 0, dctl.d32);
50757 + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
50758 + }
50759 +
50760 +}
50761 +#endif
50762 +
50763 +/**
50764 + * Performs remote wakeup.
50765 + */
50766 +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
50767 +{
50768 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50769 + dwc_irqflags_t flags;
50770 + if (dwc_otg_is_device_mode(core_if)) {
50771 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50772 +#ifdef CONFIG_USB_DWC_OTG_LPM
50773 + if (core_if->lx_state == DWC_OTG_L1) {
50774 + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
50775 + } else {
50776 +#endif
50777 + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
50778 +#ifdef CONFIG_USB_DWC_OTG_LPM
50779 + }
50780 +#endif
50781 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50782 + }
50783 + return;
50784 +}
50785 +
50786 +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
50787 +{
50788 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50789 + dctl_data_t dctl = { 0 };
50790 +
50791 + if (dwc_otg_is_device_mode(core_if)) {
50792 + dctl.b.sftdiscon = 1;
50793 + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
50794 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
50795 + dwc_udelay(no_of_usecs);
50796 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
50797 +
50798 + } else{
50799 + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
50800 + }
50801 + return;
50802 +
50803 +}
50804 +
50805 +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
50806 +{
50807 + dsts_data_t dsts;
50808 + gotgctl_data_t gotgctl;
50809 +
50810 + /*
50811 + * This function starts the Protocol if no session is in progress. If
50812 + * a session is already in progress, but the device is suspended,
50813 + * remote wakeup signaling is started.
50814 + */
50815 +
50816 + /* Check if valid session */
50817 + gotgctl.d32 =
50818 + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
50819 + if (gotgctl.b.bsesvld) {
50820 + /* Check if suspend state */
50821 + dsts.d32 =
50822 + DWC_READ_REG32(&
50823 + (GET_CORE_IF(pcd)->dev_if->
50824 + dev_global_regs->dsts));
50825 + if (dsts.b.suspsts) {
50826 + dwc_otg_pcd_remote_wakeup(pcd, 1);
50827 + }
50828 + } else {
50829 + dwc_otg_pcd_initiate_srp(pcd);
50830 + }
50831 +
50832 + return 0;
50833 +
50834 +}
50835 +
50836 +/**
50837 + * Start the SRP timer to detect when the SRP does not complete within
50838 + * 6 seconds.
50839 + *
50840 + * @param pcd the pcd structure.
50841 + */
50842 +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
50843 +{
50844 + dwc_irqflags_t flags;
50845 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50846 + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
50847 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50848 +}
50849 +
50850 +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
50851 +{
50852 + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
50853 +}
50854 +
50855 +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
50856 +{
50857 + return GET_CORE_IF(pcd)->core_params->lpm_enable;
50858 +}
50859 +
50860 +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
50861 +{
50862 + return pcd->b_hnp_enable;
50863 +}
50864 +
50865 +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
50866 +{
50867 + return pcd->a_hnp_support;
50868 +}
50869 +
50870 +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
50871 +{
50872 + return pcd->a_alt_hnp_support;
50873 +}
50874 +
50875 +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
50876 +{
50877 + return pcd->remote_wakeup_enable;
50878 +}
50879 +
50880 +#endif /* DWC_HOST_ONLY */
50881 --- /dev/null
50882 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
50883 @@ -0,0 +1,273 @@
50884 +/* ==========================================================================
50885 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
50886 + * $Revision: #48 $
50887 + * $Date: 2012/08/10 $
50888 + * $Change: 2047372 $
50889 + *
50890 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
50891 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
50892 + * otherwise expressly agreed to in writing between Synopsys and you.
50893 + *
50894 + * The Software IS NOT an item of Licensed Software or Licensed Product under
50895 + * any End User Software License Agreement or Agreement for Licensed Product
50896 + * with Synopsys or any supplement thereto. You are permitted to use and
50897 + * redistribute this Software in source and binary forms, with or without
50898 + * modification, provided that redistributions of source code must retain this
50899 + * notice. You may not view, use, disclose, copy or distribute this file or
50900 + * any information contained herein except pursuant to this license grant from
50901 + * Synopsys. If you do not agree with this notice, including the disclaimer
50902 + * below, then you are not authorized to use the Software.
50903 + *
50904 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
50905 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50906 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50907 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
50908 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50909 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50910 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50911 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50912 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50913 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
50914 + * DAMAGE.
50915 + * ========================================================================== */
50916 +#ifndef DWC_HOST_ONLY
50917 +#if !defined(__DWC_PCD_H__)
50918 +#define __DWC_PCD_H__
50919 +
50920 +#include "dwc_otg_os_dep.h"
50921 +#include "usb.h"
50922 +#include "dwc_otg_cil.h"
50923 +#include "dwc_otg_pcd_if.h"
50924 +#include "dwc_otg_driver.h"
50925 +
50926 +struct cfiobject;
50927 +
50928 +/**
50929 + * @file
50930 + *
50931 + * This file contains the structures, constants, and interfaces for
50932 + * the Perpherial Contoller Driver (PCD).
50933 + *
50934 + * The Peripheral Controller Driver (PCD) for Linux will implement the
50935 + * Gadget API, so that the existing Gadget drivers can be used. For
50936 + * the Mass Storage Function driver the File-backed USB Storage Gadget
50937 + * (FBS) driver will be used. The FBS driver supports the
50938 + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
50939 + * transports.
50940 + *
50941 + */
50942 +
50943 +/** Invalid DMA Address */
50944 +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
50945 +
50946 +/** Max Transfer size for any EP */
50947 +#define DDMA_MAX_TRANSFER_SIZE 65535
50948 +
50949 +/**
50950 + * Get the pointer to the core_if from the pcd pointer.
50951 + */
50952 +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
50953 +
50954 +/**
50955 + * States of EP0.
50956 + */
50957 +typedef enum ep0_state {
50958 + EP0_DISCONNECT, /* no host */
50959 + EP0_IDLE,
50960 + EP0_IN_DATA_PHASE,
50961 + EP0_OUT_DATA_PHASE,
50962 + EP0_IN_STATUS_PHASE,
50963 + EP0_OUT_STATUS_PHASE,
50964 + EP0_STALL,
50965 +} ep0state_e;
50966 +
50967 +/** Fordward declaration.*/
50968 +struct dwc_otg_pcd;
50969 +
50970 +/** DWC_otg iso request structure.
50971 + *
50972 + */
50973 +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
50974 +
50975 +#ifdef DWC_UTE_PER_IO
50976 +
50977 +/**
50978 + * This shall be the exact analogy of the same type structure defined in the
50979 + * usb_gadget.h. Each descriptor contains
50980 + */
50981 +struct dwc_iso_pkt_desc_port {
50982 + uint32_t offset;
50983 + uint32_t length; /* expected length */
50984 + uint32_t actual_length;
50985 + uint32_t status;
50986 +};
50987 +
50988 +struct dwc_iso_xreq_port {
50989 + /** transfer/submission flag */
50990 + uint32_t tr_sub_flags;
50991 + /** Start the request ASAP */
50992 +#define DWC_EREQ_TF_ASAP 0x00000002
50993 + /** Just enqueue the request w/o initiating a transfer */
50994 +#define DWC_EREQ_TF_ENQUEUE 0x00000004
50995 +
50996 + /**
50997 + * count of ISO packets attached to this request - shall
50998 + * not exceed the pio_alloc_pkt_count
50999 + */
51000 + uint32_t pio_pkt_count;
51001 + /** count of ISO packets allocated for this request */
51002 + uint32_t pio_alloc_pkt_count;
51003 + /** number of ISO packet errors */
51004 + uint32_t error_count;
51005 + /** reserved for future extension */
51006 + uint32_t res;
51007 + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
51008 + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
51009 +};
51010 +#endif
51011 +/** DWC_otg request structure.
51012 + * This structure is a list of requests.
51013 + */
51014 +typedef struct dwc_otg_pcd_request {
51015 + void *priv;
51016 + void *buf;
51017 + dwc_dma_t dma;
51018 + uint32_t length;
51019 + uint32_t actual;
51020 + unsigned sent_zlp:1;
51021 + /**
51022 + * Used instead of original buffer if
51023 + * it(physical address) is not dword-aligned.
51024 + **/
51025 + uint8_t *dw_align_buf;
51026 + dwc_dma_t dw_align_buf_dma;
51027 +
51028 + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
51029 +#ifdef DWC_UTE_PER_IO
51030 + struct dwc_iso_xreq_port ext_req;
51031 + //void *priv_ereq_nport; /* */
51032 +#endif
51033 +} dwc_otg_pcd_request_t;
51034 +
51035 +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
51036 +
51037 +/** PCD EP structure.
51038 + * This structure describes an EP, there is an array of EPs in the PCD
51039 + * structure.
51040 + */
51041 +typedef struct dwc_otg_pcd_ep {
51042 + /** USB EP Descriptor */
51043 + const usb_endpoint_descriptor_t *desc;
51044 +
51045 + /** queue of dwc_otg_pcd_requests. */
51046 + struct req_list queue;
51047 + unsigned stopped:1;
51048 + unsigned disabling:1;
51049 + unsigned dma:1;
51050 + unsigned queue_sof:1;
51051 +
51052 +#ifdef DWC_EN_ISOC
51053 + /** ISOC req handle passed */
51054 + void *iso_req_handle;
51055 +#endif //_EN_ISOC_
51056 +
51057 + /** DWC_otg ep data. */
51058 + dwc_ep_t dwc_ep;
51059 +
51060 + /** Pointer to PCD */
51061 + struct dwc_otg_pcd *pcd;
51062 +
51063 + void *priv;
51064 +} dwc_otg_pcd_ep_t;
51065 +
51066 +/** DWC_otg PCD Structure.
51067 + * This structure encapsulates the data for the dwc_otg PCD.
51068 + */
51069 +struct dwc_otg_pcd {
51070 + const struct dwc_otg_pcd_function_ops *fops;
51071 + /** The DWC otg device pointer */
51072 + struct dwc_otg_device *otg_dev;
51073 + /** Core Interface */
51074 + dwc_otg_core_if_t *core_if;
51075 + /** State of EP0 */
51076 + ep0state_e ep0state;
51077 + /** EP0 Request is pending */
51078 + unsigned ep0_pending:1;
51079 + /** Indicates when SET CONFIGURATION Request is in process */
51080 + unsigned request_config:1;
51081 + /** The state of the Remote Wakeup Enable. */
51082 + unsigned remote_wakeup_enable:1;
51083 + /** The state of the B-Device HNP Enable. */
51084 + unsigned b_hnp_enable:1;
51085 + /** The state of A-Device HNP Support. */
51086 + unsigned a_hnp_support:1;
51087 + /** The state of the A-Device Alt HNP support. */
51088 + unsigned a_alt_hnp_support:1;
51089 + /** Count of pending Requests */
51090 + unsigned request_pending;
51091 +
51092 + /** SETUP packet for EP0
51093 + * This structure is allocated as a DMA buffer on PCD initialization
51094 + * with enough space for up to 3 setup packets.
51095 + */
51096 + union {
51097 + usb_device_request_t req;
51098 + uint32_t d32[2];
51099 + } *setup_pkt;
51100 +
51101 + dwc_dma_t setup_pkt_dma_handle;
51102 +
51103 + /* Additional buffer and flag for CTRL_WR premature case */
51104 + uint8_t *backup_buf;
51105 + unsigned data_terminated;
51106 +
51107 + /** 2-byte dma buffer used to return status from GET_STATUS */
51108 + uint16_t *status_buf;
51109 + dwc_dma_t status_buf_dma_handle;
51110 +
51111 + /** EP0 */
51112 + dwc_otg_pcd_ep_t ep0;
51113 +
51114 + /** Array of IN EPs. */
51115 + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
51116 + /** Array of OUT EPs. */
51117 + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
51118 + /** number of valid EPs in the above array. */
51119 +// unsigned num_eps : 4;
51120 + dwc_spinlock_t *lock;
51121 +
51122 + /** Tasklet to defer starting of TEST mode transmissions until
51123 + * Status Phase has been completed.
51124 + */
51125 + dwc_tasklet_t *test_mode_tasklet;
51126 +
51127 + /** Tasklet to delay starting of xfer in DMA mode */
51128 + dwc_tasklet_t *start_xfer_tasklet;
51129 +
51130 + /** The test mode to enter when the tasklet is executed. */
51131 + unsigned test_mode;
51132 + /** The cfi_api structure that implements most of the CFI API
51133 + * and OTG specific core configuration functionality
51134 + */
51135 +#ifdef DWC_UTE_CFI
51136 + struct cfiobject *cfi;
51137 +#endif
51138 +
51139 +};
51140 +
51141 +static inline struct device *dwc_otg_pcd_to_dev(struct dwc_otg_pcd *pcd)
51142 +{
51143 + return &pcd->otg_dev->os_dep.platformdev->dev;
51144 +}
51145 +
51146 +//FIXME this functions should be static, and this prototypes should be removed
51147 +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
51148 +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
51149 + dwc_otg_pcd_request_t * req, int32_t status);
51150 +
51151 +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
51152 + void *req_handle);
51153 +
51154 +extern void do_test_mode(void *data);
51155 +#endif
51156 +#endif /* DWC_HOST_ONLY */
51157 --- /dev/null
51158 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
51159 @@ -0,0 +1,361 @@
51160 +/* ==========================================================================
51161 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
51162 + * $Revision: #11 $
51163 + * $Date: 2011/10/26 $
51164 + * $Change: 1873028 $
51165 + *
51166 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
51167 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
51168 + * otherwise expressly agreed to in writing between Synopsys and you.
51169 + *
51170 + * The Software IS NOT an item of Licensed Software or Licensed Product under
51171 + * any End User Software License Agreement or Agreement for Licensed Product
51172 + * with Synopsys or any supplement thereto. You are permitted to use and
51173 + * redistribute this Software in source and binary forms, with or without
51174 + * modification, provided that redistributions of source code must retain this
51175 + * notice. You may not view, use, disclose, copy or distribute this file or
51176 + * any information contained herein except pursuant to this license grant from
51177 + * Synopsys. If you do not agree with this notice, including the disclaimer
51178 + * below, then you are not authorized to use the Software.
51179 + *
51180 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
51181 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51182 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51183 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
51184 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51185 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51186 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51187 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51188 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51189 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51190 + * DAMAGE.
51191 + * ========================================================================== */
51192 +#ifndef DWC_HOST_ONLY
51193 +
51194 +#if !defined(__DWC_PCD_IF_H__)
51195 +#define __DWC_PCD_IF_H__
51196 +
51197 +//#include "dwc_os.h"
51198 +#include "dwc_otg_core_if.h"
51199 +#include "dwc_otg_driver.h"
51200 +
51201 +/** @file
51202 + * This file defines DWC_OTG PCD Core API.
51203 + */
51204 +
51205 +struct dwc_otg_pcd;
51206 +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
51207 +
51208 +/** Maxpacket size for EP0 */
51209 +#define MAX_EP0_SIZE 64
51210 +/** Maxpacket size for any EP */
51211 +#define MAX_PACKET_SIZE 1024
51212 +
51213 +/** @name Function Driver Callbacks */
51214 +/** @{ */
51215 +
51216 +/** This function will be called whenever a previously queued request has
51217 + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
51218 + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
51219 + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
51220 + * parameters. */
51221 +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51222 + void *req_handle, int32_t status,
51223 + uint32_t actual);
51224 +/**
51225 + * This function will be called whenever a previousle queued ISOC request has
51226 + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
51227 + * function.
51228 + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
51229 + * functions.
51230 + */
51231 +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51232 + void *req_handle, int proc_buf_num);
51233 +/** This function should handle any SETUP request that cannot be handled by the
51234 + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
51235 + * class-specific requests, etc. The function must non-blocking.
51236 + *
51237 + * Returns 0 on success.
51238 + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
51239 + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
51240 + * Returns -DWC_E_SHUTDOWN on any other error. */
51241 +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
51242 +/** This is called whenever the device has been disconnected. The function
51243 + * driver should take appropriate action to clean up all pending requests in the
51244 + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
51245 + * state. */
51246 +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
51247 +/** This function is called when device has been connected. */
51248 +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
51249 +/** This function is called when device has been suspended */
51250 +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
51251 +/** This function is called when device has received LPM tokens, i.e.
51252 + * device has been sent to sleep state. */
51253 +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
51254 +/** This function is called when device has been resumed
51255 + * from suspend(L2) or L1 sleep state. */
51256 +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
51257 +/** This function is called whenever hnp params has been changed.
51258 + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
51259 + * to get hnp parameters. */
51260 +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
51261 +/** This function is called whenever USB RESET is detected. */
51262 +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
51263 +
51264 +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
51265 +
51266 +/**
51267 + *
51268 + * @param ep_handle Void pointer to the usb_ep structure
51269 + * @param ereq_port Pointer to the extended request structure created in the
51270 + * portable part.
51271 + */
51272 +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51273 + void *req_handle, int32_t status,
51274 + void *ereq_port);
51275 +/** Function Driver Ops Data Structure */
51276 +struct dwc_otg_pcd_function_ops {
51277 + dwc_connect_cb_t connect;
51278 + dwc_disconnect_cb_t disconnect;
51279 + dwc_setup_cb_t setup;
51280 + dwc_completion_cb_t complete;
51281 + dwc_isoc_completion_cb_t isoc_complete;
51282 + dwc_suspend_cb_t suspend;
51283 + dwc_sleep_cb_t sleep;
51284 + dwc_resume_cb_t resume;
51285 + dwc_reset_cb_t reset;
51286 + dwc_hnp_params_changed_cb_t hnp_changed;
51287 + cfi_setup_cb_t cfi_setup;
51288 +#ifdef DWC_UTE_PER_IO
51289 + xiso_completion_cb_t xisoc_complete;
51290 +#endif
51291 +};
51292 +/** @} */
51293 +
51294 +/** @name Function Driver Functions */
51295 +/** @{ */
51296 +
51297 +/** Call this function to get pointer on dwc_otg_pcd_t,
51298 + * this pointer will be used for all PCD API functions.
51299 + *
51300 + * @param core_if The DWC_OTG Core
51301 + */
51302 +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev);
51303 +
51304 +/** Frees PCD allocated by dwc_otg_pcd_init
51305 + *
51306 + * @param pcd The PCD
51307 + */
51308 +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
51309 +
51310 +/** Call this to bind the function driver to the PCD Core.
51311 + *
51312 + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
51313 + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
51314 + */
51315 +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
51316 + const struct dwc_otg_pcd_function_ops *fops);
51317 +
51318 +/** Enables an endpoint for use. This function enables an endpoint in
51319 + * the PCD. The endpoint is described by the ep_desc which has the
51320 + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
51321 + * to the endpoint from other API functions and in callbacks. Normally this
51322 + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
51323 + * core for that interface.
51324 + *
51325 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51326 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51327 + * Returns 0 on success.
51328 + *
51329 + * @param pcd The PCD
51330 + * @param ep_desc Endpoint descriptor
51331 + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
51332 + */
51333 +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
51334 + const uint8_t * ep_desc, void *usb_ep);
51335 +
51336 +/** Disable the endpoint referenced by ep_handle.
51337 + *
51338 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51339 + * Returns -DWC_E_SHUTDOWN if any other error occurred.
51340 + * Returns 0 on success. */
51341 +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
51342 +
51343 +/** Queue a data transfer request on the endpoint referenced by ep_handle.
51344 + * After the transfer is completes, the complete callback will be called with
51345 + * the request status.
51346 + *
51347 + * @param pcd The PCD
51348 + * @param ep_handle The handle of the endpoint
51349 + * @param buf The buffer for the data
51350 + * @param dma_buf The DMA buffer for the data
51351 + * @param buflen The length of the data transfer
51352 + * @param zero Specifies whether to send zero length last packet.
51353 + * @param req_handle Set this handle to any value to use to reference this
51354 + * request in the ep_dequeue function or from the complete callback
51355 + * @param atomic_alloc If driver need to perform atomic allocations
51356 + * for internal data structures.
51357 + *
51358 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51359 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51360 + * Returns 0 on success. */
51361 +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
51362 + uint8_t * buf, dwc_dma_t dma_buf,
51363 + uint32_t buflen, int zero, void *req_handle,
51364 + int atomic_alloc);
51365 +#ifdef DWC_UTE_PER_IO
51366 +/**
51367 + *
51368 + * @param ereq_nonport Pointer to the extended request part of the
51369 + * usb_request structure defined in usb_gadget.h file.
51370 + */
51371 +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
51372 + uint8_t * buf, dwc_dma_t dma_buf,
51373 + uint32_t buflen, int zero,
51374 + void *req_handle, int atomic_alloc,
51375 + void *ereq_nonport);
51376 +
51377 +#endif
51378 +
51379 +/** De-queue the specified data transfer that has not yet completed.
51380 + *
51381 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51382 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51383 + * Returns 0 on success. */
51384 +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
51385 + void *req_handle);
51386 +
51387 +/** Halt (STALL) an endpoint or clear it.
51388 + *
51389 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51390 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51391 + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
51392 + * Returns 0 on success. */
51393 +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
51394 +
51395 +/** This function */
51396 +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
51397 +
51398 +/** This function should be called on every hardware interrupt */
51399 +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
51400 +
51401 +/** This function returns current frame number */
51402 +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
51403 +
51404 +/**
51405 + * Start isochronous transfers on the endpoint referenced by ep_handle.
51406 + * For isochronous transfers duble buffering is used.
51407 + * After processing each of buffers comlete callback will be called with
51408 + * status for each transaction.
51409 + *
51410 + * @param pcd The PCD
51411 + * @param ep_handle The handle of the endpoint
51412 + * @param buf0 The virtual address of first data buffer
51413 + * @param buf1 The virtual address of second data buffer
51414 + * @param dma0 The DMA address of first data buffer
51415 + * @param dma1 The DMA address of second data buffer
51416 + * @param sync_frame Data pattern frame number
51417 + * @param dp_frame Data size for pattern frame
51418 + * @param data_per_frame Data size for regular frame
51419 + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
51420 + * @param buf_proc_intrvl Interval of ISOC Buffer processing
51421 + * @param req_handle Handle of ISOC request
51422 + * @param atomic_alloc Specefies whether to perform atomic allocation for
51423 + * internal data structures.
51424 + *
51425 + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
51426 + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
51427 + * Returns -DW_E_SHUTDOWN for any other error.
51428 + * Returns 0 on success
51429 + */
51430 +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
51431 + uint8_t * buf0, uint8_t * buf1,
51432 + dwc_dma_t dma0, dwc_dma_t dma1,
51433 + int sync_frame, int dp_frame,
51434 + int data_per_frame, int start_frame,
51435 + int buf_proc_intrvl, void *req_handle,
51436 + int atomic_alloc);
51437 +
51438 +/** Stop ISOC transfers on endpoint referenced by ep_handle.
51439 + *
51440 + * @param pcd The PCD
51441 + * @param ep_handle The handle of the endpoint
51442 + * @param req_handle Handle of ISOC request
51443 + *
51444 + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
51445 + * Returns 0 on success
51446 + */
51447 +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
51448 + void *req_handle);
51449 +
51450 +/** Get ISOC packet status.
51451 + *
51452 + * @param pcd The PCD
51453 + * @param ep_handle The handle of the endpoint
51454 + * @param iso_req_handle Isochronoush request handle
51455 + * @param packet Number of packet
51456 + * @param status Out parameter for returning status
51457 + * @param actual Out parameter for returning actual length
51458 + * @param offset Out parameter for returning offset
51459 + *
51460 + */
51461 +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
51462 + void *ep_handle,
51463 + void *iso_req_handle, int packet,
51464 + int *status, int *actual,
51465 + int *offset);
51466 +
51467 +/** Get ISOC packet count.
51468 + *
51469 + * @param pcd The PCD
51470 + * @param ep_handle The handle of the endpoint
51471 + * @param iso_req_handle
51472 + */
51473 +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
51474 + void *ep_handle,
51475 + void *iso_req_handle);
51476 +
51477 +/** This function starts the SRP Protocol if no session is in progress. If
51478 + * a session is already in progress, but the device is suspended,
51479 + * remote wakeup signaling is started.
51480 + */
51481 +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
51482 +
51483 +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
51484 +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
51485 +
51486 +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
51487 +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
51488 +
51489 +/** Initiate SRP */
51490 +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
51491 +
51492 +/** Starts remote wakeup signaling. */
51493 +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
51494 +
51495 +/** Starts micorsecond soft disconnect. */
51496 +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
51497 +/** This function returns whether device is dualspeed.*/
51498 +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
51499 +
51500 +/** This function returns whether device is otg. */
51501 +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
51502 +
51503 +/** These functions allow to get hnp parameters */
51504 +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
51505 +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
51506 +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
51507 +
51508 +/** CFI specific Interface functions */
51509 +/** Allocate a cfi buffer */
51510 +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
51511 + dwc_dma_t * addr, size_t buflen,
51512 + int flags);
51513 +
51514 +/******************************************************************************/
51515 +
51516 +/** @} */
51517 +
51518 +#endif /* __DWC_PCD_IF_H__ */
51519 +
51520 +#endif /* DWC_HOST_ONLY */
51521 --- /dev/null
51522 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
51523 @@ -0,0 +1,5148 @@
51524 +/* ==========================================================================
51525 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
51526 + * $Revision: #116 $
51527 + * $Date: 2012/08/10 $
51528 + * $Change: 2047372 $
51529 + *
51530 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
51531 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
51532 + * otherwise expressly agreed to in writing between Synopsys and you.
51533 + *
51534 + * The Software IS NOT an item of Licensed Software or Licensed Product under
51535 + * any End User Software License Agreement or Agreement for Licensed Product
51536 + * with Synopsys or any supplement thereto. You are permitted to use and
51537 + * redistribute this Software in source and binary forms, with or without
51538 + * modification, provided that redistributions of source code must retain this
51539 + * notice. You may not view, use, disclose, copy or distribute this file or
51540 + * any information contained herein except pursuant to this license grant from
51541 + * Synopsys. If you do not agree with this notice, including the disclaimer
51542 + * below, then you are not authorized to use the Software.
51543 + *
51544 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
51545 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51546 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51547 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
51548 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51549 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51550 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51551 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51552 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51553 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51554 + * DAMAGE.
51555 + * ========================================================================== */
51556 +#ifndef DWC_HOST_ONLY
51557 +
51558 +#include "dwc_otg_pcd.h"
51559 +
51560 +#ifdef DWC_UTE_CFI
51561 +#include "dwc_otg_cfi.h"
51562 +#endif
51563 +
51564 +#ifdef DWC_UTE_PER_IO
51565 +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
51566 +#endif
51567 +//#define PRINT_CFI_DMA_DESCS
51568 +
51569 +#define DEBUG_EP0
51570 +
51571 +/**
51572 + * This function updates OTG.
51573 + */
51574 +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
51575 +{
51576 +
51577 + if (reset) {
51578 + pcd->b_hnp_enable = 0;
51579 + pcd->a_hnp_support = 0;
51580 + pcd->a_alt_hnp_support = 0;
51581 + }
51582 +
51583 + if (pcd->fops->hnp_changed) {
51584 + pcd->fops->hnp_changed(pcd);
51585 + }
51586 +}
51587 +
51588 +/** @file
51589 + * This file contains the implementation of the PCD Interrupt handlers.
51590 + *
51591 + * The PCD handles the device interrupts. Many conditions can cause a
51592 + * device interrupt. When an interrupt occurs, the device interrupt
51593 + * service routine determines the cause of the interrupt and
51594 + * dispatches handling to the appropriate function. These interrupt
51595 + * handling functions are described below.
51596 + * All interrupt registers are processed from LSB to MSB.
51597 + */
51598 +
51599 +/**
51600 + * This function prints the ep0 state for debug purposes.
51601 + */
51602 +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
51603 +{
51604 +#ifdef DEBUG
51605 + char str[40];
51606 +
51607 + switch (pcd->ep0state) {
51608 + case EP0_DISCONNECT:
51609 + dwc_strcpy(str, "EP0_DISCONNECT");
51610 + break;
51611 + case EP0_IDLE:
51612 + dwc_strcpy(str, "EP0_IDLE");
51613 + break;
51614 + case EP0_IN_DATA_PHASE:
51615 + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
51616 + break;
51617 + case EP0_OUT_DATA_PHASE:
51618 + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
51619 + break;
51620 + case EP0_IN_STATUS_PHASE:
51621 + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
51622 + break;
51623 + case EP0_OUT_STATUS_PHASE:
51624 + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
51625 + break;
51626 + case EP0_STALL:
51627 + dwc_strcpy(str, "EP0_STALL");
51628 + break;
51629 + default:
51630 + dwc_strcpy(str, "EP0_INVALID");
51631 + }
51632 +
51633 + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
51634 +#endif
51635 +}
51636 +
51637 +/**
51638 + * This function calculate the size of the payload in the memory
51639 + * for out endpoints and prints size for debug purposes(used in
51640 + * 2.93a DevOutNak feature).
51641 + */
51642 +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
51643 +{
51644 +#ifdef DEBUG
51645 + deptsiz_data_t deptsiz_init = {.d32 = 0 };
51646 + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
51647 + int pack_num;
51648 + unsigned payload;
51649 +
51650 + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
51651 + deptsiz_updt.d32 =
51652 + DWC_READ_REG32(&pcd->core_if->dev_if->
51653 + out_ep_regs[ep->num]->doeptsiz);
51654 + /* Payload will be */
51655 + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
51656 + /* Packet count is decremented every time a packet
51657 + * is written to the RxFIFO not in to the external memory
51658 + * So, if payload == 0, then it means no packet was sent to ext memory*/
51659 + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
51660 + DWC_DEBUGPL(DBG_PCDV,
51661 + "Payload for EP%d-%s\n",
51662 + ep->num, (ep->is_in ? "IN" : "OUT"));
51663 + DWC_DEBUGPL(DBG_PCDV,
51664 + "Number of transfered bytes = 0x%08x\n", payload);
51665 + DWC_DEBUGPL(DBG_PCDV,
51666 + "Number of transfered packets = %d\n", pack_num);
51667 +#endif
51668 +}
51669 +
51670 +
51671 +#ifdef DWC_UTE_CFI
51672 +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
51673 + const uint8_t * epname, int descnum)
51674 +{
51675 + CFI_INFO
51676 + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
51677 + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
51678 + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
51679 + ddesc->status.b.bs);
51680 +}
51681 +#endif
51682 +
51683 +/**
51684 + * This function returns pointer to in ep struct with number ep_num
51685 + */
51686 +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
51687 +{
51688 + int i;
51689 + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
51690 + if (ep_num == 0) {
51691 + return &pcd->ep0;
51692 + } else {
51693 + for (i = 0; i < num_in_eps; ++i) {
51694 + if (pcd->in_ep[i].dwc_ep.num == ep_num)
51695 + return &pcd->in_ep[i];
51696 + }
51697 + return 0;
51698 + }
51699 +}
51700 +
51701 +/**
51702 + * This function returns pointer to out ep struct with number ep_num
51703 + */
51704 +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
51705 +{
51706 + int i;
51707 + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
51708 + if (ep_num == 0) {
51709 + return &pcd->ep0;
51710 + } else {
51711 + for (i = 0; i < num_out_eps; ++i) {
51712 + if (pcd->out_ep[i].dwc_ep.num == ep_num)
51713 + return &pcd->out_ep[i];
51714 + }
51715 + return 0;
51716 + }
51717 +}
51718 +
51719 +/**
51720 + * This functions gets a pointer to an EP from the wIndex address
51721 + * value of the control request.
51722 + */
51723 +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
51724 +{
51725 + dwc_otg_pcd_ep_t *ep;
51726 + uint32_t ep_num = UE_GET_ADDR(wIndex);
51727 +
51728 + if (ep_num == 0) {
51729 + ep = &pcd->ep0;
51730 + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
51731 + ep = &pcd->in_ep[ep_num - 1];
51732 + } else {
51733 + ep = &pcd->out_ep[ep_num - 1];
51734 + }
51735 +
51736 + return ep;
51737 +}
51738 +
51739 +/**
51740 + * This function checks the EP request queue, if the queue is not
51741 + * empty the next request is started.
51742 + */
51743 +void start_next_request(dwc_otg_pcd_ep_t * ep)
51744 +{
51745 + dwc_otg_pcd_request_t *req = 0;
51746 + uint32_t max_transfer =
51747 + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
51748 +
51749 +#ifdef DWC_UTE_CFI
51750 + struct dwc_otg_pcd *pcd;
51751 + pcd = ep->pcd;
51752 +#endif
51753 +
51754 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
51755 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
51756 +
51757 +#ifdef DWC_UTE_CFI
51758 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
51759 + ep->dwc_ep.cfi_req_len = req->length;
51760 + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
51761 + } else {
51762 +#endif
51763 + /* Setup and start the Transfer */
51764 + if (req->dw_align_buf) {
51765 + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
51766 + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
51767 + ep->dwc_ep.xfer_buff = req->dw_align_buf;
51768 + } else {
51769 + ep->dwc_ep.dma_addr = req->dma;
51770 + ep->dwc_ep.start_xfer_buff = req->buf;
51771 + ep->dwc_ep.xfer_buff = req->buf;
51772 + }
51773 + ep->dwc_ep.sent_zlp = 0;
51774 + ep->dwc_ep.total_len = req->length;
51775 + ep->dwc_ep.xfer_len = 0;
51776 + ep->dwc_ep.xfer_count = 0;
51777 +
51778 + ep->dwc_ep.maxxfer = max_transfer;
51779 + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
51780 + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
51781 + - (DDMA_MAX_TRANSFER_SIZE % 4);
51782 + if (ep->dwc_ep.is_in) {
51783 + if (ep->dwc_ep.maxxfer >
51784 + DDMA_MAX_TRANSFER_SIZE) {
51785 + ep->dwc_ep.maxxfer =
51786 + DDMA_MAX_TRANSFER_SIZE;
51787 + }
51788 + } else {
51789 + if (ep->dwc_ep.maxxfer > out_max_xfer) {
51790 + ep->dwc_ep.maxxfer =
51791 + out_max_xfer;
51792 + }
51793 + }
51794 + }
51795 + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
51796 + ep->dwc_ep.maxxfer -=
51797 + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
51798 + }
51799 + if (req->sent_zlp) {
51800 + if ((ep->dwc_ep.total_len %
51801 + ep->dwc_ep.maxpacket == 0)
51802 + && (ep->dwc_ep.total_len != 0)) {
51803 + ep->dwc_ep.sent_zlp = 1;
51804 + }
51805 +
51806 + }
51807 +#ifdef DWC_UTE_CFI
51808 + }
51809 +#endif
51810 + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
51811 + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
51812 + DWC_PRINTF("There are no more ISOC requests \n");
51813 + ep->dwc_ep.frame_num = 0xFFFFFFFF;
51814 + }
51815 +}
51816 +
51817 +/**
51818 + * This function handles the SOF Interrupts. At this time the SOF
51819 + * Interrupt is disabled.
51820 + */
51821 +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
51822 +{
51823 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51824 +
51825 + gintsts_data_t gintsts;
51826 +
51827 + DWC_DEBUGPL(DBG_PCD, "SOF\n");
51828 +
51829 + /* Clear interrupt */
51830 + gintsts.d32 = 0;
51831 + gintsts.b.sofintr = 1;
51832 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
51833 +
51834 + return 1;
51835 +}
51836 +
51837 +/**
51838 + * This function handles the Rx Status Queue Level Interrupt, which
51839 + * indicates that there is a least one packet in the Rx FIFO. The
51840 + * packets are moved from the FIFO to memory, where they will be
51841 + * processed when the Endpoint Interrupt Register indicates Transfer
51842 + * Complete or SETUP Phase Done.
51843 + *
51844 + * Repeat the following until the Rx Status Queue is empty:
51845 + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
51846 + * info
51847 + * -# If Receive FIFO is empty then skip to step Clear the interrupt
51848 + * and exit
51849 + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
51850 + * SETUP data to the buffer
51851 + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
51852 + * to the destination buffer
51853 + */
51854 +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
51855 +{
51856 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51857 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
51858 + gintmsk_data_t gintmask = {.d32 = 0 };
51859 + device_grxsts_data_t status;
51860 + dwc_otg_pcd_ep_t *ep;
51861 + gintsts_data_t gintsts;
51862 +#ifdef DEBUG
51863 + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
51864 +#endif
51865 +
51866 + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
51867 + /* Disable the Rx Status Queue Level interrupt */
51868 + gintmask.b.rxstsqlvl = 1;
51869 + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
51870 +
51871 + /* Get the Status from the top of the FIFO */
51872 + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
51873 +
51874 + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
51875 + "pktsts:%x Frame:%d(0x%0x)\n",
51876 + status.b.epnum, status.b.bcnt,
51877 + dpid_str[status.b.dpid],
51878 + status.b.pktsts, status.b.fn, status.b.fn);
51879 + /* Get pointer to EP structure */
51880 + ep = get_out_ep(pcd, status.b.epnum);
51881 +
51882 + switch (status.b.pktsts) {
51883 + case DWC_DSTS_GOUT_NAK:
51884 + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
51885 + break;
51886 + case DWC_STS_DATA_UPDT:
51887 + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
51888 + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
51889 + /** @todo NGS Check for buffer overflow? */
51890 + dwc_otg_read_packet(core_if,
51891 + ep->dwc_ep.xfer_buff,
51892 + status.b.bcnt);
51893 + ep->dwc_ep.xfer_count += status.b.bcnt;
51894 + ep->dwc_ep.xfer_buff += status.b.bcnt;
51895 + }
51896 + break;
51897 + case DWC_STS_XFER_COMP:
51898 + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
51899 + break;
51900 + case DWC_DSTS_SETUP_COMP:
51901 +#ifdef DEBUG_EP0
51902 + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
51903 +#endif
51904 + break;
51905 + case DWC_DSTS_SETUP_UPDT:
51906 + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
51907 +#ifdef DEBUG_EP0
51908 + DWC_DEBUGPL(DBG_PCD,
51909 + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
51910 + pcd->setup_pkt->req.bmRequestType,
51911 + pcd->setup_pkt->req.bRequest,
51912 + UGETW(pcd->setup_pkt->req.wValue),
51913 + UGETW(pcd->setup_pkt->req.wIndex),
51914 + UGETW(pcd->setup_pkt->req.wLength));
51915 +#endif
51916 + ep->dwc_ep.xfer_count += status.b.bcnt;
51917 + break;
51918 + default:
51919 + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
51920 + status.b.pktsts);
51921 + break;
51922 + }
51923 +
51924 + /* Enable the Rx Status Queue Level interrupt */
51925 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
51926 + /* Clear interrupt */
51927 + gintsts.d32 = 0;
51928 + gintsts.b.rxstsqlvl = 1;
51929 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
51930 +
51931 + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
51932 + return 1;
51933 +}
51934 +
51935 +/**
51936 + * This function examines the Device IN Token Learning Queue to
51937 + * determine the EP number of the last IN token received. This
51938 + * implementation is for the Mass Storage device where there are only
51939 + * 2 IN EPs (Control-IN and BULK-IN).
51940 + *
51941 + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
51942 + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
51943 + *
51944 + * @param core_if Programming view of DWC_otg controller.
51945 + *
51946 + */
51947 +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
51948 +{
51949 + dwc_otg_device_global_regs_t *dev_global_regs =
51950 + core_if->dev_if->dev_global_regs;
51951 + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
51952 + /* Number of Token Queue Registers */
51953 + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
51954 + dtknq1_data_t dtknqr1;
51955 + uint32_t in_tkn_epnums[4];
51956 + int ndx = 0;
51957 + int i = 0;
51958 + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
51959 + int epnum = 0;
51960 +
51961 + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
51962 +
51963 + /* Read the DTKNQ Registers */
51964 + for (i = 0; i < DTKNQ_REG_CNT; i++) {
51965 + in_tkn_epnums[i] = DWC_READ_REG32(addr);
51966 + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
51967 + in_tkn_epnums[i]);
51968 + if (addr == &dev_global_regs->dvbusdis) {
51969 + addr = &dev_global_regs->dtknqr3_dthrctl;
51970 + } else {
51971 + ++addr;
51972 + }
51973 +
51974 + }
51975 +
51976 + /* Copy the DTKNQR1 data to the bit field. */
51977 + dtknqr1.d32 = in_tkn_epnums[0];
51978 + /* Get the EP numbers */
51979 + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
51980 + ndx = dtknqr1.b.intknwptr - 1;
51981 +
51982 + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
51983 + if (ndx == -1) {
51984 + /** @todo Find a simpler way to calculate the max
51985 + * queue position.*/
51986 + int cnt = TOKEN_Q_DEPTH;
51987 + if (TOKEN_Q_DEPTH <= 6) {
51988 + cnt = TOKEN_Q_DEPTH - 1;
51989 + } else if (TOKEN_Q_DEPTH <= 14) {
51990 + cnt = TOKEN_Q_DEPTH - 7;
51991 + } else if (TOKEN_Q_DEPTH <= 22) {
51992 + cnt = TOKEN_Q_DEPTH - 15;
51993 + } else {
51994 + cnt = TOKEN_Q_DEPTH - 23;
51995 + }
51996 + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
51997 + } else {
51998 + if (ndx <= 5) {
51999 + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
52000 + } else if (ndx <= 13) {
52001 + ndx -= 6;
52002 + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
52003 + } else if (ndx <= 21) {
52004 + ndx -= 14;
52005 + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
52006 + } else if (ndx <= 29) {
52007 + ndx -= 22;
52008 + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
52009 + }
52010 + }
52011 + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
52012 + return epnum;
52013 +}
52014 +
52015 +/**
52016 + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
52017 + * The active request is checked for the next packet to be loaded into
52018 + * the non-periodic Tx FIFO.
52019 + */
52020 +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
52021 +{
52022 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52023 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
52024 + dwc_otg_dev_in_ep_regs_t *ep_regs;
52025 + gnptxsts_data_t txstatus = {.d32 = 0 };
52026 + gintsts_data_t gintsts;
52027 +
52028 + int epnum = 0;
52029 + dwc_otg_pcd_ep_t *ep = 0;
52030 + uint32_t len = 0;
52031 + int dwords;
52032 +
52033 + /* Get the epnum from the IN Token Learning Queue. */
52034 + epnum = get_ep_of_last_in_token(core_if);
52035 + ep = get_in_ep(pcd, epnum);
52036 +
52037 + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
52038 +
52039 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
52040 +
52041 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
52042 + if (len > ep->dwc_ep.maxpacket) {
52043 + len = ep->dwc_ep.maxpacket;
52044 + }
52045 + dwords = (len + 3) / 4;
52046 +
52047 + /* While there is space in the queue and space in the FIFO and
52048 + * More data to tranfer, Write packets to the Tx FIFO */
52049 + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
52050 + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
52051 +
52052 + while (txstatus.b.nptxqspcavail > 0 &&
52053 + txstatus.b.nptxfspcavail > dwords &&
52054 + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
52055 + /* Write the FIFO */
52056 + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
52057 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
52058 +
52059 + if (len > ep->dwc_ep.maxpacket) {
52060 + len = ep->dwc_ep.maxpacket;
52061 + }
52062 +
52063 + dwords = (len + 3) / 4;
52064 + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
52065 + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
52066 + }
52067 +
52068 + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
52069 + DWC_READ_REG32(&global_regs->gnptxsts));
52070 +
52071 + /* Clear interrupt */
52072 + gintsts.d32 = 0;
52073 + gintsts.b.nptxfempty = 1;
52074 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
52075 +
52076 + return 1;
52077 +}
52078 +
52079 +/**
52080 + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
52081 + * The active request is checked for the next packet to be loaded into
52082 + * apropriate Tx FIFO.
52083 + */
52084 +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
52085 +{
52086 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52087 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52088 + dwc_otg_dev_in_ep_regs_t *ep_regs;
52089 + dtxfsts_data_t txstatus = {.d32 = 0 };
52090 + dwc_otg_pcd_ep_t *ep = 0;
52091 + uint32_t len = 0;
52092 + int dwords;
52093 +
52094 + ep = get_in_ep(pcd, epnum);
52095 +
52096 + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
52097 +
52098 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
52099 +
52100 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
52101 +
52102 + if (len > ep->dwc_ep.maxpacket) {
52103 + len = ep->dwc_ep.maxpacket;
52104 + }
52105 +
52106 + dwords = (len + 3) / 4;
52107 +
52108 + /* While there is space in the queue and space in the FIFO and
52109 + * More data to tranfer, Write packets to the Tx FIFO */
52110 + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
52111 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
52112 +
52113 + while (txstatus.b.txfspcavail > dwords &&
52114 + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
52115 + ep->dwc_ep.xfer_len != 0) {
52116 + /* Write the FIFO */
52117 + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
52118 +
52119 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
52120 + if (len > ep->dwc_ep.maxpacket) {
52121 + len = ep->dwc_ep.maxpacket;
52122 + }
52123 +
52124 + dwords = (len + 3) / 4;
52125 + txstatus.d32 =
52126 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
52127 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
52128 + txstatus.d32);
52129 + }
52130 +
52131 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
52132 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
52133 +
52134 + return 1;
52135 +}
52136 +
52137 +/**
52138 + * This function is called when the Device is disconnected. It stops
52139 + * any active requests and informs the Gadget driver of the
52140 + * disconnect.
52141 + */
52142 +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
52143 +{
52144 + int i, num_in_eps, num_out_eps;
52145 + dwc_otg_pcd_ep_t *ep;
52146 +
52147 + gintmsk_data_t intr_mask = {.d32 = 0 };
52148 +
52149 + DWC_SPINLOCK(pcd->lock);
52150 +
52151 + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
52152 + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
52153 +
52154 + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
52155 + /* don't disconnect drivers more than once */
52156 + if (pcd->ep0state == EP0_DISCONNECT) {
52157 + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
52158 + DWC_SPINUNLOCK(pcd->lock);
52159 + return;
52160 + }
52161 + pcd->ep0state = EP0_DISCONNECT;
52162 +
52163 + /* Reset the OTG state. */
52164 + dwc_otg_pcd_update_otg(pcd, 1);
52165 +
52166 + /* Disable the NP Tx Fifo Empty Interrupt. */
52167 + intr_mask.b.nptxfempty = 1;
52168 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52169 + intr_mask.d32, 0);
52170 +
52171 + /* Flush the FIFOs */
52172 + /**@todo NGS Flush Periodic FIFOs */
52173 + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
52174 + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
52175 +
52176 + /* prevent new request submissions, kill any outstanding requests */
52177 + ep = &pcd->ep0;
52178 + dwc_otg_request_nuke(ep);
52179 + /* prevent new request submissions, kill any outstanding requests */
52180 + for (i = 0; i < num_in_eps; i++) {
52181 + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
52182 + dwc_otg_request_nuke(ep);
52183 + }
52184 + /* prevent new request submissions, kill any outstanding requests */
52185 + for (i = 0; i < num_out_eps; i++) {
52186 + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
52187 + dwc_otg_request_nuke(ep);
52188 + }
52189 +
52190 + /* report disconnect; the driver is already quiesced */
52191 + if (pcd->fops->disconnect) {
52192 + DWC_SPINUNLOCK(pcd->lock);
52193 + pcd->fops->disconnect(pcd);
52194 + DWC_SPINLOCK(pcd->lock);
52195 + }
52196 + DWC_SPINUNLOCK(pcd->lock);
52197 +}
52198 +
52199 +/**
52200 + * This interrupt indicates that ...
52201 + */
52202 +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
52203 +{
52204 + gintmsk_data_t intr_mask = {.d32 = 0 };
52205 + gintsts_data_t gintsts;
52206 +
52207 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
52208 + intr_mask.b.i2cintr = 1;
52209 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52210 + intr_mask.d32, 0);
52211 +
52212 + /* Clear interrupt */
52213 + gintsts.d32 = 0;
52214 + gintsts.b.i2cintr = 1;
52215 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52216 + gintsts.d32);
52217 + return 1;
52218 +}
52219 +
52220 +/**
52221 + * This interrupt indicates that ...
52222 + */
52223 +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
52224 +{
52225 + gintsts_data_t gintsts;
52226 +#if defined(VERBOSE)
52227 + DWC_PRINTF("Early Suspend Detected\n");
52228 +#endif
52229 +
52230 + /* Clear interrupt */
52231 + gintsts.d32 = 0;
52232 + gintsts.b.erlysuspend = 1;
52233 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52234 + gintsts.d32);
52235 + return 1;
52236 +}
52237 +
52238 +/**
52239 + * This function configures EPO to receive SETUP packets.
52240 + *
52241 + * @todo NGS: Update the comments from the HW FS.
52242 + *
52243 + * -# Program the following fields in the endpoint specific registers
52244 + * for Control OUT EP 0, in order to receive a setup packet
52245 + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
52246 + * setup packets)
52247 + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
52248 + * to back setup packets)
52249 + * - In DMA mode, DOEPDMA0 Register with a memory address to
52250 + * store any setup packets received
52251 + *
52252 + * @param core_if Programming view of DWC_otg controller.
52253 + * @param pcd Programming view of the PCD.
52254 + */
52255 +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
52256 + dwc_otg_pcd_t * pcd)
52257 +{
52258 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52259 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
52260 + dwc_otg_dev_dma_desc_t *dma_desc;
52261 + depctl_data_t doepctl = {.d32 = 0 };
52262 +
52263 +#ifdef VERBOSE
52264 + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
52265 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
52266 +#endif
52267 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
52268 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
52269 + if (doepctl.b.epena) {
52270 + return;
52271 + }
52272 + }
52273 +
52274 + doeptsize0.b.supcnt = 3;
52275 + doeptsize0.b.pktcnt = 1;
52276 + doeptsize0.b.xfersize = 8 * 3;
52277 +
52278 + if (core_if->dma_enable) {
52279 + if (!core_if->dma_desc_enable) {
52280 + /** put here as for Hermes mode deptisz register should not be written */
52281 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
52282 + doeptsize0.d32);
52283 +
52284 + /** @todo dma needs to handle multiple setup packets (up to 3) */
52285 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
52286 + pcd->setup_pkt_dma_handle);
52287 + } else {
52288 + dev_if->setup_desc_index =
52289 + (dev_if->setup_desc_index + 1) & 1;
52290 + dma_desc =
52291 + dev_if->setup_desc_addr[dev_if->setup_desc_index];
52292 +
52293 + /** DMA Descriptor Setup */
52294 + dma_desc->status.b.bs = BS_HOST_BUSY;
52295 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
52296 + dma_desc->status.b.sr = 0;
52297 + dma_desc->status.b.mtrf = 0;
52298 + }
52299 + dma_desc->status.b.l = 1;
52300 + dma_desc->status.b.ioc = 1;
52301 + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
52302 + dma_desc->buf = pcd->setup_pkt_dma_handle;
52303 + dma_desc->status.b.sts = 0;
52304 + dma_desc->status.b.bs = BS_HOST_READY;
52305 +
52306 + /** DOEPDMA0 Register write */
52307 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
52308 + dev_if->dma_setup_desc_addr
52309 + [dev_if->setup_desc_index]);
52310 + }
52311 +
52312 + } else {
52313 + /** put here as for Hermes mode deptisz register should not be written */
52314 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
52315 + doeptsize0.d32);
52316 + }
52317 +
52318 + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
52319 + doepctl.d32 = 0;
52320 + doepctl.b.epena = 1;
52321 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
52322 + doepctl.b.cnak = 1;
52323 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
52324 + } else {
52325 + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
52326 + }
52327 +
52328 +#ifdef VERBOSE
52329 + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
52330 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
52331 + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
52332 + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
52333 +#endif
52334 +}
52335 +
52336 +/**
52337 + * This interrupt occurs when a USB Reset is detected. When the USB
52338 + * Reset Interrupt occurs the device state is set to DEFAULT and the
52339 + * EP0 state is set to IDLE.
52340 + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
52341 + * -# Unmask the following interrupt bits
52342 + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
52343 + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
52344 + * - DOEPMSK.SETUP = 1
52345 + * - DOEPMSK.XferCompl = 1
52346 + * - DIEPMSK.XferCompl = 1
52347 + * - DIEPMSK.TimeOut = 1
52348 + * -# Program the following fields in the endpoint specific registers
52349 + * for Control OUT EP 0, in order to receive a setup packet
52350 + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
52351 + * setup packets)
52352 + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
52353 + * to back setup packets)
52354 + * - In DMA mode, DOEPDMA0 Register with a memory address to
52355 + * store any setup packets received
52356 + * At this point, all the required initialization, except for enabling
52357 + * the control 0 OUT endpoint is done, for receiving SETUP packets.
52358 + */
52359 +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
52360 +{
52361 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52362 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52363 + depctl_data_t doepctl = {.d32 = 0 };
52364 + depctl_data_t diepctl = {.d32 = 0 };
52365 + daint_data_t daintmsk = {.d32 = 0 };
52366 + doepmsk_data_t doepmsk = {.d32 = 0 };
52367 + diepmsk_data_t diepmsk = {.d32 = 0 };
52368 + dcfg_data_t dcfg = {.d32 = 0 };
52369 + grstctl_t resetctl = {.d32 = 0 };
52370 + dctl_data_t dctl = {.d32 = 0 };
52371 + int i = 0;
52372 + gintsts_data_t gintsts;
52373 + pcgcctl_data_t power = {.d32 = 0 };
52374 +
52375 + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
52376 + if (power.b.stoppclk) {
52377 + power.d32 = 0;
52378 + power.b.stoppclk = 1;
52379 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52380 +
52381 + power.b.pwrclmp = 1;
52382 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52383 +
52384 + power.b.rstpdwnmodule = 1;
52385 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52386 + }
52387 +
52388 + core_if->lx_state = DWC_OTG_L0;
52389 +
52390 + DWC_PRINTF("USB RESET\n");
52391 +#ifdef DWC_EN_ISOC
52392 + for (i = 1; i < 16; ++i) {
52393 + dwc_otg_pcd_ep_t *ep;
52394 + dwc_ep_t *dwc_ep;
52395 + ep = get_in_ep(pcd, i);
52396 + if (ep != 0) {
52397 + dwc_ep = &ep->dwc_ep;
52398 + dwc_ep->next_frame = 0xffffffff;
52399 + }
52400 + }
52401 +#endif /* DWC_EN_ISOC */
52402 +
52403 + /* reset the HNP settings */
52404 + dwc_otg_pcd_update_otg(pcd, 1);
52405 +
52406 + /* Clear the Remote Wakeup Signalling */
52407 + dctl.b.rmtwkupsig = 1;
52408 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
52409 +
52410 + /* Set NAK for all OUT EPs */
52411 + doepctl.b.snak = 1;
52412 + for (i = 0; i <= dev_if->num_out_eps; i++) {
52413 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
52414 + }
52415 +
52416 + /* Flush the NP Tx FIFO */
52417 + dwc_otg_flush_tx_fifo(core_if, 0x10);
52418 + /* Flush the Learning Queue */
52419 + resetctl.b.intknqflsh = 1;
52420 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
52421 +
52422 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
52423 + core_if->start_predict = 0;
52424 + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
52425 + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
52426 + }
52427 + core_if->nextep_seq[0] = 0;
52428 + core_if->first_in_nextep_seq = 0;
52429 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
52430 + diepctl.b.nextep = 0;
52431 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
52432 +
52433 + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
52434 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
52435 + dcfg.b.epmscnt = 2;
52436 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
52437 +
52438 + DWC_DEBUGPL(DBG_PCDV,
52439 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
52440 + __func__, core_if->first_in_nextep_seq);
52441 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
52442 + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
52443 + }
52444 + }
52445 +
52446 + if (core_if->multiproc_int_enable) {
52447 + daintmsk.b.inep0 = 1;
52448 + daintmsk.b.outep0 = 1;
52449 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
52450 + daintmsk.d32);
52451 +
52452 + doepmsk.b.setup = 1;
52453 + doepmsk.b.xfercompl = 1;
52454 + doepmsk.b.ahberr = 1;
52455 + doepmsk.b.epdisabled = 1;
52456 +
52457 + if ((core_if->dma_desc_enable) ||
52458 + (core_if->dma_enable
52459 + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
52460 + doepmsk.b.stsphsercvd = 1;
52461 + }
52462 + if (core_if->dma_desc_enable)
52463 + doepmsk.b.bna = 1;
52464 +/*
52465 + doepmsk.b.babble = 1;
52466 + doepmsk.b.nyet = 1;
52467 +
52468 + if (core_if->dma_enable) {
52469 + doepmsk.b.nak = 1;
52470 + }
52471 +*/
52472 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
52473 + doepmsk.d32);
52474 +
52475 + diepmsk.b.xfercompl = 1;
52476 + diepmsk.b.timeout = 1;
52477 + diepmsk.b.epdisabled = 1;
52478 + diepmsk.b.ahberr = 1;
52479 + diepmsk.b.intknepmis = 1;
52480 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
52481 + diepmsk.b.intknepmis = 0;
52482 +
52483 +/* if (core_if->dma_desc_enable) {
52484 + diepmsk.b.bna = 1;
52485 + }
52486 +*/
52487 +/*
52488 + if (core_if->dma_enable) {
52489 + diepmsk.b.nak = 1;
52490 + }
52491 +*/
52492 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
52493 + diepmsk.d32);
52494 + } else {
52495 + daintmsk.b.inep0 = 1;
52496 + daintmsk.b.outep0 = 1;
52497 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
52498 + daintmsk.d32);
52499 +
52500 + doepmsk.b.setup = 1;
52501 + doepmsk.b.xfercompl = 1;
52502 + doepmsk.b.ahberr = 1;
52503 + doepmsk.b.epdisabled = 1;
52504 +
52505 + if ((core_if->dma_desc_enable) ||
52506 + (core_if->dma_enable
52507 + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
52508 + doepmsk.b.stsphsercvd = 1;
52509 + }
52510 + if (core_if->dma_desc_enable)
52511 + doepmsk.b.bna = 1;
52512 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
52513 +
52514 + diepmsk.b.xfercompl = 1;
52515 + diepmsk.b.timeout = 1;
52516 + diepmsk.b.epdisabled = 1;
52517 + diepmsk.b.ahberr = 1;
52518 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
52519 + diepmsk.b.intknepmis = 0;
52520 +/*
52521 + if (core_if->dma_desc_enable) {
52522 + diepmsk.b.bna = 1;
52523 + }
52524 +*/
52525 +
52526 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
52527 + }
52528 +
52529 + /* Reset Device Address */
52530 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
52531 + dcfg.b.devaddr = 0;
52532 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
52533 +
52534 + /* setup EP0 to receive SETUP packets */
52535 + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
52536 + ep0_out_start(core_if, pcd);
52537 +
52538 + /* Clear interrupt */
52539 + gintsts.d32 = 0;
52540 + gintsts.b.usbreset = 1;
52541 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52542 +
52543 + return 1;
52544 +}
52545 +
52546 +/**
52547 + * Get the device speed from the device status register and convert it
52548 + * to USB speed constant.
52549 + *
52550 + * @param core_if Programming view of DWC_otg controller.
52551 + */
52552 +static int get_device_speed(dwc_otg_core_if_t * core_if)
52553 +{
52554 + dsts_data_t dsts;
52555 + int speed = 0;
52556 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
52557 +
52558 + switch (dsts.b.enumspd) {
52559 + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
52560 + speed = USB_SPEED_HIGH;
52561 + break;
52562 + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
52563 + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
52564 + speed = USB_SPEED_FULL;
52565 + break;
52566 +
52567 + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
52568 + speed = USB_SPEED_LOW;
52569 + break;
52570 + }
52571 +
52572 + return speed;
52573 +}
52574 +
52575 +/**
52576 + * Read the device status register and set the device speed in the
52577 + * data structure.
52578 + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
52579 + */
52580 +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
52581 +{
52582 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52583 + gintsts_data_t gintsts;
52584 + gusbcfg_data_t gusbcfg;
52585 + dwc_otg_core_global_regs_t *global_regs =
52586 + GET_CORE_IF(pcd)->core_global_regs;
52587 + uint8_t utmi16b, utmi8b;
52588 + int speed;
52589 + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
52590 +
52591 + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
52592 + utmi16b = 6; //vahrama old value was 6;
52593 + utmi8b = 9;
52594 + } else {
52595 + utmi16b = 4;
52596 + utmi8b = 8;
52597 + }
52598 + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
52599 + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
52600 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52601 + }
52602 +
52603 +#ifdef DEBUG_EP0
52604 + print_ep0_state(pcd);
52605 +#endif
52606 +
52607 + if (pcd->ep0state == EP0_DISCONNECT) {
52608 + pcd->ep0state = EP0_IDLE;
52609 + } else if (pcd->ep0state == EP0_STALL) {
52610 + pcd->ep0state = EP0_IDLE;
52611 + }
52612 +
52613 + pcd->ep0state = EP0_IDLE;
52614 +
52615 + ep0->stopped = 0;
52616 +
52617 + speed = get_device_speed(GET_CORE_IF(pcd));
52618 + pcd->fops->connect(pcd, speed);
52619 +
52620 + /* Set USB turnaround time based on device speed and PHY interface. */
52621 + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
52622 + if (speed == USB_SPEED_HIGH) {
52623 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52624 + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
52625 + /* ULPI interface */
52626 + gusbcfg.b.usbtrdtim = 9;
52627 + }
52628 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52629 + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
52630 + /* UTMI+ interface */
52631 + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
52632 + gusbcfg.b.usbtrdtim = utmi8b;
52633 + } else if (GET_CORE_IF(pcd)->hwcfg4.
52634 + b.utmi_phy_data_width == 1) {
52635 + gusbcfg.b.usbtrdtim = utmi16b;
52636 + } else if (GET_CORE_IF(pcd)->
52637 + core_params->phy_utmi_width == 8) {
52638 + gusbcfg.b.usbtrdtim = utmi8b;
52639 + } else {
52640 + gusbcfg.b.usbtrdtim = utmi16b;
52641 + }
52642 + }
52643 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52644 + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
52645 + /* UTMI+ OR ULPI interface */
52646 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
52647 + /* ULPI interface */
52648 + gusbcfg.b.usbtrdtim = 9;
52649 + } else {
52650 + /* UTMI+ interface */
52651 + if (GET_CORE_IF(pcd)->
52652 + core_params->phy_utmi_width == 16) {
52653 + gusbcfg.b.usbtrdtim = utmi16b;
52654 + } else {
52655 + gusbcfg.b.usbtrdtim = utmi8b;
52656 + }
52657 + }
52658 + }
52659 + } else {
52660 + /* Full or low speed */
52661 + gusbcfg.b.usbtrdtim = 9;
52662 + }
52663 + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
52664 +
52665 + /* Clear interrupt */
52666 + gintsts.d32 = 0;
52667 + gintsts.b.enumdone = 1;
52668 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52669 + gintsts.d32);
52670 + return 1;
52671 +}
52672 +
52673 +/**
52674 + * This interrupt indicates that the ISO OUT Packet was dropped due to
52675 + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
52676 + * read all the data from the Rx FIFO.
52677 + */
52678 +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
52679 +{
52680 + gintmsk_data_t intr_mask = {.d32 = 0 };
52681 + gintsts_data_t gintsts;
52682 +
52683 + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
52684 + "ISOC Out Dropped");
52685 +
52686 + intr_mask.b.isooutdrop = 1;
52687 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52688 + intr_mask.d32, 0);
52689 +
52690 + /* Clear interrupt */
52691 + gintsts.d32 = 0;
52692 + gintsts.b.isooutdrop = 1;
52693 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52694 + gintsts.d32);
52695 +
52696 + return 1;
52697 +}
52698 +
52699 +/**
52700 + * This interrupt indicates the end of the portion of the micro-frame
52701 + * for periodic transactions. If there is a periodic transaction for
52702 + * the next frame, load the packets into the EP periodic Tx FIFO.
52703 + */
52704 +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
52705 +{
52706 + gintmsk_data_t intr_mask = {.d32 = 0 };
52707 + gintsts_data_t gintsts;
52708 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
52709 +
52710 + intr_mask.b.eopframe = 1;
52711 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52712 + intr_mask.d32, 0);
52713 +
52714 + /* Clear interrupt */
52715 + gintsts.d32 = 0;
52716 + gintsts.b.eopframe = 1;
52717 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52718 + gintsts.d32);
52719 +
52720 + return 1;
52721 +}
52722 +
52723 +/**
52724 + * This interrupt indicates that EP of the packet on the top of the
52725 + * non-periodic Tx FIFO does not match EP of the IN Token received.
52726 + *
52727 + * The "Device IN Token Queue" Registers are read to determine the
52728 + * order the IN Tokens have been received. The non-periodic Tx FIFO
52729 + * is flushed, so it can be reloaded in the order seen in the IN Token
52730 + * Queue.
52731 + */
52732 +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
52733 +{
52734 + gintsts_data_t gintsts;
52735 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52736 + dctl_data_t dctl;
52737 + gintmsk_data_t intr_mask = {.d32 = 0 };
52738 +
52739 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
52740 + core_if->start_predict = 1;
52741 +
52742 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
52743 +
52744 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
52745 + if (!gintsts.b.ginnakeff) {
52746 + /* Disable EP Mismatch interrupt */
52747 + intr_mask.d32 = 0;
52748 + intr_mask.b.epmismatch = 1;
52749 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
52750 + /* Enable the Global IN NAK Effective Interrupt */
52751 + intr_mask.d32 = 0;
52752 + intr_mask.b.ginnakeff = 1;
52753 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
52754 + /* Set the global non-periodic IN NAK handshake */
52755 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
52756 + dctl.b.sgnpinnak = 1;
52757 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
52758 + } else {
52759 + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
52760 + }
52761 + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
52762 + * handler after Global IN NAK Effective interrupt will be asserted */
52763 + }
52764 + /* Clear interrupt */
52765 + gintsts.d32 = 0;
52766 + gintsts.b.epmismatch = 1;
52767 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52768 +
52769 + return 1;
52770 +}
52771 +
52772 +/**
52773 + * This interrupt is valid only in DMA mode. This interrupt indicates that the
52774 + * core has stopped fetching data for IN endpoints due to the unavailability of
52775 + * TxFIFO space or Request Queue space. This interrupt is used by the
52776 + * application for an endpoint mismatch algorithm.
52777 + *
52778 + * @param pcd The PCD
52779 + */
52780 +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
52781 +{
52782 + gintsts_data_t gintsts;
52783 + gintmsk_data_t gintmsk_data;
52784 + dctl_data_t dctl;
52785 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52786 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
52787 +
52788 + /* Clear the global non-periodic IN NAK handshake */
52789 + dctl.d32 = 0;
52790 + dctl.b.cgnpinnak = 1;
52791 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
52792 +
52793 + /* Mask GINTSTS.FETSUSP interrupt */
52794 + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
52795 + gintmsk_data.b.fetsusp = 0;
52796 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
52797 +
52798 + /* Clear interrupt */
52799 + gintsts.d32 = 0;
52800 + gintsts.b.fetsusp = 1;
52801 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52802 +
52803 + return 1;
52804 +}
52805 +/**
52806 + * This funcion stalls EP0.
52807 + */
52808 +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
52809 +{
52810 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52811 + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
52812 + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
52813 + ctrl->bmRequestType, ctrl->bRequest, err_val);
52814 +
52815 + ep0->dwc_ep.is_in = 1;
52816 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
52817 + pcd->ep0.stopped = 1;
52818 + pcd->ep0state = EP0_IDLE;
52819 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52820 +}
52821 +
52822 +/**
52823 + * This functions delegates the setup command to the gadget driver.
52824 + */
52825 +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
52826 + usb_device_request_t * ctrl)
52827 +{
52828 + int ret = 0;
52829 + DWC_SPINUNLOCK(pcd->lock);
52830 + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
52831 + DWC_SPINLOCK(pcd->lock);
52832 + if (ret < 0) {
52833 + ep0_do_stall(pcd, ret);
52834 + }
52835 +
52836 + /** @todo This is a g_file_storage gadget driver specific
52837 + * workaround: a DELAYED_STATUS result from the fsg_setup
52838 + * routine will result in the gadget queueing a EP0 IN status
52839 + * phase for a two-stage control transfer. Exactly the same as
52840 + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
52841 + * specific request. Need a generic way to know when the gadget
52842 + * driver will queue the status phase. Can we assume when we
52843 + * call the gadget driver setup() function that it will always
52844 + * queue and require the following flag? Need to look into
52845 + * this.
52846 + */
52847 +
52848 + if (ret == 256 + 999) {
52849 + pcd->request_config = 1;
52850 + }
52851 +}
52852 +
52853 +#ifdef DWC_UTE_CFI
52854 +/**
52855 + * This functions delegates the CFI setup commands to the gadget driver.
52856 + * This function will return a negative value to indicate a failure.
52857 + */
52858 +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
52859 + struct cfi_usb_ctrlrequest *ctrl_req)
52860 +{
52861 + int ret = 0;
52862 +
52863 + if (pcd->fops && pcd->fops->cfi_setup) {
52864 + DWC_SPINUNLOCK(pcd->lock);
52865 + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
52866 + DWC_SPINLOCK(pcd->lock);
52867 + if (ret < 0) {
52868 + ep0_do_stall(pcd, ret);
52869 + return ret;
52870 + }
52871 + }
52872 +
52873 + return ret;
52874 +}
52875 +#endif
52876 +
52877 +/**
52878 + * This function starts the Zero-Length Packet for the IN status phase
52879 + * of a 2 stage control transfer.
52880 + */
52881 +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
52882 +{
52883 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52884 + if (pcd->ep0state == EP0_STALL) {
52885 + return;
52886 + }
52887 +
52888 + pcd->ep0state = EP0_IN_STATUS_PHASE;
52889 +
52890 + /* Prepare for more SETUP Packets */
52891 + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
52892 + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
52893 + && (pcd->core_if->dma_desc_enable)
52894 + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
52895 + DWC_DEBUGPL(DBG_PCDV,
52896 + "Data terminated wait next packet in out_desc_addr\n");
52897 + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
52898 + pcd->data_terminated = 1;
52899 + }
52900 + ep0->dwc_ep.xfer_len = 0;
52901 + ep0->dwc_ep.xfer_count = 0;
52902 + ep0->dwc_ep.is_in = 1;
52903 + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
52904 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52905 +
52906 + /* Prepare for more SETUP Packets */
52907 + //ep0_out_start(GET_CORE_IF(pcd), pcd);
52908 +}
52909 +
52910 +/**
52911 + * This function starts the Zero-Length Packet for the OUT status phase
52912 + * of a 2 stage control transfer.
52913 + */
52914 +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
52915 +{
52916 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52917 + if (pcd->ep0state == EP0_STALL) {
52918 + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
52919 + return;
52920 + }
52921 + pcd->ep0state = EP0_OUT_STATUS_PHASE;
52922 +
52923 + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
52924 + ep0->dwc_ep.xfer_len = 0;
52925 + ep0->dwc_ep.xfer_count = 0;
52926 + ep0->dwc_ep.is_in = 0;
52927 + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
52928 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52929 +
52930 + /* Prepare for more SETUP Packets */
52931 + if (GET_CORE_IF(pcd)->dma_enable == 0) {
52932 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52933 + }
52934 +}
52935 +
52936 +/**
52937 + * Clear the EP halt (STALL) and if pending requests start the
52938 + * transfer.
52939 + */
52940 +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
52941 +{
52942 + if (ep->dwc_ep.stall_clear_flag == 0)
52943 + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
52944 +
52945 + /* Reactive the EP */
52946 + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
52947 + if (ep->stopped) {
52948 + ep->stopped = 0;
52949 + /* If there is a request in the EP queue start it */
52950 +
52951 + /** @todo FIXME: this causes an EP mismatch in DMA mode.
52952 + * epmismatch not yet implemented. */
52953 +
52954 + /*
52955 + * Above fixme is solved by implmenting a tasklet to call the
52956 + * start_next_request(), outside of interrupt context at some
52957 + * time after the current time, after a clear-halt setup packet.
52958 + * Still need to implement ep mismatch in the future if a gadget
52959 + * ever uses more than one endpoint at once
52960 + */
52961 + ep->queue_sof = 1;
52962 + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
52963 + }
52964 + /* Start Control Status Phase */
52965 + do_setup_in_status_phase(pcd);
52966 +}
52967 +
52968 +/**
52969 + * This function is called when the SET_FEATURE TEST_MODE Setup packet
52970 + * is sent from the host. The Device Control register is written with
52971 + * the Test Mode bits set to the specified Test Mode. This is done as
52972 + * a tasklet so that the "Status" phase of the control transfer
52973 + * completes before transmitting the TEST packets.
52974 + *
52975 + * @todo This has not been tested since the tasklet struct was put
52976 + * into the PCD struct!
52977 + *
52978 + */
52979 +void do_test_mode(void *data)
52980 +{
52981 + dctl_data_t dctl;
52982 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
52983 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52984 + int test_mode = pcd->test_mode;
52985 +
52986 +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
52987 +
52988 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
52989 + switch (test_mode) {
52990 + case 1: // TEST_J
52991 + dctl.b.tstctl = 1;
52992 + break;
52993 +
52994 + case 2: // TEST_K
52995 + dctl.b.tstctl = 2;
52996 + break;
52997 +
52998 + case 3: // TEST_SE0_NAK
52999 + dctl.b.tstctl = 3;
53000 + break;
53001 +
53002 + case 4: // TEST_PACKET
53003 + dctl.b.tstctl = 4;
53004 + break;
53005 +
53006 + case 5: // TEST_FORCE_ENABLE
53007 + dctl.b.tstctl = 5;
53008 + break;
53009 + }
53010 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
53011 +}
53012 +
53013 +/**
53014 + * This function process the GET_STATUS Setup Commands.
53015 + */
53016 +static inline void do_get_status(dwc_otg_pcd_t * pcd)
53017 +{
53018 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53019 + dwc_otg_pcd_ep_t *ep;
53020 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
53021 + uint16_t *status = pcd->status_buf;
53022 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
53023 +
53024 +#ifdef DEBUG_EP0
53025 + DWC_DEBUGPL(DBG_PCD,
53026 + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
53027 + ctrl.bmRequestType, ctrl.bRequest,
53028 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53029 + UGETW(ctrl.wLength));
53030 +#endif
53031 +
53032 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
53033 + case UT_DEVICE:
53034 + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
53035 + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
53036 + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
53037 + DWC_PRINTF("OTG CAP - %d, %d\n",
53038 + core_if->core_params->otg_cap,
53039 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
53040 + if (core_if->otg_ver == 1
53041 + && core_if->core_params->otg_cap ==
53042 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
53043 + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
53044 + *otgsts = (core_if->otg_sts & 0x1);
53045 + pcd->ep0_pending = 1;
53046 + ep0->dwc_ep.start_xfer_buff =
53047 + (uint8_t *) otgsts;
53048 + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
53049 + ep0->dwc_ep.dma_addr =
53050 + pcd->status_buf_dma_handle;
53051 + ep0->dwc_ep.xfer_len = 1;
53052 + ep0->dwc_ep.xfer_count = 0;
53053 + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
53054 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
53055 + &ep0->dwc_ep);
53056 + return;
53057 + } else {
53058 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53059 + return;
53060 + }
53061 + break;
53062 + } else {
53063 + *status = 0x1; /* Self powered */
53064 + *status |= pcd->remote_wakeup_enable << 1;
53065 + break;
53066 + }
53067 + case UT_INTERFACE:
53068 + *status = 0;
53069 + break;
53070 +
53071 + case UT_ENDPOINT:
53072 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
53073 + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
53074 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53075 + return;
53076 + }
53077 + /** @todo check for EP stall */
53078 + *status = ep->stopped;
53079 + break;
53080 + }
53081 + pcd->ep0_pending = 1;
53082 + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
53083 + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
53084 + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
53085 + ep0->dwc_ep.xfer_len = 2;
53086 + ep0->dwc_ep.xfer_count = 0;
53087 + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
53088 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
53089 +}
53090 +
53091 +/**
53092 + * This function process the SET_FEATURE Setup Commands.
53093 + */
53094 +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
53095 +{
53096 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
53097 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
53098 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53099 + dwc_otg_pcd_ep_t *ep = 0;
53100 + int32_t otg_cap_param = core_if->core_params->otg_cap;
53101 + gotgctl_data_t gotgctl = {.d32 = 0 };
53102 +
53103 + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
53104 + ctrl.bmRequestType, ctrl.bRequest,
53105 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53106 + UGETW(ctrl.wLength));
53107 + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
53108 +
53109 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
53110 + case UT_DEVICE:
53111 + switch (UGETW(ctrl.wValue)) {
53112 + case UF_DEVICE_REMOTE_WAKEUP:
53113 + pcd->remote_wakeup_enable = 1;
53114 + break;
53115 +
53116 + case UF_TEST_MODE:
53117 + /* Setup the Test Mode tasklet to do the Test
53118 + * Packet generation after the SETUP Status
53119 + * phase has completed. */
53120 +
53121 + /** @todo This has not been tested since the
53122 + * tasklet struct was put into the PCD
53123 + * struct! */
53124 + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
53125 + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
53126 + break;
53127 +
53128 + case UF_DEVICE_B_HNP_ENABLE:
53129 + DWC_DEBUGPL(DBG_PCDV,
53130 + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
53131 +
53132 + /* dev may initiate HNP */
53133 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
53134 + pcd->b_hnp_enable = 1;
53135 + dwc_otg_pcd_update_otg(pcd, 0);
53136 + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
53137 + /**@todo Is the gotgctl.devhnpen cleared
53138 + * by a USB Reset? */
53139 + gotgctl.b.devhnpen = 1;
53140 + gotgctl.b.hnpreq = 1;
53141 + DWC_WRITE_REG32(&global_regs->gotgctl,
53142 + gotgctl.d32);
53143 + } else {
53144 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53145 + return;
53146 + }
53147 + break;
53148 +
53149 + case UF_DEVICE_A_HNP_SUPPORT:
53150 + /* RH port supports HNP */
53151 + DWC_DEBUGPL(DBG_PCDV,
53152 + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
53153 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
53154 + pcd->a_hnp_support = 1;
53155 + dwc_otg_pcd_update_otg(pcd, 0);
53156 + } else {
53157 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53158 + return;
53159 + }
53160 + break;
53161 +
53162 + case UF_DEVICE_A_ALT_HNP_SUPPORT:
53163 + /* other RH port does */
53164 + DWC_DEBUGPL(DBG_PCDV,
53165 + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
53166 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
53167 + pcd->a_alt_hnp_support = 1;
53168 + dwc_otg_pcd_update_otg(pcd, 0);
53169 + } else {
53170 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53171 + return;
53172 + }
53173 + break;
53174 +
53175 + default:
53176 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53177 + return;
53178 +
53179 + }
53180 + do_setup_in_status_phase(pcd);
53181 + break;
53182 +
53183 + case UT_INTERFACE:
53184 + do_gadget_setup(pcd, &ctrl);
53185 + break;
53186 +
53187 + case UT_ENDPOINT:
53188 + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
53189 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
53190 + if (ep == 0) {
53191 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53192 + return;
53193 + }
53194 + ep->stopped = 1;
53195 + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
53196 + }
53197 + do_setup_in_status_phase(pcd);
53198 + break;
53199 + }
53200 +}
53201 +
53202 +/**
53203 + * This function process the CLEAR_FEATURE Setup Commands.
53204 + */
53205 +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
53206 +{
53207 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53208 + dwc_otg_pcd_ep_t *ep = 0;
53209 +
53210 + DWC_DEBUGPL(DBG_PCD,
53211 + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
53212 + ctrl.bmRequestType, ctrl.bRequest,
53213 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53214 + UGETW(ctrl.wLength));
53215 +
53216 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
53217 + case UT_DEVICE:
53218 + switch (UGETW(ctrl.wValue)) {
53219 + case UF_DEVICE_REMOTE_WAKEUP:
53220 + pcd->remote_wakeup_enable = 0;
53221 + break;
53222 +
53223 + case UF_TEST_MODE:
53224 + /** @todo Add CLEAR_FEATURE for TEST modes. */
53225 + break;
53226 +
53227 + default:
53228 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53229 + return;
53230 + }
53231 + do_setup_in_status_phase(pcd);
53232 + break;
53233 +
53234 + case UT_ENDPOINT:
53235 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
53236 + if (ep == 0) {
53237 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53238 + return;
53239 + }
53240 +
53241 + pcd_clear_halt(pcd, ep);
53242 +
53243 + break;
53244 + }
53245 +}
53246 +
53247 +/**
53248 + * This function process the SET_ADDRESS Setup Commands.
53249 + */
53250 +static inline void do_set_address(dwc_otg_pcd_t * pcd)
53251 +{
53252 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
53253 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53254 +
53255 + if (ctrl.bmRequestType == UT_DEVICE) {
53256 + dcfg_data_t dcfg = {.d32 = 0 };
53257 +
53258 +#ifdef DEBUG_EP0
53259 +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
53260 +#endif
53261 + dcfg.b.devaddr = UGETW(ctrl.wValue);
53262 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
53263 + do_setup_in_status_phase(pcd);
53264 + }
53265 +}
53266 +
53267 +/**
53268 + * This function processes SETUP commands. In Linux, the USB Command
53269 + * processing is done in two places - the first being the PCD and the
53270 + * second in the Gadget Driver (for example, the File-Backed Storage
53271 + * Gadget Driver).
53272 + *
53273 + * <table>
53274 + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
53275 + *
53276 + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
53277 + * defined in chapter 9 of the USB 2.0 Specification chapter 9
53278 + * </td></tr>
53279 + *
53280 + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
53281 + * requests are the ENDPOINT_HALT feature is procesed, all others the
53282 + * interface requests are ignored.</td></tr>
53283 + *
53284 + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
53285 + * requests are processed by the PCD. Interface requests are passed
53286 + * to the Gadget Driver.</td></tr>
53287 + *
53288 + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
53289 + * with device address received </td></tr>
53290 + *
53291 + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
53292 + * requested descriptor</td></tr>
53293 + *
53294 + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
53295 + * not implemented by any of the existing Gadget Drivers.</td></tr>
53296 + *
53297 + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
53298 + * all EPs and enable EPs for new configuration.</td></tr>
53299 + *
53300 + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
53301 + * the current configuration</td></tr>
53302 + *
53303 + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
53304 + * EPs and enable EPs for new configuration.</td></tr>
53305 + *
53306 + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
53307 + * current interface.</td></tr>
53308 + *
53309 + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
53310 + * message.</td></tr>
53311 + * </table>
53312 + *
53313 + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
53314 + * processed by pcd_setup. Calling the Function Driver's setup function from
53315 + * pcd_setup processes the gadget SETUP commands.
53316 + */
53317 +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
53318 +{
53319 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
53320 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53321 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53322 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
53323 +
53324 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
53325 +
53326 +#ifdef DWC_UTE_CFI
53327 + int retval = 0;
53328 + struct cfi_usb_ctrlrequest cfi_req;
53329 +#endif
53330 +
53331 + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
53332 +
53333 + /** In BDMA more then 1 setup packet is not supported till 3.00a */
53334 + if (core_if->dma_enable && core_if->dma_desc_enable == 0
53335 + && (doeptsize0.b.supcnt < 2)
53336 + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
53337 + DWC_ERROR
53338 + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
53339 + }
53340 + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
53341 + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
53342 + ctrl =
53343 + (pcd->setup_pkt +
53344 + (3 - doeptsize0.b.supcnt - 1 +
53345 + ep0->dwc_ep.stp_rollover))->req;
53346 + }
53347 +#ifdef DEBUG_EP0
53348 + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
53349 + ctrl.bmRequestType, ctrl.bRequest,
53350 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53351 + UGETW(ctrl.wLength));
53352 +#endif
53353 +
53354 + /* Clean up the request queue */
53355 + dwc_otg_request_nuke(ep0);
53356 + ep0->stopped = 0;
53357 +
53358 + if (ctrl.bmRequestType & UE_DIR_IN) {
53359 + ep0->dwc_ep.is_in = 1;
53360 + pcd->ep0state = EP0_IN_DATA_PHASE;
53361 + } else {
53362 + ep0->dwc_ep.is_in = 0;
53363 + pcd->ep0state = EP0_OUT_DATA_PHASE;
53364 + }
53365 +
53366 + if (UGETW(ctrl.wLength) == 0) {
53367 + ep0->dwc_ep.is_in = 1;
53368 + pcd->ep0state = EP0_IN_STATUS_PHASE;
53369 + }
53370 +
53371 + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
53372 +
53373 +#ifdef DWC_UTE_CFI
53374 + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
53375 +
53376 + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
53377 + ctrl.bRequestType, ctrl.bRequest);
53378 + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
53379 + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
53380 + retval = cfi_setup(pcd, &cfi_req);
53381 + if (retval < 0) {
53382 + ep0_do_stall(pcd, retval);
53383 + pcd->ep0_pending = 0;
53384 + return;
53385 + }
53386 +
53387 + /* if need gadget setup then call it and check the retval */
53388 + if (pcd->cfi->need_gadget_att) {
53389 + retval =
53390 + cfi_gadget_setup(pcd,
53391 + &pcd->
53392 + cfi->ctrl_req);
53393 + if (retval < 0) {
53394 + pcd->ep0_pending = 0;
53395 + return;
53396 + }
53397 + }
53398 +
53399 + if (pcd->cfi->need_status_in_complete) {
53400 + do_setup_in_status_phase(pcd);
53401 + }
53402 + return;
53403 + }
53404 + }
53405 +#endif
53406 +
53407 + /* handle non-standard (class/vendor) requests in the gadget driver */
53408 + do_gadget_setup(pcd, &ctrl);
53409 + return;
53410 + }
53411 +
53412 + /** @todo NGS: Handle bad setup packet? */
53413 +
53414 +///////////////////////////////////////////
53415 +//// --- Standard Request handling --- ////
53416 +
53417 + switch (ctrl.bRequest) {
53418 + case UR_GET_STATUS:
53419 + do_get_status(pcd);
53420 + break;
53421 +
53422 + case UR_CLEAR_FEATURE:
53423 + do_clear_feature(pcd);
53424 + break;
53425 +
53426 + case UR_SET_FEATURE:
53427 + do_set_feature(pcd);
53428 + break;
53429 +
53430 + case UR_SET_ADDRESS:
53431 + do_set_address(pcd);
53432 + break;
53433 +
53434 + case UR_SET_INTERFACE:
53435 + case UR_SET_CONFIG:
53436 +// _pcd->request_config = 1; /* Configuration changed */
53437 + do_gadget_setup(pcd, &ctrl);
53438 + break;
53439 +
53440 + case UR_SYNCH_FRAME:
53441 + do_gadget_setup(pcd, &ctrl);
53442 + break;
53443 +
53444 + default:
53445 + /* Call the Gadget Driver's setup functions */
53446 + do_gadget_setup(pcd, &ctrl);
53447 + break;
53448 + }
53449 +}
53450 +
53451 +/**
53452 + * This function completes the ep0 control transfer.
53453 + */
53454 +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
53455 +{
53456 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
53457 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53458 + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
53459 + dev_if->in_ep_regs[ep->dwc_ep.num];
53460 +#ifdef DEBUG_EP0
53461 + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
53462 + dev_if->out_ep_regs[ep->dwc_ep.num];
53463 +#endif
53464 + deptsiz0_data_t deptsiz;
53465 + dev_dma_desc_sts_t desc_sts;
53466 + dwc_otg_pcd_request_t *req;
53467 + int is_last = 0;
53468 + dwc_otg_pcd_t *pcd = ep->pcd;
53469 +
53470 +#ifdef DWC_UTE_CFI
53471 + struct cfi_usb_ctrlrequest *ctrlreq;
53472 + int retval = -DWC_E_NOT_SUPPORTED;
53473 +#endif
53474 +
53475 + desc_sts.b.bytes = 0;
53476 +
53477 + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53478 + if (ep->dwc_ep.is_in) {
53479 +#ifdef DEBUG_EP0
53480 + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
53481 +#endif
53482 + do_setup_out_status_phase(pcd);
53483 + } else {
53484 +#ifdef DEBUG_EP0
53485 + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
53486 +#endif
53487 +
53488 +#ifdef DWC_UTE_CFI
53489 + ctrlreq = &pcd->cfi->ctrl_req;
53490 +
53491 + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
53492 + if (ctrlreq->bRequest > 0xB0
53493 + && ctrlreq->bRequest < 0xBF) {
53494 +
53495 + /* Return if the PCD failed to handle the request */
53496 + if ((retval =
53497 + pcd->cfi->ops.
53498 + ctrl_write_complete(pcd->cfi,
53499 + pcd)) < 0) {
53500 + CFI_INFO
53501 + ("ERROR setting a new value in the PCD(%d)\n",
53502 + retval);
53503 + ep0_do_stall(pcd, retval);
53504 + pcd->ep0_pending = 0;
53505 + return 0;
53506 + }
53507 +
53508 + /* If the gadget needs to be notified on the request */
53509 + if (pcd->cfi->need_gadget_att == 1) {
53510 + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
53511 + retval =
53512 + cfi_gadget_setup(pcd,
53513 + &pcd->cfi->
53514 + ctrl_req);
53515 +
53516 + /* Return from the function if the gadget failed to process
53517 + * the request properly - this should never happen !!!
53518 + */
53519 + if (retval < 0) {
53520 + CFI_INFO
53521 + ("ERROR setting a new value in the gadget(%d)\n",
53522 + retval);
53523 + pcd->ep0_pending = 0;
53524 + return 0;
53525 + }
53526 + }
53527 +
53528 + CFI_INFO("%s: RETVAL=%d\n", __func__,
53529 + retval);
53530 + /* If we hit here then the PCD and the gadget has properly
53531 + * handled the request - so send the ZLP IN to the host.
53532 + */
53533 + /* @todo: MAS - decide whether we need to start the setup
53534 + * stage based on the need_setup value of the cfi object
53535 + */
53536 + do_setup_in_status_phase(pcd);
53537 + pcd->ep0_pending = 0;
53538 + return 1;
53539 + }
53540 + }
53541 +#endif
53542 +
53543 + do_setup_in_status_phase(pcd);
53544 + }
53545 + pcd->ep0_pending = 0;
53546 + return 1;
53547 + }
53548 +
53549 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53550 + return 0;
53551 + }
53552 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
53553 +
53554 + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
53555 + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
53556 + is_last = 1;
53557 + } else if (ep->dwc_ep.is_in) {
53558 + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
53559 + if (core_if->dma_desc_enable != 0)
53560 + desc_sts = dev_if->in_desc_addr->status;
53561 +#ifdef DEBUG_EP0
53562 + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
53563 + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
53564 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53565 +#endif
53566 +
53567 + if (((core_if->dma_desc_enable == 0)
53568 + && (deptsiz.b.xfersize == 0))
53569 + || ((core_if->dma_desc_enable != 0)
53570 + && (desc_sts.b.bytes == 0))) {
53571 + req->actual = ep->dwc_ep.xfer_count;
53572 + /* Is a Zero Len Packet needed? */
53573 + if (req->sent_zlp) {
53574 +#ifdef DEBUG_EP0
53575 + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
53576 +#endif
53577 + req->sent_zlp = 0;
53578 + }
53579 + do_setup_out_status_phase(pcd);
53580 + }
53581 + } else {
53582 + /* ep0-OUT */
53583 +#ifdef DEBUG_EP0
53584 + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
53585 + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
53586 + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
53587 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53588 +#endif
53589 + req->actual = ep->dwc_ep.xfer_count;
53590 +
53591 + /* Is a Zero Len Packet needed? */
53592 + if (req->sent_zlp) {
53593 +#ifdef DEBUG_EP0
53594 + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
53595 +#endif
53596 + req->sent_zlp = 0;
53597 + }
53598 + /* For older cores do setup in status phase in Slave/BDMA modes,
53599 + * starting from 3.00 do that only in slave, and for DMA modes
53600 + * just re-enable ep 0 OUT here*/
53601 + if (core_if->dma_enable == 0
53602 + || (core_if->dma_desc_enable == 0
53603 + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
53604 + do_setup_in_status_phase(pcd);
53605 + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
53606 + DWC_DEBUGPL(DBG_PCDV,
53607 + "Enable out ep before in status phase\n");
53608 + ep0_out_start(core_if, pcd);
53609 + }
53610 + }
53611 +
53612 + /* Complete the request */
53613 + if (is_last) {
53614 + dwc_otg_request_done(ep, req, 0);
53615 + ep->dwc_ep.start_xfer_buff = 0;
53616 + ep->dwc_ep.xfer_buff = 0;
53617 + ep->dwc_ep.xfer_len = 0;
53618 + return 1;
53619 + }
53620 + return 0;
53621 +}
53622 +
53623 +#ifdef DWC_UTE_CFI
53624 +/**
53625 + * This function calculates traverses all the CFI DMA descriptors and
53626 + * and accumulates the bytes that are left to be transfered.
53627 + *
53628 + * @return The total bytes left to transfered, or a negative value as failure
53629 + */
53630 +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
53631 +{
53632 + int32_t ret = 0;
53633 + int i;
53634 + struct dwc_otg_dma_desc *ddesc = NULL;
53635 + struct cfi_ep *cfiep;
53636 +
53637 + /* See if the pcd_ep has its respective cfi_ep mapped */
53638 + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
53639 + if (!cfiep) {
53640 + CFI_INFO("%s: Failed to find ep\n", __func__);
53641 + return -1;
53642 + }
53643 +
53644 + ddesc = ep->dwc_ep.descs;
53645 +
53646 + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
53647 +
53648 +#if defined(PRINT_CFI_DMA_DESCS)
53649 + print_desc(ddesc, ep->ep.name, i);
53650 +#endif
53651 + ret += ddesc->status.b.bytes;
53652 + ddesc++;
53653 + }
53654 +
53655 + if (ret)
53656 + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
53657 + ret);
53658 +
53659 + return ret;
53660 +}
53661 +#endif
53662 +
53663 +/**
53664 + * This function completes the request for the EP. If there are
53665 + * additional requests for the EP in the queue they will be started.
53666 + */
53667 +static void complete_ep(dwc_otg_pcd_ep_t * ep)
53668 +{
53669 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
53670 + struct device *dev = dwc_otg_pcd_to_dev(ep->pcd);
53671 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53672 + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
53673 + dev_if->in_ep_regs[ep->dwc_ep.num];
53674 + deptsiz_data_t deptsiz;
53675 + dev_dma_desc_sts_t desc_sts;
53676 + dwc_otg_pcd_request_t *req = 0;
53677 + dwc_otg_dev_dma_desc_t *dma_desc;
53678 + uint32_t byte_count = 0;
53679 + int is_last = 0;
53680 + int i;
53681 +
53682 + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
53683 + (ep->dwc_ep.is_in ? "IN" : "OUT"));
53684 +
53685 + /* Get any pending requests */
53686 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53687 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
53688 + if (!req) {
53689 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
53690 + return;
53691 + }
53692 + } else {
53693 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
53694 + return;
53695 + }
53696 +
53697 + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
53698 +
53699 + if (ep->dwc_ep.is_in) {
53700 + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
53701 +
53702 + if (core_if->dma_enable) {
53703 + if (core_if->dma_desc_enable == 0) {
53704 + if (deptsiz.b.xfersize == 0
53705 + && deptsiz.b.pktcnt == 0) {
53706 + byte_count =
53707 + ep->dwc_ep.xfer_len -
53708 + ep->dwc_ep.xfer_count;
53709 +
53710 + ep->dwc_ep.xfer_buff += byte_count;
53711 + ep->dwc_ep.dma_addr += byte_count;
53712 + ep->dwc_ep.xfer_count += byte_count;
53713 +
53714 + DWC_DEBUGPL(DBG_PCDV,
53715 + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
53716 + ep->dwc_ep.num,
53717 + (ep->dwc_ep.
53718 + is_in ? "IN" : "OUT"),
53719 + ep->dwc_ep.xfer_len,
53720 + deptsiz.b.xfersize,
53721 + deptsiz.b.pktcnt);
53722 +
53723 + if (ep->dwc_ep.xfer_len <
53724 + ep->dwc_ep.total_len) {
53725 + dwc_otg_ep_start_transfer
53726 + (core_if, &ep->dwc_ep);
53727 + } else if (ep->dwc_ep.sent_zlp) {
53728 + /*
53729 + * This fragment of code should initiate 0
53730 + * length transfer in case if it is queued
53731 + * a transfer with size divisible to EPs max
53732 + * packet size and with usb_request zero field
53733 + * is set, which means that after data is transfered,
53734 + * it is also should be transfered
53735 + * a 0 length packet at the end. For Slave and
53736 + * Buffer DMA modes in this case SW has
53737 + * to initiate 2 transfers one with transfer size,
53738 + * and the second with 0 size. For Descriptor
53739 + * DMA mode SW is able to initiate a transfer,
53740 + * which will handle all the packets including
53741 + * the last 0 length.
53742 + */
53743 + ep->dwc_ep.sent_zlp = 0;
53744 + dwc_otg_ep_start_zl_transfer
53745 + (core_if, &ep->dwc_ep);
53746 + } else {
53747 + is_last = 1;
53748 + }
53749 + } else {
53750 + if (ep->dwc_ep.type ==
53751 + DWC_OTG_EP_TYPE_ISOC) {
53752 + req->actual = 0;
53753 + dwc_otg_request_done(ep, req, 0);
53754 +
53755 + ep->dwc_ep.start_xfer_buff = 0;
53756 + ep->dwc_ep.xfer_buff = 0;
53757 + ep->dwc_ep.xfer_len = 0;
53758 +
53759 + /* If there is a request in the queue start it. */
53760 + start_next_request(ep);
53761 + } else
53762 + DWC_WARN
53763 + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
53764 + ep->dwc_ep.num,
53765 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
53766 + deptsiz.b.xfersize,
53767 + deptsiz.b.pktcnt);
53768 + }
53769 + } else {
53770 + dma_desc = ep->dwc_ep.desc_addr;
53771 + byte_count = 0;
53772 + ep->dwc_ep.sent_zlp = 0;
53773 +
53774 +#ifdef DWC_UTE_CFI
53775 + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
53776 + ep->dwc_ep.buff_mode);
53777 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53778 + int residue;
53779 +
53780 + residue = cfi_calc_desc_residue(ep);
53781 + if (residue < 0)
53782 + return;
53783 +
53784 + byte_count = residue;
53785 + } else {
53786 +#endif
53787 + for (i = 0; i < ep->dwc_ep.desc_cnt;
53788 + ++i) {
53789 + desc_sts = dma_desc->status;
53790 + byte_count += desc_sts.b.bytes;
53791 + dma_desc++;
53792 + }
53793 +#ifdef DWC_UTE_CFI
53794 + }
53795 +#endif
53796 + if (byte_count == 0) {
53797 + ep->dwc_ep.xfer_count =
53798 + ep->dwc_ep.total_len;
53799 + is_last = 1;
53800 + } else {
53801 + DWC_WARN("Incomplete transfer\n");
53802 + }
53803 + }
53804 + } else {
53805 + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
53806 + DWC_DEBUGPL(DBG_PCDV,
53807 + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
53808 + ep->dwc_ep.num,
53809 + ep->dwc_ep.is_in ? "IN" : "OUT",
53810 + ep->dwc_ep.xfer_len,
53811 + deptsiz.b.xfersize,
53812 + deptsiz.b.pktcnt);
53813 +
53814 + /* Check if the whole transfer was completed,
53815 + * if no, setup transfer for next portion of data
53816 + */
53817 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53818 + dwc_otg_ep_start_transfer(core_if,
53819 + &ep->dwc_ep);
53820 + } else if (ep->dwc_ep.sent_zlp) {
53821 + /*
53822 + * This fragment of code should initiate 0
53823 + * length trasfer in case if it is queued
53824 + * a trasfer with size divisible to EPs max
53825 + * packet size and with usb_request zero field
53826 + * is set, which means that after data is transfered,
53827 + * it is also should be transfered
53828 + * a 0 length packet at the end. For Slave and
53829 + * Buffer DMA modes in this case SW has
53830 + * to initiate 2 transfers one with transfer size,
53831 + * and the second with 0 size. For Desriptor
53832 + * DMA mode SW is able to initiate a transfer,
53833 + * which will handle all the packets including
53834 + * the last 0 legth.
53835 + */
53836 + ep->dwc_ep.sent_zlp = 0;
53837 + dwc_otg_ep_start_zl_transfer(core_if,
53838 + &ep->dwc_ep);
53839 + } else {
53840 + is_last = 1;
53841 + }
53842 + } else {
53843 + DWC_WARN
53844 + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
53845 + ep->dwc_ep.num,
53846 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
53847 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53848 + }
53849 + }
53850 + } else {
53851 + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
53852 + dev_if->out_ep_regs[ep->dwc_ep.num];
53853 + desc_sts.d32 = 0;
53854 + if (core_if->dma_enable) {
53855 + if (core_if->dma_desc_enable) {
53856 + dma_desc = ep->dwc_ep.desc_addr;
53857 + byte_count = 0;
53858 + ep->dwc_ep.sent_zlp = 0;
53859 +
53860 +#ifdef DWC_UTE_CFI
53861 + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
53862 + ep->dwc_ep.buff_mode);
53863 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53864 + int residue;
53865 + residue = cfi_calc_desc_residue(ep);
53866 + if (residue < 0)
53867 + return;
53868 + byte_count = residue;
53869 + } else {
53870 +#endif
53871 +
53872 + for (i = 0; i < ep->dwc_ep.desc_cnt;
53873 + ++i) {
53874 + desc_sts = dma_desc->status;
53875 + byte_count += desc_sts.b.bytes;
53876 + dma_desc++;
53877 + }
53878 +
53879 +#ifdef DWC_UTE_CFI
53880 + }
53881 +#endif
53882 + /* Checking for interrupt Out transfers with not
53883 + * dword aligned mps sizes
53884 + */
53885 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
53886 + (ep->dwc_ep.maxpacket%4)) {
53887 + ep->dwc_ep.xfer_count =
53888 + ep->dwc_ep.total_len - byte_count;
53889 + if ((ep->dwc_ep.xfer_len %
53890 + ep->dwc_ep.maxpacket)
53891 + && (ep->dwc_ep.xfer_len /
53892 + ep->dwc_ep.maxpacket <
53893 + MAX_DMA_DESC_CNT))
53894 + ep->dwc_ep.xfer_len -=
53895 + (ep->dwc_ep.desc_cnt -
53896 + 1) * ep->dwc_ep.maxpacket +
53897 + ep->dwc_ep.xfer_len %
53898 + ep->dwc_ep.maxpacket;
53899 + else
53900 + ep->dwc_ep.xfer_len -=
53901 + ep->dwc_ep.desc_cnt *
53902 + ep->dwc_ep.maxpacket;
53903 + if (ep->dwc_ep.xfer_len > 0) {
53904 + dwc_otg_ep_start_transfer
53905 + (core_if, &ep->dwc_ep);
53906 + } else {
53907 + is_last = 1;
53908 + }
53909 + } else {
53910 + ep->dwc_ep.xfer_count =
53911 + ep->dwc_ep.total_len - byte_count +
53912 + ((4 -
53913 + (ep->dwc_ep.
53914 + total_len & 0x3)) & 0x3);
53915 + is_last = 1;
53916 + }
53917 + } else {
53918 + deptsiz.d32 = 0;
53919 + deptsiz.d32 =
53920 + DWC_READ_REG32(&out_ep_regs->doeptsiz);
53921 +
53922 + byte_count = (ep->dwc_ep.xfer_len -
53923 + ep->dwc_ep.xfer_count -
53924 + deptsiz.b.xfersize);
53925 + ep->dwc_ep.xfer_buff += byte_count;
53926 + ep->dwc_ep.dma_addr += byte_count;
53927 + ep->dwc_ep.xfer_count += byte_count;
53928 +
53929 + /* Check if the whole transfer was completed,
53930 + * if no, setup transfer for next portion of data
53931 + */
53932 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53933 + dwc_otg_ep_start_transfer(core_if,
53934 + &ep->dwc_ep);
53935 + } else if (ep->dwc_ep.sent_zlp) {
53936 + /*
53937 + * This fragment of code should initiate 0
53938 + * length trasfer in case if it is queued
53939 + * a trasfer with size divisible to EPs max
53940 + * packet size and with usb_request zero field
53941 + * is set, which means that after data is transfered,
53942 + * it is also should be transfered
53943 + * a 0 length packet at the end. For Slave and
53944 + * Buffer DMA modes in this case SW has
53945 + * to initiate 2 transfers one with transfer size,
53946 + * and the second with 0 size. For Desriptor
53947 + * DMA mode SW is able to initiate a transfer,
53948 + * which will handle all the packets including
53949 + * the last 0 legth.
53950 + */
53951 + ep->dwc_ep.sent_zlp = 0;
53952 + dwc_otg_ep_start_zl_transfer(core_if,
53953 + &ep->dwc_ep);
53954 + } else {
53955 + is_last = 1;
53956 + }
53957 + }
53958 + } else {
53959 + /* Check if the whole transfer was completed,
53960 + * if no, setup transfer for next portion of data
53961 + */
53962 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53963 + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
53964 + } else if (ep->dwc_ep.sent_zlp) {
53965 + /*
53966 + * This fragment of code should initiate 0
53967 + * length transfer in case if it is queued
53968 + * a transfer with size divisible to EPs max
53969 + * packet size and with usb_request zero field
53970 + * is set, which means that after data is transfered,
53971 + * it is also should be transfered
53972 + * a 0 length packet at the end. For Slave and
53973 + * Buffer DMA modes in this case SW has
53974 + * to initiate 2 transfers one with transfer size,
53975 + * and the second with 0 size. For Descriptor
53976 + * DMA mode SW is able to initiate a transfer,
53977 + * which will handle all the packets including
53978 + * the last 0 length.
53979 + */
53980 + ep->dwc_ep.sent_zlp = 0;
53981 + dwc_otg_ep_start_zl_transfer(core_if,
53982 + &ep->dwc_ep);
53983 + } else {
53984 + is_last = 1;
53985 + }
53986 + }
53987 +
53988 + DWC_DEBUGPL(DBG_PCDV,
53989 + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
53990 + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
53991 + ep->dwc_ep.is_in ? "IN" : "OUT",
53992 + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
53993 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53994 + }
53995 +
53996 + /* Complete the request */
53997 + if (is_last) {
53998 +#ifdef DWC_UTE_CFI
53999 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
54000 + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
54001 + } else {
54002 +#endif
54003 + req->actual = ep->dwc_ep.xfer_count;
54004 +#ifdef DWC_UTE_CFI
54005 + }
54006 +#endif
54007 + if (req->dw_align_buf) {
54008 + if (!ep->dwc_ep.is_in) {
54009 + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
54010 + }
54011 + DWC_DMA_FREE(dev, req->length, req->dw_align_buf,
54012 + req->dw_align_buf_dma);
54013 + }
54014 +
54015 + dwc_otg_request_done(ep, req, 0);
54016 +
54017 + ep->dwc_ep.start_xfer_buff = 0;
54018 + ep->dwc_ep.xfer_buff = 0;
54019 + ep->dwc_ep.xfer_len = 0;
54020 +
54021 + /* If there is a request in the queue start it. */
54022 + start_next_request(ep);
54023 + }
54024 +}
54025 +
54026 +#ifdef DWC_EN_ISOC
54027 +
54028 +/**
54029 + * This function BNA interrupt for Isochronous EPs
54030 + *
54031 + */
54032 +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
54033 +{
54034 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54035 + volatile uint32_t *addr;
54036 + depctl_data_t depctl = {.d32 = 0 };
54037 + dwc_otg_pcd_t *pcd = ep->pcd;
54038 + dwc_otg_dev_dma_desc_t *dma_desc;
54039 + int i;
54040 +
54041 + dma_desc =
54042 + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
54043 +
54044 + if (dwc_ep->is_in) {
54045 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54046 + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
54047 + sts.d32 = dma_desc->status.d32;
54048 + sts.b_iso_in.bs = BS_HOST_READY;
54049 + dma_desc->status.d32 = sts.d32;
54050 + }
54051 + } else {
54052 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54053 + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
54054 + sts.d32 = dma_desc->status.d32;
54055 + sts.b_iso_out.bs = BS_HOST_READY;
54056 + dma_desc->status.d32 = sts.d32;
54057 + }
54058 + }
54059 +
54060 + if (dwc_ep->is_in == 0) {
54061 + addr =
54062 + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
54063 + num]->doepctl;
54064 + } else {
54065 + addr =
54066 + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
54067 + }
54068 + depctl.b.epena = 1;
54069 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
54070 +}
54071 +
54072 +/**
54073 + * This function sets latest iso packet information(non-PTI mode)
54074 + *
54075 + * @param core_if Programming view of DWC_otg controller.
54076 + * @param ep The EP to start the transfer on.
54077 + *
54078 + */
54079 +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
54080 +{
54081 + deptsiz_data_t deptsiz = {.d32 = 0 };
54082 + dma_addr_t dma_addr;
54083 + uint32_t offset;
54084 +
54085 + if (ep->proc_buf_num)
54086 + dma_addr = ep->dma_addr1;
54087 + else
54088 + dma_addr = ep->dma_addr0;
54089 +
54090 + if (ep->is_in) {
54091 + deptsiz.d32 =
54092 + DWC_READ_REG32(&core_if->dev_if->
54093 + in_ep_regs[ep->num]->dieptsiz);
54094 + offset = ep->data_per_frame;
54095 + } else {
54096 + deptsiz.d32 =
54097 + DWC_READ_REG32(&core_if->dev_if->
54098 + out_ep_regs[ep->num]->doeptsiz);
54099 + offset =
54100 + ep->data_per_frame +
54101 + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
54102 + }
54103 +
54104 + if (!deptsiz.b.xfersize) {
54105 + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
54106 + ep->pkt_info[ep->cur_pkt].offset =
54107 + ep->cur_pkt_dma_addr - dma_addr;
54108 + ep->pkt_info[ep->cur_pkt].status = 0;
54109 + } else {
54110 + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
54111 + ep->pkt_info[ep->cur_pkt].offset =
54112 + ep->cur_pkt_dma_addr - dma_addr;
54113 + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
54114 + }
54115 + ep->cur_pkt_addr += offset;
54116 + ep->cur_pkt_dma_addr += offset;
54117 + ep->cur_pkt++;
54118 +}
54119 +
54120 +/**
54121 + * This function sets latest iso packet information(DDMA mode)
54122 + *
54123 + * @param core_if Programming view of DWC_otg controller.
54124 + * @param dwc_ep The EP to start the transfer on.
54125 + *
54126 + */
54127 +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
54128 + dwc_ep_t * dwc_ep)
54129 +{
54130 + dwc_otg_dev_dma_desc_t *dma_desc;
54131 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54132 + iso_pkt_info_t *iso_packet;
54133 + uint32_t data_per_desc;
54134 + uint32_t offset;
54135 + int i, j;
54136 +
54137 + iso_packet = dwc_ep->pkt_info;
54138 +
54139 + /** Reinit closed DMA Descriptors*/
54140 + /** ISO OUT EP */
54141 + if (dwc_ep->is_in == 0) {
54142 + dma_desc =
54143 + dwc_ep->iso_desc_addr +
54144 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54145 + offset = 0;
54146 +
54147 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
54148 + i += dwc_ep->pkt_per_frm) {
54149 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
54150 + data_per_desc =
54151 + ((j + 1) * dwc_ep->maxpacket >
54152 + dwc_ep->
54153 + data_per_frame) ? dwc_ep->data_per_frame -
54154 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54155 + data_per_desc +=
54156 + (data_per_desc % 4) ? (4 -
54157 + data_per_desc %
54158 + 4) : 0;
54159 +
54160 + sts.d32 = dma_desc->status.d32;
54161 +
54162 + /* Write status in iso_packet_decsriptor */
54163 + iso_packet->status =
54164 + sts.b_iso_out.rxsts +
54165 + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54166 + if (iso_packet->status) {
54167 + iso_packet->status = -DWC_E_NO_DATA;
54168 + }
54169 +
54170 + /* Received data length */
54171 + if (!sts.b_iso_out.rxbytes) {
54172 + iso_packet->length =
54173 + data_per_desc -
54174 + sts.b_iso_out.rxbytes;
54175 + } else {
54176 + iso_packet->length =
54177 + data_per_desc -
54178 + sts.b_iso_out.rxbytes + (4 -
54179 + dwc_ep->data_per_frame
54180 + % 4);
54181 + }
54182 +
54183 + iso_packet->offset = offset;
54184 +
54185 + offset += data_per_desc;
54186 + dma_desc++;
54187 + iso_packet++;
54188 + }
54189 + }
54190 +
54191 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
54192 + data_per_desc =
54193 + ((j + 1) * dwc_ep->maxpacket >
54194 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54195 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54196 + data_per_desc +=
54197 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54198 +
54199 + sts.d32 = dma_desc->status.d32;
54200 +
54201 + /* Write status in iso_packet_decsriptor */
54202 + iso_packet->status =
54203 + sts.b_iso_out.rxsts +
54204 + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54205 + if (iso_packet->status) {
54206 + iso_packet->status = -DWC_E_NO_DATA;
54207 + }
54208 +
54209 + /* Received data length */
54210 + iso_packet->length =
54211 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
54212 +
54213 + iso_packet->offset = offset;
54214 +
54215 + offset += data_per_desc;
54216 + iso_packet++;
54217 + dma_desc++;
54218 + }
54219 +
54220 + sts.d32 = dma_desc->status.d32;
54221 +
54222 + /* Write status in iso_packet_decsriptor */
54223 + iso_packet->status =
54224 + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54225 + if (iso_packet->status) {
54226 + iso_packet->status = -DWC_E_NO_DATA;
54227 + }
54228 + /* Received data length */
54229 + if (!sts.b_iso_out.rxbytes) {
54230 + iso_packet->length =
54231 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
54232 + } else {
54233 + iso_packet->length =
54234 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
54235 + (4 - dwc_ep->data_per_frame % 4);
54236 + }
54237 +
54238 + iso_packet->offset = offset;
54239 + } else {
54240 +/** ISO IN EP */
54241 +
54242 + dma_desc =
54243 + dwc_ep->iso_desc_addr +
54244 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54245 +
54246 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
54247 + sts.d32 = dma_desc->status.d32;
54248 +
54249 + /* Write status in iso packet descriptor */
54250 + iso_packet->status =
54251 + sts.b_iso_in.txsts +
54252 + (sts.b_iso_in.bs ^ BS_DMA_DONE);
54253 + if (iso_packet->status != 0) {
54254 + iso_packet->status = -DWC_E_NO_DATA;
54255 +
54256 + }
54257 + /* Bytes has been transfered */
54258 + iso_packet->length =
54259 + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
54260 +
54261 + dma_desc++;
54262 + iso_packet++;
54263 + }
54264 +
54265 + sts.d32 = dma_desc->status.d32;
54266 + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
54267 + sts.d32 = dma_desc->status.d32;
54268 + }
54269 +
54270 + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
54271 + iso_packet->status =
54272 + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
54273 + if (iso_packet->status != 0) {
54274 + iso_packet->status = -DWC_E_NO_DATA;
54275 + }
54276 +
54277 + /* Bytes has been transfered */
54278 + iso_packet->length =
54279 + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
54280 + }
54281 +}
54282 +
54283 +/**
54284 + * This function reinitialize DMA Descriptors for Isochronous transfer
54285 + *
54286 + * @param core_if Programming view of DWC_otg controller.
54287 + * @param dwc_ep The EP to start the transfer on.
54288 + *
54289 + */
54290 +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
54291 +{
54292 + int i, j;
54293 + dwc_otg_dev_dma_desc_t *dma_desc;
54294 + dma_addr_t dma_ad;
54295 + volatile uint32_t *addr;
54296 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54297 + uint32_t data_per_desc;
54298 +
54299 + if (dwc_ep->is_in == 0) {
54300 + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
54301 + } else {
54302 + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
54303 + }
54304 +
54305 + if (dwc_ep->proc_buf_num == 0) {
54306 + /** Buffer 0 descriptors setup */
54307 + dma_ad = dwc_ep->dma_addr0;
54308 + } else {
54309 + /** Buffer 1 descriptors setup */
54310 + dma_ad = dwc_ep->dma_addr1;
54311 + }
54312 +
54313 + /** Reinit closed DMA Descriptors*/
54314 + /** ISO OUT EP */
54315 + if (dwc_ep->is_in == 0) {
54316 + dma_desc =
54317 + dwc_ep->iso_desc_addr +
54318 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54319 +
54320 + sts.b_iso_out.bs = BS_HOST_READY;
54321 + sts.b_iso_out.rxsts = 0;
54322 + sts.b_iso_out.l = 0;
54323 + sts.b_iso_out.sp = 0;
54324 + sts.b_iso_out.ioc = 0;
54325 + sts.b_iso_out.pid = 0;
54326 + sts.b_iso_out.framenum = 0;
54327 +
54328 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
54329 + i += dwc_ep->pkt_per_frm) {
54330 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
54331 + data_per_desc =
54332 + ((j + 1) * dwc_ep->maxpacket >
54333 + dwc_ep->
54334 + data_per_frame) ? dwc_ep->data_per_frame -
54335 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54336 + data_per_desc +=
54337 + (data_per_desc % 4) ? (4 -
54338 + data_per_desc %
54339 + 4) : 0;
54340 + sts.b_iso_out.rxbytes = data_per_desc;
54341 + dma_desc->buf = dma_ad;
54342 + dma_desc->status.d32 = sts.d32;
54343 +
54344 + dma_ad += data_per_desc;
54345 + dma_desc++;
54346 + }
54347 + }
54348 +
54349 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
54350 +
54351 + data_per_desc =
54352 + ((j + 1) * dwc_ep->maxpacket >
54353 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54354 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54355 + data_per_desc +=
54356 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54357 + sts.b_iso_out.rxbytes = data_per_desc;
54358 +
54359 + dma_desc->buf = dma_ad;
54360 + dma_desc->status.d32 = sts.d32;
54361 +
54362 + dma_desc++;
54363 + dma_ad += data_per_desc;
54364 + }
54365 +
54366 + sts.b_iso_out.ioc = 1;
54367 + sts.b_iso_out.l = dwc_ep->proc_buf_num;
54368 +
54369 + data_per_desc =
54370 + ((j + 1) * dwc_ep->maxpacket >
54371 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54372 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54373 + data_per_desc +=
54374 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54375 + sts.b_iso_out.rxbytes = data_per_desc;
54376 +
54377 + dma_desc->buf = dma_ad;
54378 + dma_desc->status.d32 = sts.d32;
54379 + } else {
54380 +/** ISO IN EP */
54381 +
54382 + dma_desc =
54383 + dwc_ep->iso_desc_addr +
54384 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54385 +
54386 + sts.b_iso_in.bs = BS_HOST_READY;
54387 + sts.b_iso_in.txsts = 0;
54388 + sts.b_iso_in.sp = 0;
54389 + sts.b_iso_in.ioc = 0;
54390 + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
54391 + sts.b_iso_in.framenum = dwc_ep->next_frame;
54392 + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
54393 + sts.b_iso_in.l = 0;
54394 +
54395 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
54396 + dma_desc->buf = dma_ad;
54397 + dma_desc->status.d32 = sts.d32;
54398 +
54399 + sts.b_iso_in.framenum += dwc_ep->bInterval;
54400 + dma_ad += dwc_ep->data_per_frame;
54401 + dma_desc++;
54402 + }
54403 +
54404 + sts.b_iso_in.ioc = 1;
54405 + sts.b_iso_in.l = dwc_ep->proc_buf_num;
54406 +
54407 + dma_desc->buf = dma_ad;
54408 + dma_desc->status.d32 = sts.d32;
54409 +
54410 + dwc_ep->next_frame =
54411 + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
54412 + }
54413 + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
54414 +}
54415 +
54416 +/**
54417 + * This function is to handle Iso EP transfer complete interrupt
54418 + * in case Iso out packet was dropped
54419 + *
54420 + * @param core_if Programming view of DWC_otg controller.
54421 + * @param dwc_ep The EP for wihich transfer complete was asserted
54422 + *
54423 + */
54424 +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
54425 + dwc_ep_t * dwc_ep)
54426 +{
54427 + uint32_t dma_addr;
54428 + uint32_t drp_pkt;
54429 + uint32_t drp_pkt_cnt;
54430 + deptsiz_data_t deptsiz = {.d32 = 0 };
54431 + depctl_data_t depctl = {.d32 = 0 };
54432 + int i;
54433 +
54434 + deptsiz.d32 =
54435 + DWC_READ_REG32(&core_if->dev_if->
54436 + out_ep_regs[dwc_ep->num]->doeptsiz);
54437 +
54438 + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
54439 + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
54440 +
54441 + /* Setting dropped packets status */
54442 + for (i = 0; i < drp_pkt_cnt; ++i) {
54443 + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
54444 + drp_pkt++;
54445 + deptsiz.b.pktcnt--;
54446 + }
54447 +
54448 + if (deptsiz.b.pktcnt > 0) {
54449 + deptsiz.b.xfersize =
54450 + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
54451 + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
54452 + } else {
54453 + deptsiz.b.xfersize = 0;
54454 + deptsiz.b.pktcnt = 0;
54455 + }
54456 +
54457 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
54458 + deptsiz.d32);
54459 +
54460 + if (deptsiz.b.pktcnt > 0) {
54461 + if (dwc_ep->proc_buf_num) {
54462 + dma_addr =
54463 + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
54464 + deptsiz.b.xfersize;
54465 + } else {
54466 + dma_addr =
54467 + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
54468 + deptsiz.b.xfersize;;
54469 + }
54470 +
54471 + DWC_WRITE_REG32(&core_if->dev_if->
54472 + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
54473 +
54474 + /** Re-enable endpoint, clear nak */
54475 + depctl.d32 = 0;
54476 + depctl.b.epena = 1;
54477 + depctl.b.cnak = 1;
54478 +
54479 + DWC_MODIFY_REG32(&core_if->dev_if->
54480 + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
54481 + depctl.d32);
54482 + return 0;
54483 + } else {
54484 + return 1;
54485 + }
54486 +}
54487 +
54488 +/**
54489 + * This function sets iso packets information(PTI mode)
54490 + *
54491 + * @param core_if Programming view of DWC_otg controller.
54492 + * @param ep The EP to start the transfer on.
54493 + *
54494 + */
54495 +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
54496 +{
54497 + int i, j;
54498 + dma_addr_t dma_ad;
54499 + iso_pkt_info_t *packet_info = ep->pkt_info;
54500 + uint32_t offset;
54501 + uint32_t frame_data;
54502 + deptsiz_data_t deptsiz;
54503 +
54504 + if (ep->proc_buf_num == 0) {
54505 + /** Buffer 0 descriptors setup */
54506 + dma_ad = ep->dma_addr0;
54507 + } else {
54508 + /** Buffer 1 descriptors setup */
54509 + dma_ad = ep->dma_addr1;
54510 + }
54511 +
54512 + if (ep->is_in) {
54513 + deptsiz.d32 =
54514 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
54515 + dieptsiz);
54516 + } else {
54517 + deptsiz.d32 =
54518 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
54519 + doeptsiz);
54520 + }
54521 +
54522 + if (!deptsiz.b.xfersize) {
54523 + offset = 0;
54524 + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
54525 + frame_data = ep->data_per_frame;
54526 + for (j = 0; j < ep->pkt_per_frm; ++j) {
54527 +
54528 + /* Packet status - is not set as initially
54529 + * it is set to 0 and if packet was sent
54530 + successfully, status field will remain 0*/
54531 +
54532 + /* Bytes has been transfered */
54533 + packet_info->length =
54534 + (ep->maxpacket <
54535 + frame_data) ? ep->maxpacket : frame_data;
54536 +
54537 + /* Received packet offset */
54538 + packet_info->offset = offset;
54539 + offset += packet_info->length;
54540 + frame_data -= packet_info->length;
54541 +
54542 + packet_info++;
54543 + }
54544 + }
54545 + return 1;
54546 + } else {
54547 + /* This is a workaround for in case of Transfer Complete with
54548 + * PktDrpSts interrupts merging - in this case Transfer complete
54549 + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
54550 + * set and with DOEPTSIZ register non zero. Investigations showed,
54551 + * that this happens when Out packet is dropped, but because of
54552 + * interrupts merging during first interrupt handling PktDrpSts
54553 + * bit is cleared and for next merged interrupts it is not reset.
54554 + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
54555 + */
54556 + if (ep->is_in) {
54557 + return 1;
54558 + } else {
54559 + return handle_iso_out_pkt_dropped(core_if, ep);
54560 + }
54561 + }
54562 +}
54563 +
54564 +/**
54565 + * This function is to handle Iso EP transfer complete interrupt
54566 + *
54567 + * @param pcd The PCD
54568 + * @param ep The EP for which transfer complete was asserted
54569 + *
54570 + */
54571 +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
54572 +{
54573 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
54574 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54575 + uint8_t is_last = 0;
54576 +
54577 + if (ep->dwc_ep.next_frame == 0xffffffff) {
54578 + DWC_WARN("Next frame is not set!\n");
54579 + return;
54580 + }
54581 +
54582 + if (core_if->dma_enable) {
54583 + if (core_if->dma_desc_enable) {
54584 + set_ddma_iso_pkts_info(core_if, dwc_ep);
54585 + reinit_ddma_iso_xfer(core_if, dwc_ep);
54586 + is_last = 1;
54587 + } else {
54588 + if (core_if->pti_enh_enable) {
54589 + if (set_iso_pkts_info(core_if, dwc_ep)) {
54590 + dwc_ep->proc_buf_num =
54591 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
54592 + dwc_otg_iso_ep_start_buf_transfer
54593 + (core_if, dwc_ep);
54594 + is_last = 1;
54595 + }
54596 + } else {
54597 + set_current_pkt_info(core_if, dwc_ep);
54598 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
54599 + is_last = 1;
54600 + dwc_ep->cur_pkt = 0;
54601 + dwc_ep->proc_buf_num =
54602 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
54603 + if (dwc_ep->proc_buf_num) {
54604 + dwc_ep->cur_pkt_addr =
54605 + dwc_ep->xfer_buff1;
54606 + dwc_ep->cur_pkt_dma_addr =
54607 + dwc_ep->dma_addr1;
54608 + } else {
54609 + dwc_ep->cur_pkt_addr =
54610 + dwc_ep->xfer_buff0;
54611 + dwc_ep->cur_pkt_dma_addr =
54612 + dwc_ep->dma_addr0;
54613 + }
54614 +
54615 + }
54616 + dwc_otg_iso_ep_start_frm_transfer(core_if,
54617 + dwc_ep);
54618 + }
54619 + }
54620 + } else {
54621 + set_current_pkt_info(core_if, dwc_ep);
54622 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
54623 + is_last = 1;
54624 + dwc_ep->cur_pkt = 0;
54625 + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
54626 + if (dwc_ep->proc_buf_num) {
54627 + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
54628 + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
54629 + } else {
54630 + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
54631 + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
54632 + }
54633 +
54634 + }
54635 + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
54636 + }
54637 + if (is_last)
54638 + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
54639 +}
54640 +#endif /* DWC_EN_ISOC */
54641 +
54642 +/**
54643 + * This function handle BNA interrupt for Non Isochronous EPs
54644 + *
54645 + */
54646 +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
54647 +{
54648 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54649 + volatile uint32_t *addr;
54650 + depctl_data_t depctl = {.d32 = 0 };
54651 + dwc_otg_pcd_t *pcd = ep->pcd;
54652 + dwc_otg_dev_dma_desc_t *dma_desc;
54653 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54654 + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
54655 + int i, start;
54656 +
54657 + if (!dwc_ep->desc_cnt)
54658 + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
54659 + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
54660 +
54661 + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
54662 + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
54663 + uint32_t doepdma;
54664 + dwc_otg_dev_out_ep_regs_t *out_regs =
54665 + core_if->dev_if->out_ep_regs[dwc_ep->num];
54666 + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
54667 + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
54668 + dma_desc = &(dwc_ep->desc_addr[start]);
54669 + } else {
54670 + start = 0;
54671 + dma_desc = dwc_ep->desc_addr;
54672 + }
54673 +
54674 +
54675 + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
54676 + sts.d32 = dma_desc->status.d32;
54677 + sts.b.bs = BS_HOST_READY;
54678 + dma_desc->status.d32 = sts.d32;
54679 + }
54680 +
54681 + if (dwc_ep->is_in == 0) {
54682 + addr =
54683 + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
54684 + doepctl;
54685 + } else {
54686 + addr =
54687 + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
54688 + }
54689 + depctl.b.epena = 1;
54690 + depctl.b.cnak = 1;
54691 + DWC_MODIFY_REG32(addr, 0, depctl.d32);
54692 +}
54693 +
54694 +/**
54695 + * This function handles EP0 Control transfers.
54696 + *
54697 + * The state of the control transfers are tracked in
54698 + * <code>ep0state</code>.
54699 + */
54700 +static void handle_ep0(dwc_otg_pcd_t * pcd)
54701 +{
54702 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
54703 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
54704 + dev_dma_desc_sts_t desc_sts;
54705 + deptsiz0_data_t deptsiz;
54706 + uint32_t byte_count;
54707 +
54708 +#ifdef DEBUG_EP0
54709 + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
54710 + print_ep0_state(pcd);
54711 +#endif
54712 +
54713 +// DWC_PRINTF("HANDLE EP0\n");
54714 +
54715 + switch (pcd->ep0state) {
54716 + case EP0_DISCONNECT:
54717 + break;
54718 +
54719 + case EP0_IDLE:
54720 + pcd->request_config = 0;
54721 +
54722 + pcd_setup(pcd);
54723 + break;
54724 +
54725 + case EP0_IN_DATA_PHASE:
54726 +#ifdef DEBUG_EP0
54727 + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
54728 + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
54729 + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
54730 +#endif
54731 +
54732 + if (core_if->dma_enable != 0) {
54733 + /*
54734 + * For EP0 we can only program 1 packet at a time so we
54735 + * need to do the make calculations after each complete.
54736 + * Call write_packet to make the calculations, as in
54737 + * slave mode, and use those values to determine if we
54738 + * can complete.
54739 + */
54740 + if (core_if->dma_desc_enable == 0) {
54741 + deptsiz.d32 =
54742 + DWC_READ_REG32(&core_if->
54743 + dev_if->in_ep_regs[0]->
54744 + dieptsiz);
54745 + byte_count =
54746 + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
54747 + } else {
54748 + desc_sts =
54749 + core_if->dev_if->in_desc_addr->status;
54750 + byte_count =
54751 + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
54752 + }
54753 + ep0->dwc_ep.xfer_count += byte_count;
54754 + ep0->dwc_ep.xfer_buff += byte_count;
54755 + ep0->dwc_ep.dma_addr += byte_count;
54756 + }
54757 + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
54758 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54759 + &ep0->dwc_ep);
54760 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
54761 + } else if (ep0->dwc_ep.sent_zlp) {
54762 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54763 + &ep0->dwc_ep);
54764 + ep0->dwc_ep.sent_zlp = 0;
54765 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
54766 + } else {
54767 + ep0_complete_request(ep0);
54768 + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
54769 + }
54770 + break;
54771 + case EP0_OUT_DATA_PHASE:
54772 +#ifdef DEBUG_EP0
54773 + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
54774 + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
54775 + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
54776 +#endif
54777 + if (core_if->dma_enable != 0) {
54778 + if (core_if->dma_desc_enable == 0) {
54779 + deptsiz.d32 =
54780 + DWC_READ_REG32(&core_if->
54781 + dev_if->out_ep_regs[0]->
54782 + doeptsiz);
54783 + byte_count =
54784 + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
54785 + } else {
54786 + desc_sts =
54787 + core_if->dev_if->out_desc_addr->status;
54788 + byte_count =
54789 + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
54790 + }
54791 + ep0->dwc_ep.xfer_count += byte_count;
54792 + ep0->dwc_ep.xfer_buff += byte_count;
54793 + ep0->dwc_ep.dma_addr += byte_count;
54794 + }
54795 + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
54796 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54797 + &ep0->dwc_ep);
54798 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
54799 + } else if (ep0->dwc_ep.sent_zlp) {
54800 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54801 + &ep0->dwc_ep);
54802 + ep0->dwc_ep.sent_zlp = 0;
54803 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
54804 + } else {
54805 + ep0_complete_request(ep0);
54806 + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
54807 + }
54808 + break;
54809 +
54810 + case EP0_IN_STATUS_PHASE:
54811 + case EP0_OUT_STATUS_PHASE:
54812 + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
54813 + ep0_complete_request(ep0);
54814 + pcd->ep0state = EP0_IDLE;
54815 + ep0->stopped = 1;
54816 + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
54817 +
54818 + /* Prepare for more SETUP Packets */
54819 + if (core_if->dma_enable) {
54820 + ep0_out_start(core_if, pcd);
54821 + }
54822 + break;
54823 +
54824 + case EP0_STALL:
54825 + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
54826 + break;
54827 + }
54828 +#ifdef DEBUG_EP0
54829 + print_ep0_state(pcd);
54830 +#endif
54831 +}
54832 +
54833 +/**
54834 + * Restart transfer
54835 + */
54836 +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
54837 +{
54838 + dwc_otg_core_if_t *core_if;
54839 + dwc_otg_dev_if_t *dev_if;
54840 + deptsiz_data_t dieptsiz = {.d32 = 0 };
54841 + dwc_otg_pcd_ep_t *ep;
54842 +
54843 + ep = get_in_ep(pcd, epnum);
54844 +
54845 +#ifdef DWC_EN_ISOC
54846 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
54847 + return;
54848 + }
54849 +#endif /* DWC_EN_ISOC */
54850 +
54851 + core_if = GET_CORE_IF(pcd);
54852 + dev_if = core_if->dev_if;
54853 +
54854 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
54855 +
54856 + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
54857 + " stopped=%d\n", ep->dwc_ep.xfer_buff,
54858 + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
54859 + /*
54860 + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
54861 + */
54862 + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
54863 + ep->dwc_ep.start_xfer_buff != 0) {
54864 + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
54865 + ep->dwc_ep.xfer_count = 0;
54866 + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
54867 + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
54868 + } else {
54869 + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
54870 + /* convert packet size to dwords. */
54871 + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
54872 + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
54873 + }
54874 + ep->stopped = 0;
54875 + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
54876 + "xfer_len=%0x stopped=%d\n",
54877 + ep->dwc_ep.xfer_buff,
54878 + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
54879 + ep->stopped);
54880 + if (epnum == 0) {
54881 + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
54882 + } else {
54883 + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
54884 + }
54885 + }
54886 +}
54887 +
54888 +/*
54889 + * This function create new nextep sequnce based on Learn Queue.
54890 + *
54891 + * @param core_if Programming view of DWC_otg controller
54892 + */
54893 +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
54894 +{
54895 + dwc_otg_device_global_regs_t *dev_global_regs =
54896 + core_if->dev_if->dev_global_regs;
54897 + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
54898 + /* Number of Token Queue Registers */
54899 + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
54900 + dtknq1_data_t dtknqr1;
54901 + uint32_t in_tkn_epnums[4];
54902 + uint8_t seqnum[MAX_EPS_CHANNELS];
54903 + uint8_t intkn_seq[TOKEN_Q_DEPTH];
54904 + grstctl_t resetctl = {.d32 = 0 };
54905 + uint8_t temp;
54906 + int ndx = 0;
54907 + int start = 0;
54908 + int end = 0;
54909 + int sort_done = 0;
54910 + int i = 0;
54911 + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
54912 +
54913 +
54914 + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
54915 +
54916 + /* Read the DTKNQ Registers */
54917 + for (i = 0; i < DTKNQ_REG_CNT; i++) {
54918 + in_tkn_epnums[i] = DWC_READ_REG32(addr);
54919 + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
54920 + in_tkn_epnums[i]);
54921 + if (addr == &dev_global_regs->dvbusdis) {
54922 + addr = &dev_global_regs->dtknqr3_dthrctl;
54923 + } else {
54924 + ++addr;
54925 + }
54926 +
54927 + }
54928 +
54929 + /* Copy the DTKNQR1 data to the bit field. */
54930 + dtknqr1.d32 = in_tkn_epnums[0];
54931 + if (dtknqr1.b.wrap_bit) {
54932 + ndx = dtknqr1.b.intknwptr;
54933 + end = ndx -1;
54934 + if (end < 0)
54935 + end = TOKEN_Q_DEPTH -1;
54936 + } else {
54937 + ndx = 0;
54938 + end = dtknqr1.b.intknwptr -1;
54939 + if (end < 0)
54940 + end = 0;
54941 + }
54942 + start = ndx;
54943 +
54944 + /* Fill seqnum[] by initial values: EP number + 31 */
54945 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
54946 + seqnum[i] = i +31;
54947 + }
54948 +
54949 + /* Fill intkn_seq[] from in_tkn_epnums[0] */
54950 + for (i=0; i < 6; i++)
54951 + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
54952 +
54953 + if (TOKEN_Q_DEPTH > 6) {
54954 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54955 + for (i=6; i < 14; i++)
54956 + intkn_seq[i] =
54957 + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
54958 + }
54959 +
54960 + if (TOKEN_Q_DEPTH > 14) {
54961 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54962 + for (i=14; i < 22; i++)
54963 + intkn_seq[i] =
54964 + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
54965 + }
54966 +
54967 + if (TOKEN_Q_DEPTH > 22) {
54968 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54969 + for (i=22; i < 30; i++)
54970 + intkn_seq[i] =
54971 + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
54972 + }
54973 +
54974 + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
54975 + start, end);
54976 + for (i=0; i<TOKEN_Q_DEPTH; i++)
54977 + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
54978 +
54979 + /* Update seqnum based on intkn_seq[] */
54980 + i = 0;
54981 + do {
54982 + seqnum[intkn_seq[ndx]] = i;
54983 + ndx++;
54984 + i++;
54985 + if (ndx == TOKEN_Q_DEPTH)
54986 + ndx = 0;
54987 + } while ( i < TOKEN_Q_DEPTH );
54988 +
54989 + /* Mark non active EP's in seqnum[] by 0xff */
54990 + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
54991 + if (core_if->nextep_seq[i] == 0xff )
54992 + seqnum[i] = 0xff;
54993 + }
54994 +
54995 + /* Sort seqnum[] */
54996 + sort_done = 0;
54997 + while (!sort_done) {
54998 + sort_done = 1;
54999 + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
55000 + if (seqnum[i] > seqnum[i+1]) {
55001 + temp = seqnum[i];
55002 + seqnum[i] = seqnum[i+1];
55003 + seqnum[i+1] = temp;
55004 + sort_done = 0;
55005 + }
55006 + }
55007 + }
55008 +
55009 + ndx = start + seqnum[0];
55010 + if (ndx >= TOKEN_Q_DEPTH)
55011 + ndx = ndx % TOKEN_Q_DEPTH;
55012 + core_if->first_in_nextep_seq = intkn_seq[ndx];
55013 +
55014 + /* Update seqnum[] by EP numbers */
55015 + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
55016 + ndx = start + i;
55017 + if (seqnum[i] < 31) {
55018 + ndx = start + seqnum[i];
55019 + if (ndx >= TOKEN_Q_DEPTH)
55020 + ndx = ndx % TOKEN_Q_DEPTH;
55021 + seqnum[i] = intkn_seq[ndx];
55022 + } else {
55023 + if (seqnum[i] < 0xff) {
55024 + seqnum[i] = seqnum[i] - 31;
55025 + } else {
55026 + break;
55027 + }
55028 + }
55029 + }
55030 +
55031 + /* Update nextep_seq[] based on seqnum[] */
55032 + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
55033 + if (seqnum[i] != 0xff) {
55034 + if (seqnum[i+1] != 0xff) {
55035 + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
55036 + } else {
55037 + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
55038 + break;
55039 + }
55040 + } else {
55041 + break;
55042 + }
55043 + }
55044 +
55045 + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
55046 + __func__, core_if->first_in_nextep_seq);
55047 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
55048 + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
55049 + }
55050 +
55051 + /* Flush the Learning Queue */
55052 + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
55053 + resetctl.b.intknqflsh = 1;
55054 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
55055 +
55056 +
55057 +}
55058 +
55059 +/**
55060 + * handle the IN EP disable interrupt.
55061 + */
55062 +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
55063 + const uint32_t epnum)
55064 +{
55065 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55066 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
55067 + deptsiz_data_t dieptsiz = {.d32 = 0 };
55068 + dctl_data_t dctl = {.d32 = 0 };
55069 + dwc_otg_pcd_ep_t *ep;
55070 + dwc_ep_t *dwc_ep;
55071 + gintmsk_data_t gintmsk_data;
55072 + depctl_data_t depctl;
55073 + uint32_t diepdma;
55074 + uint32_t remain_to_transfer = 0;
55075 + uint8_t i;
55076 + uint32_t xfer_size;
55077 +
55078 + ep = get_in_ep(pcd, epnum);
55079 + dwc_ep = &ep->dwc_ep;
55080 +
55081 + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55082 + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
55083 + complete_ep(ep);
55084 + return;
55085 + }
55086 +
55087 + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
55088 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
55089 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
55090 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55091 +
55092 + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
55093 + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
55094 +
55095 + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
55096 + if (ep->stopped) {
55097 + if (core_if->en_multiple_tx_fifo)
55098 + /* Flush the Tx FIFO */
55099 + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
55100 + /* Clear the Global IN NP NAK */
55101 + dctl.d32 = 0;
55102 + dctl.b.cgnpinnak = 1;
55103 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
55104 + /* Restart the transaction */
55105 + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
55106 + restart_transfer(pcd, epnum);
55107 + }
55108 + } else {
55109 + /* Restart the transaction */
55110 + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
55111 + restart_transfer(pcd, epnum);
55112 + }
55113 + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
55114 + }
55115 + return;
55116 + }
55117 +
55118 + if (core_if->start_predict > 2) { // NP IN EP
55119 + core_if->start_predict--;
55120 + return;
55121 + }
55122 +
55123 + core_if->start_predict--;
55124 +
55125 + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
55126 +
55127 + predict_nextep_seq(core_if);
55128 +
55129 + /* Update all active IN EP's NextEP field based of nextep_seq[] */
55130 + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
55131 + depctl.d32 =
55132 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55133 + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
55134 + depctl.b.nextep = core_if->nextep_seq[i];
55135 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
55136 + }
55137 + }
55138 + /* Flush Shared NP TxFIFO */
55139 + dwc_otg_flush_tx_fifo(core_if, 0);
55140 + /* Rewind buffers */
55141 + if (!core_if->dma_desc_enable) {
55142 + i = core_if->first_in_nextep_seq;
55143 + do {
55144 + ep = get_in_ep(pcd, i);
55145 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
55146 + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
55147 + if (xfer_size > ep->dwc_ep.maxxfer)
55148 + xfer_size = ep->dwc_ep.maxxfer;
55149 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55150 + if (dieptsiz.b.pktcnt != 0) {
55151 + if (xfer_size == 0) {
55152 + remain_to_transfer = 0;
55153 + } else {
55154 + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
55155 + remain_to_transfer =
55156 + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
55157 + } else {
55158 + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
55159 + + (xfer_size % ep->dwc_ep.maxpacket);
55160 + }
55161 + }
55162 + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
55163 + dieptsiz.b.xfersize = remain_to_transfer;
55164 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
55165 + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
55166 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
55167 + }
55168 + i = core_if->nextep_seq[i];
55169 + } while (i != core_if->first_in_nextep_seq);
55170 + } else { // dma_desc_enable
55171 + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
55172 + }
55173 +
55174 + /* Restart transfers in predicted sequences */
55175 + i = core_if->first_in_nextep_seq;
55176 + do {
55177 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
55178 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55179 + if (dieptsiz.b.pktcnt != 0) {
55180 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55181 + depctl.b.epena = 1;
55182 + depctl.b.cnak = 1;
55183 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
55184 + }
55185 + i = core_if->nextep_seq[i];
55186 + } while (i != core_if->first_in_nextep_seq);
55187 +
55188 + /* Clear the global non-periodic IN NAK handshake */
55189 + dctl.d32 = 0;
55190 + dctl.b.cgnpinnak = 1;
55191 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
55192 +
55193 + /* Unmask EP Mismatch interrupt */
55194 + gintmsk_data.d32 = 0;
55195 + gintmsk_data.b.epmismatch = 1;
55196 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
55197 +
55198 + core_if->start_predict = 0;
55199 +
55200 + }
55201 +}
55202 +
55203 +/**
55204 + * Handler for the IN EP timeout handshake interrupt.
55205 + */
55206 +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
55207 + const uint32_t epnum)
55208 +{
55209 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55210 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
55211 +
55212 +#ifdef DEBUG
55213 + deptsiz_data_t dieptsiz = {.d32 = 0 };
55214 + uint32_t num = 0;
55215 +#endif
55216 + dctl_data_t dctl = {.d32 = 0 };
55217 + dwc_otg_pcd_ep_t *ep;
55218 +
55219 + gintmsk_data_t intr_mask = {.d32 = 0 };
55220 +
55221 + ep = get_in_ep(pcd, epnum);
55222 +
55223 + /* Disable the NP Tx Fifo Empty Interrrupt */
55224 + if (!core_if->dma_enable) {
55225 + intr_mask.b.nptxfempty = 1;
55226 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
55227 + intr_mask.d32, 0);
55228 + }
55229 + /** @todo NGS Check EP type.
55230 + * Implement for Periodic EPs */
55231 + /*
55232 + * Non-periodic EP
55233 + */
55234 + /* Enable the Global IN NAK Effective Interrupt */
55235 + intr_mask.b.ginnakeff = 1;
55236 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
55237 +
55238 + /* Set Global IN NAK */
55239 + dctl.b.sgnpinnak = 1;
55240 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
55241 +
55242 + ep->stopped = 1;
55243 +
55244 +#ifdef DEBUG
55245 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
55246 + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
55247 + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
55248 +#endif
55249 +
55250 +#ifdef DISABLE_PERIODIC_EP
55251 + /*
55252 + * Set the NAK bit for this EP to
55253 + * start the disable process.
55254 + */
55255 + diepctl.d32 = 0;
55256 + diepctl.b.snak = 1;
55257 + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
55258 + diepctl.d32);
55259 + ep->disabling = 1;
55260 + ep->stopped = 1;
55261 +#endif
55262 +}
55263 +
55264 +/**
55265 + * Handler for the IN EP NAK interrupt.
55266 + */
55267 +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
55268 + const uint32_t epnum)
55269 +{
55270 + /** @todo implement ISR */
55271 + dwc_otg_core_if_t *core_if;
55272 + diepmsk_data_t intr_mask = {.d32 = 0 };
55273 +
55274 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
55275 + core_if = GET_CORE_IF(pcd);
55276 + intr_mask.b.nak = 1;
55277 +
55278 + if (core_if->multiproc_int_enable) {
55279 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55280 + diepeachintmsk[epnum], intr_mask.d32, 0);
55281 + } else {
55282 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
55283 + intr_mask.d32, 0);
55284 + }
55285 +
55286 + return 1;
55287 +}
55288 +
55289 +/**
55290 + * Handler for the OUT EP Babble interrupt.
55291 + */
55292 +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
55293 + const uint32_t epnum)
55294 +{
55295 + /** @todo implement ISR */
55296 + dwc_otg_core_if_t *core_if;
55297 + doepmsk_data_t intr_mask = {.d32 = 0 };
55298 +
55299 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
55300 + "OUT EP Babble");
55301 + core_if = GET_CORE_IF(pcd);
55302 + intr_mask.b.babble = 1;
55303 +
55304 + if (core_if->multiproc_int_enable) {
55305 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55306 + doepeachintmsk[epnum], intr_mask.d32, 0);
55307 + } else {
55308 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55309 + intr_mask.d32, 0);
55310 + }
55311 +
55312 + return 1;
55313 +}
55314 +
55315 +/**
55316 + * Handler for the OUT EP NAK interrupt.
55317 + */
55318 +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
55319 + const uint32_t epnum)
55320 +{
55321 + /** @todo implement ISR */
55322 + dwc_otg_core_if_t *core_if;
55323 + doepmsk_data_t intr_mask = {.d32 = 0 };
55324 +
55325 + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
55326 + core_if = GET_CORE_IF(pcd);
55327 + intr_mask.b.nak = 1;
55328 +
55329 + if (core_if->multiproc_int_enable) {
55330 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55331 + doepeachintmsk[epnum], intr_mask.d32, 0);
55332 + } else {
55333 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55334 + intr_mask.d32, 0);
55335 + }
55336 +
55337 + return 1;
55338 +}
55339 +
55340 +/**
55341 + * Handler for the OUT EP NYET interrupt.
55342 + */
55343 +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
55344 + const uint32_t epnum)
55345 +{
55346 + /** @todo implement ISR */
55347 + dwc_otg_core_if_t *core_if;
55348 + doepmsk_data_t intr_mask = {.d32 = 0 };
55349 +
55350 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
55351 + core_if = GET_CORE_IF(pcd);
55352 + intr_mask.b.nyet = 1;
55353 +
55354 + if (core_if->multiproc_int_enable) {
55355 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55356 + doepeachintmsk[epnum], intr_mask.d32, 0);
55357 + } else {
55358 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55359 + intr_mask.d32, 0);
55360 + }
55361 +
55362 + return 1;
55363 +}
55364 +
55365 +/**
55366 + * This interrupt indicates that an IN EP has a pending Interrupt.
55367 + * The sequence for handling the IN EP interrupt is shown below:
55368 + * -# Read the Device All Endpoint Interrupt register
55369 + * -# Repeat the following for each IN EP interrupt bit set (from
55370 + * LSB to MSB).
55371 + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
55372 + * -# If "Transfer Complete" call the request complete function
55373 + * -# If "Endpoint Disabled" complete the EP disable procedure.
55374 + * -# If "AHB Error Interrupt" log error
55375 + * -# If "Time-out Handshake" log error
55376 + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
55377 + * FIFO.
55378 + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
55379 + * Mismatch Interrupt)
55380 + */
55381 +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
55382 +{
55383 +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
55384 +do { \
55385 + diepint_data_t diepint = {.d32=0}; \
55386 + diepint.b.__intr = 1; \
55387 + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
55388 + diepint.d32); \
55389 +} while (0)
55390 +
55391 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55392 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
55393 + diepint_data_t diepint = {.d32 = 0 };
55394 + depctl_data_t depctl = {.d32 = 0 };
55395 + uint32_t ep_intr;
55396 + uint32_t epnum = 0;
55397 + dwc_otg_pcd_ep_t *ep;
55398 + dwc_ep_t *dwc_ep;
55399 + gintmsk_data_t intr_mask = {.d32 = 0 };
55400 +
55401 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
55402 +
55403 + /* Read in the device interrupt bits */
55404 + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
55405 +
55406 + /* Service the Device IN interrupts for each endpoint */
55407 + while (ep_intr) {
55408 + if (ep_intr & 0x1) {
55409 + uint32_t empty_msk;
55410 + /* Get EP pointer */
55411 + ep = get_in_ep(pcd, epnum);
55412 + dwc_ep = &ep->dwc_ep;
55413 +
55414 + depctl.d32 =
55415 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55416 + empty_msk =
55417 + DWC_READ_REG32(&dev_if->
55418 + dev_global_regs->dtknqr4_fifoemptymsk);
55419 +
55420 + DWC_DEBUGPL(DBG_PCDV,
55421 + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
55422 + epnum, empty_msk, depctl.d32);
55423 +
55424 + DWC_DEBUGPL(DBG_PCD,
55425 + "EP%d-%s: type=%d, mps=%d\n",
55426 + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
55427 + dwc_ep->type, dwc_ep->maxpacket);
55428 +
55429 + diepint.d32 =
55430 + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
55431 +
55432 + DWC_DEBUGPL(DBG_PCDV,
55433 + "EP %d Interrupt Register - 0x%x\n", epnum,
55434 + diepint.d32);
55435 + /* Transfer complete */
55436 + if (diepint.b.xfercompl) {
55437 + /* Disable the NP Tx FIFO Empty
55438 + * Interrupt */
55439 + if (core_if->en_multiple_tx_fifo == 0) {
55440 + intr_mask.b.nptxfempty = 1;
55441 + DWC_MODIFY_REG32
55442 + (&core_if->core_global_regs->gintmsk,
55443 + intr_mask.d32, 0);
55444 + } else {
55445 + /* Disable the Tx FIFO Empty Interrupt for this EP */
55446 + uint32_t fifoemptymsk =
55447 + 0x1 << dwc_ep->num;
55448 + DWC_MODIFY_REG32(&core_if->
55449 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
55450 + fifoemptymsk, 0);
55451 + }
55452 + /* Clear the bit in DIEPINTn for this interrupt */
55453 + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
55454 +
55455 + /* Complete the transfer */
55456 + if (epnum == 0) {
55457 + handle_ep0(pcd);
55458 + }
55459 +#ifdef DWC_EN_ISOC
55460 + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55461 + if (!ep->stopped)
55462 + complete_iso_ep(pcd, ep);
55463 + }
55464 +#endif /* DWC_EN_ISOC */
55465 +#ifdef DWC_UTE_PER_IO
55466 + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55467 + if (!ep->stopped)
55468 + complete_xiso_ep(ep);
55469 + }
55470 +#endif /* DWC_UTE_PER_IO */
55471 + else {
55472 + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
55473 + dwc_ep->bInterval > 1) {
55474 + dwc_ep->frame_num += dwc_ep->bInterval;
55475 + if (dwc_ep->frame_num > 0x3FFF)
55476 + {
55477 + dwc_ep->frm_overrun = 1;
55478 + dwc_ep->frame_num &= 0x3FFF;
55479 + } else
55480 + dwc_ep->frm_overrun = 0;
55481 + }
55482 + complete_ep(ep);
55483 + if(diepint.b.nak)
55484 + CLEAR_IN_EP_INTR(core_if, epnum, nak);
55485 + }
55486 + }
55487 + /* Endpoint disable */
55488 + if (diepint.b.epdisabled) {
55489 + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
55490 + epnum);
55491 + handle_in_ep_disable_intr(pcd, epnum);
55492 +
55493 + /* Clear the bit in DIEPINTn for this interrupt */
55494 + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
55495 + }
55496 + /* AHB Error */
55497 + if (diepint.b.ahberr) {
55498 + DWC_ERROR("EP%d IN AHB Error\n", epnum);
55499 + /* Clear the bit in DIEPINTn for this interrupt */
55500 + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
55501 + }
55502 + /* TimeOUT Handshake (non-ISOC IN EPs) */
55503 + if (diepint.b.timeout) {
55504 + DWC_ERROR("EP%d IN Time-out\n", epnum);
55505 + handle_in_ep_timeout_intr(pcd, epnum);
55506 +
55507 + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
55508 + }
55509 + /** IN Token received with TxF Empty */
55510 + if (diepint.b.intktxfemp) {
55511 + DWC_DEBUGPL(DBG_ANY,
55512 + "EP%d IN TKN TxFifo Empty\n",
55513 + epnum);
55514 + if (!ep->stopped && epnum != 0) {
55515 +
55516 + diepmsk_data_t diepmsk = {.d32 = 0 };
55517 + diepmsk.b.intktxfemp = 1;
55518 +
55519 + if (core_if->multiproc_int_enable) {
55520 + DWC_MODIFY_REG32
55521 + (&dev_if->dev_global_regs->diepeachintmsk
55522 + [epnum], diepmsk.d32, 0);
55523 + } else {
55524 + DWC_MODIFY_REG32
55525 + (&dev_if->dev_global_regs->diepmsk,
55526 + diepmsk.d32, 0);
55527 + }
55528 + } else if (core_if->dma_desc_enable
55529 + && epnum == 0
55530 + && pcd->ep0state ==
55531 + EP0_OUT_STATUS_PHASE) {
55532 + // EP0 IN set STALL
55533 + depctl.d32 =
55534 + DWC_READ_REG32(&dev_if->in_ep_regs
55535 + [epnum]->diepctl);
55536 +
55537 + /* set the disable and stall bits */
55538 + if (depctl.b.epena) {
55539 + depctl.b.epdis = 1;
55540 + }
55541 + depctl.b.stall = 1;
55542 + DWC_WRITE_REG32(&dev_if->in_ep_regs
55543 + [epnum]->diepctl,
55544 + depctl.d32);
55545 + }
55546 + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
55547 + }
55548 + /** IN Token Received with EP mismatch */
55549 + if (diepint.b.intknepmis) {
55550 + DWC_DEBUGPL(DBG_ANY,
55551 + "EP%d IN TKN EP Mismatch\n", epnum);
55552 + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
55553 + }
55554 + /** IN Endpoint NAK Effective */
55555 + if (diepint.b.inepnakeff) {
55556 + DWC_DEBUGPL(DBG_ANY,
55557 + "EP%d IN EP NAK Effective\n",
55558 + epnum);
55559 + /* Periodic EP */
55560 + if (ep->disabling) {
55561 + depctl.d32 = 0;
55562 + depctl.b.snak = 1;
55563 + depctl.b.epdis = 1;
55564 + DWC_MODIFY_REG32(&dev_if->in_ep_regs
55565 + [epnum]->diepctl,
55566 + depctl.d32,
55567 + depctl.d32);
55568 + }
55569 + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
55570 +
55571 + }
55572 +
55573 + /** IN EP Tx FIFO Empty Intr */
55574 + if (diepint.b.emptyintr) {
55575 + DWC_DEBUGPL(DBG_ANY,
55576 + "EP%d Tx FIFO Empty Intr \n",
55577 + epnum);
55578 + write_empty_tx_fifo(pcd, epnum);
55579 +
55580 + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
55581 +
55582 + }
55583 +
55584 + /** IN EP BNA Intr */
55585 + if (diepint.b.bna) {
55586 + CLEAR_IN_EP_INTR(core_if, epnum, bna);
55587 + if (core_if->dma_desc_enable) {
55588 +#ifdef DWC_EN_ISOC
55589 + if (dwc_ep->type ==
55590 + DWC_OTG_EP_TYPE_ISOC) {
55591 + /*
55592 + * This checking is performed to prevent first "false" BNA
55593 + * handling occuring right after reconnect
55594 + */
55595 + if (dwc_ep->next_frame !=
55596 + 0xffffffff)
55597 + dwc_otg_pcd_handle_iso_bna(ep);
55598 + } else
55599 +#endif /* DWC_EN_ISOC */
55600 + {
55601 + dwc_otg_pcd_handle_noniso_bna(ep);
55602 + }
55603 + }
55604 + }
55605 + /* NAK Interrutp */
55606 + if (diepint.b.nak) {
55607 + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
55608 + epnum);
55609 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
55610 + depctl_data_t depctl;
55611 + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
55612 + ep->dwc_ep.frame_num = core_if->frame_num;
55613 + if (ep->dwc_ep.bInterval > 1) {
55614 + depctl.d32 = 0;
55615 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55616 + if (ep->dwc_ep.frame_num & 0x1) {
55617 + depctl.b.setd1pid = 1;
55618 + depctl.b.setd0pid = 0;
55619 + } else {
55620 + depctl.b.setd0pid = 1;
55621 + depctl.b.setd1pid = 0;
55622 + }
55623 + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
55624 + }
55625 + start_next_request(ep);
55626 + }
55627 + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
55628 + if (dwc_ep->frame_num > 0x3FFF) {
55629 + dwc_ep->frm_overrun = 1;
55630 + dwc_ep->frame_num &= 0x3FFF;
55631 + } else
55632 + dwc_ep->frm_overrun = 0;
55633 + }
55634 +
55635 + CLEAR_IN_EP_INTR(core_if, epnum, nak);
55636 + }
55637 + }
55638 + epnum++;
55639 + ep_intr >>= 1;
55640 + }
55641 +
55642 + return 1;
55643 +#undef CLEAR_IN_EP_INTR
55644 +}
55645 +
55646 +/**
55647 + * This interrupt indicates that an OUT EP has a pending Interrupt.
55648 + * The sequence for handling the OUT EP interrupt is shown below:
55649 + * -# Read the Device All Endpoint Interrupt register
55650 + * -# Repeat the following for each OUT EP interrupt bit set (from
55651 + * LSB to MSB).
55652 + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
55653 + * -# If "Transfer Complete" call the request complete function
55654 + * -# If "Endpoint Disabled" complete the EP disable procedure.
55655 + * -# If "AHB Error Interrupt" log error
55656 + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
55657 + * Command Processing)
55658 + */
55659 +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
55660 +{
55661 +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
55662 +do { \
55663 + doepint_data_t doepint = {.d32=0}; \
55664 + doepint.b.__intr = 1; \
55665 + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
55666 + doepint.d32); \
55667 +} while (0)
55668 +
55669 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55670 + uint32_t ep_intr;
55671 + doepint_data_t doepint = {.d32 = 0 };
55672 + uint32_t epnum = 0;
55673 + dwc_otg_pcd_ep_t *ep;
55674 + dwc_ep_t *dwc_ep;
55675 + dctl_data_t dctl = {.d32 = 0 };
55676 + gintmsk_data_t gintmsk = {.d32 = 0 };
55677 +
55678 +
55679 + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
55680 +
55681 + /* Read in the device interrupt bits */
55682 + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
55683 +
55684 + while (ep_intr) {
55685 + if (ep_intr & 0x1) {
55686 + /* Get EP pointer */
55687 + ep = get_out_ep(pcd, epnum);
55688 + dwc_ep = &ep->dwc_ep;
55689 +
55690 +#ifdef VERBOSE
55691 + DWC_DEBUGPL(DBG_PCDV,
55692 + "EP%d-%s: type=%d, mps=%d\n",
55693 + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
55694 + dwc_ep->type, dwc_ep->maxpacket);
55695 +#endif
55696 + doepint.d32 =
55697 + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
55698 + /* Moved this interrupt upper due to core deffect of asserting
55699 + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
55700 + if (doepint.b.stsphsercvd) {
55701 + deptsiz0_data_t deptsiz;
55702 + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
55703 + deptsiz.d32 =
55704 + DWC_READ_REG32(&core_if->dev_if->
55705 + out_ep_regs[0]->doeptsiz);
55706 + if (core_if->snpsid >= OTG_CORE_REV_3_00a
55707 + && core_if->dma_enable
55708 + && core_if->dma_desc_enable == 0
55709 + && doepint.b.xfercompl
55710 + && deptsiz.b.xfersize == 24) {
55711 + CLEAR_OUT_EP_INTR(core_if, epnum,
55712 + xfercompl);
55713 + doepint.b.xfercompl = 0;
55714 + ep0_out_start(core_if, pcd);
55715 + }
55716 + if ((core_if->dma_desc_enable) ||
55717 + (core_if->dma_enable
55718 + && core_if->snpsid >=
55719 + OTG_CORE_REV_3_00a)) {
55720 + do_setup_in_status_phase(pcd);
55721 + }
55722 + }
55723 + /* Transfer complete */
55724 + if (doepint.b.xfercompl) {
55725 +
55726 + if (epnum == 0) {
55727 + /* Clear the bit in DOEPINTn for this interrupt */
55728 + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
55729 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
55730 + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
55731 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
55732 + doepint.d32);
55733 + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
55734 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
55735 +
55736 + if (core_if->snpsid >= OTG_CORE_REV_3_00a
55737 + && core_if->dma_enable == 0) {
55738 + doepint_data_t doepint;
55739 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55740 + out_ep_regs[0]->doepint);
55741 + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
55742 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55743 + goto exit_xfercompl;
55744 + }
55745 + }
55746 + /* In case of DDMA look at SR bit to go to the Data Stage */
55747 + if (core_if->dma_desc_enable) {
55748 + dev_dma_desc_sts_t status = {.d32 = 0};
55749 + if (pcd->ep0state == EP0_IDLE) {
55750 + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
55751 + dev_if->setup_desc_index]->status.d32;
55752 + if(pcd->data_terminated) {
55753 + pcd->data_terminated = 0;
55754 + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
55755 + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
55756 + }
55757 + if (status.b.sr) {
55758 + if (doepint.b.setup) {
55759 + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
55760 + /* Already started data stage, clear setup */
55761 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55762 + doepint.b.setup = 0;
55763 + handle_ep0(pcd);
55764 + /* Prepare for more setup packets */
55765 + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
55766 + pcd->ep0state == EP0_IN_DATA_PHASE) {
55767 + ep0_out_start(core_if, pcd);
55768 + }
55769 +
55770 + goto exit_xfercompl;
55771 + } else {
55772 + /* Prepare for more setup packets */
55773 + DWC_DEBUGPL(DBG_PCDV,
55774 + "EP0_IDLE SR=1 setup=0 new setup comes\n");
55775 + ep0_out_start(core_if, pcd);
55776 + }
55777 + }
55778 + } else {
55779 + dwc_otg_pcd_request_t *req;
55780 + dev_dma_desc_sts_t status = {.d32 = 0};
55781 + diepint_data_t diepint0;
55782 + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
55783 + in_ep_regs[0]->diepint);
55784 +
55785 + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
55786 + DWC_ERROR("EP0 is stalled/disconnected\n");
55787 + }
55788 +
55789 + /* Clear IN xfercompl if set */
55790 + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
55791 + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
55792 + DWC_WRITE_REG32(&core_if->dev_if->
55793 + in_ep_regs[0]->diepint, diepint0.d32);
55794 + }
55795 +
55796 + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
55797 + dev_if->setup_desc_index]->status.d32;
55798 +
55799 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
55800 + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
55801 + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
55802 + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
55803 + status.d32 = core_if->dev_if->
55804 + out_desc_addr->status.d32;
55805 +
55806 + if (status.b.sr) {
55807 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55808 + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
55809 + } else {
55810 + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
55811 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55812 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
55813 + pcd->ep0state == EP0_OUT_DATA_PHASE) {
55814 + /* Read arrived setup packet from req->buf */
55815 + dwc_memcpy(&pcd->setup_pkt->req,
55816 + req->buf + ep->dwc_ep.xfer_count, 8);
55817 + }
55818 + req->actual = ep->dwc_ep.xfer_count;
55819 + dwc_otg_request_done(ep, req, -ECONNRESET);
55820 + ep->dwc_ep.start_xfer_buff = 0;
55821 + ep->dwc_ep.xfer_buff = 0;
55822 + ep->dwc_ep.xfer_len = 0;
55823 + }
55824 + pcd->ep0state = EP0_IDLE;
55825 + if (doepint.b.setup) {
55826 + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
55827 + /* Data stage started, clear setup */
55828 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55829 + doepint.b.setup = 0;
55830 + handle_ep0(pcd);
55831 + /* Prepare for setup packets if ep0in was enabled*/
55832 + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
55833 + ep0_out_start(core_if, pcd);
55834 + }
55835 +
55836 + goto exit_xfercompl;
55837 + } else {
55838 + /* Prepare for more setup packets */
55839 + DWC_DEBUGPL(DBG_PCDV,
55840 + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
55841 + ep0_out_start(core_if, pcd);
55842 + }
55843 + }
55844 + }
55845 + }
55846 + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
55847 + && core_if->dma_desc_enable == 0) {
55848 + doepint_data_t doepint_temp = {.d32 = 0};
55849 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
55850 + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
55851 + out_ep_regs[ep->dwc_ep.num]->doepint);
55852 + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
55853 + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
55854 + if (pcd->ep0state == EP0_IDLE) {
55855 + if (doepint_temp.b.sr) {
55856 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55857 + }
55858 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55859 + out_ep_regs[0]->doepint);
55860 + if (doeptsize0.b.supcnt == 3) {
55861 + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
55862 + ep->dwc_ep.stp_rollover = 1;
55863 + }
55864 + if (doepint.b.setup) {
55865 +retry:
55866 + /* Already started data stage, clear setup */
55867 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55868 + doepint.b.setup = 0;
55869 + handle_ep0(pcd);
55870 + ep->dwc_ep.stp_rollover = 0;
55871 + /* Prepare for more setup packets */
55872 + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
55873 + pcd->ep0state == EP0_IN_DATA_PHASE) {
55874 + ep0_out_start(core_if, pcd);
55875 + }
55876 + goto exit_xfercompl;
55877 + } else {
55878 + /* Prepare for more setup packets */
55879 + DWC_DEBUGPL(DBG_ANY,
55880 + "EP0_IDLE SR=1 setup=0 new setup comes\n");
55881 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55882 + out_ep_regs[0]->doepint);
55883 + if(doepint.b.setup)
55884 + goto retry;
55885 + ep0_out_start(core_if, pcd);
55886 + }
55887 + } else {
55888 + dwc_otg_pcd_request_t *req;
55889 + diepint_data_t diepint0 = {.d32 = 0};
55890 + doepint_data_t doepint_temp = {.d32 = 0};
55891 + depctl_data_t diepctl0;
55892 + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
55893 + in_ep_regs[0]->diepint);
55894 + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
55895 + in_ep_regs[0]->diepctl);
55896 +
55897 + if (pcd->ep0state == EP0_IN_DATA_PHASE
55898 + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
55899 + if (diepint0.b.xfercompl) {
55900 + DWC_WRITE_REG32(&core_if->dev_if->
55901 + in_ep_regs[0]->diepint, diepint0.d32);
55902 + }
55903 + if (diepctl0.b.epena) {
55904 + diepint_data_t diepint = {.d32 = 0};
55905 + diepctl0.b.snak = 1;
55906 + DWC_WRITE_REG32(&core_if->dev_if->
55907 + in_ep_regs[0]->diepctl, diepctl0.d32);
55908 + do {
55909 + dwc_udelay(10);
55910 + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55911 + in_ep_regs[0]->diepint);
55912 + } while (!diepint.b.inepnakeff);
55913 + diepint.b.inepnakeff = 1;
55914 + DWC_WRITE_REG32(&core_if->dev_if->
55915 + in_ep_regs[0]->diepint, diepint.d32);
55916 + diepctl0.d32 = 0;
55917 + diepctl0.b.epdis = 1;
55918 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
55919 + diepctl0.d32);
55920 + do {
55921 + dwc_udelay(10);
55922 + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55923 + in_ep_regs[0]->diepint);
55924 + } while (!diepint.b.epdisabled);
55925 + diepint.b.epdisabled = 1;
55926 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
55927 + diepint.d32);
55928 + }
55929 + }
55930 + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
55931 + out_ep_regs[ep->dwc_ep.num]->doepint);
55932 + if (doepint_temp.b.sr) {
55933 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55934 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55935 + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
55936 + } else {
55937 + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
55938 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55939 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
55940 + pcd->ep0state == EP0_OUT_DATA_PHASE) {
55941 + /* Read arrived setup packet from req->buf */
55942 + dwc_memcpy(&pcd->setup_pkt->req,
55943 + req->buf + ep->dwc_ep.xfer_count, 8);
55944 + }
55945 + req->actual = ep->dwc_ep.xfer_count;
55946 + dwc_otg_request_done(ep, req, -ECONNRESET);
55947 + ep->dwc_ep.start_xfer_buff = 0;
55948 + ep->dwc_ep.xfer_buff = 0;
55949 + ep->dwc_ep.xfer_len = 0;
55950 + }
55951 + pcd->ep0state = EP0_IDLE;
55952 + if (doepint.b.setup) {
55953 + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
55954 + /* Data stage started, clear setup */
55955 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55956 + doepint.b.setup = 0;
55957 + handle_ep0(pcd);
55958 + /* Prepare for setup packets if ep0in was enabled*/
55959 + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
55960 + ep0_out_start(core_if, pcd);
55961 + }
55962 + goto exit_xfercompl;
55963 + } else {
55964 + /* Prepare for more setup packets */
55965 + DWC_DEBUGPL(DBG_PCDV,
55966 + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
55967 + ep0_out_start(core_if, pcd);
55968 + }
55969 + }
55970 + }
55971 + }
55972 + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
55973 + handle_ep0(pcd);
55974 +exit_xfercompl:
55975 + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
55976 + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
55977 + } else {
55978 + if (core_if->dma_desc_enable == 0
55979 + || pcd->ep0state != EP0_IDLE)
55980 + handle_ep0(pcd);
55981 + }
55982 +#ifdef DWC_EN_ISOC
55983 + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55984 + if (doepint.b.pktdrpsts == 0) {
55985 + /* Clear the bit in DOEPINTn for this interrupt */
55986 + CLEAR_OUT_EP_INTR(core_if,
55987 + epnum,
55988 + xfercompl);
55989 + complete_iso_ep(pcd, ep);
55990 + } else {
55991 +
55992 + doepint_data_t doepint = {.d32 = 0 };
55993 + doepint.b.xfercompl = 1;
55994 + doepint.b.pktdrpsts = 1;
55995 + DWC_WRITE_REG32
55996 + (&core_if->dev_if->out_ep_regs
55997 + [epnum]->doepint,
55998 + doepint.d32);
55999 + if (handle_iso_out_pkt_dropped
56000 + (core_if, dwc_ep)) {
56001 + complete_iso_ep(pcd,
56002 + ep);
56003 + }
56004 + }
56005 +#endif /* DWC_EN_ISOC */
56006 +#ifdef DWC_UTE_PER_IO
56007 + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
56008 + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
56009 + if (!ep->stopped)
56010 + complete_xiso_ep(ep);
56011 +#endif /* DWC_UTE_PER_IO */
56012 + } else {
56013 + /* Clear the bit in DOEPINTn for this interrupt */
56014 + CLEAR_OUT_EP_INTR(core_if, epnum,
56015 + xfercompl);
56016 +
56017 + if (core_if->core_params->dev_out_nak) {
56018 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
56019 + pcd->core_if->ep_xfer_info[epnum].state = 0;
56020 +#ifdef DEBUG
56021 + print_memory_payload(pcd, dwc_ep);
56022 +#endif
56023 + }
56024 + complete_ep(ep);
56025 + }
56026 +
56027 + }
56028 +
56029 + /* Endpoint disable */
56030 + if (doepint.b.epdisabled) {
56031 +
56032 + /* Clear the bit in DOEPINTn for this interrupt */
56033 + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
56034 + if (core_if->core_params->dev_out_nak) {
56035 +#ifdef DEBUG
56036 + print_memory_payload(pcd, dwc_ep);
56037 +#endif
56038 + /* In case of timeout condition */
56039 + if (core_if->ep_xfer_info[epnum].state == 2) {
56040 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
56041 + dev_global_regs->dctl);
56042 + dctl.b.cgoutnak = 1;
56043 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
56044 + dctl.d32);
56045 + /* Unmask goutnakeff interrupt which was masked
56046 + * during handle nak out interrupt */
56047 + gintmsk.b.goutnakeff = 1;
56048 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
56049 + 0, gintmsk.d32);
56050 +
56051 + complete_ep(ep);
56052 + }
56053 + }
56054 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
56055 + {
56056 + dctl_data_t dctl;
56057 + gintmsk_data_t intr_mask = {.d32 = 0};
56058 + dwc_otg_pcd_request_t *req = 0;
56059 +
56060 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
56061 + dev_global_regs->dctl);
56062 + dctl.b.cgoutnak = 1;
56063 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
56064 + dctl.d32);
56065 +
56066 + intr_mask.d32 = 0;
56067 + intr_mask.b.incomplisoout = 1;
56068 +
56069 + /* Get any pending requests */
56070 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
56071 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
56072 + if (!req) {
56073 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
56074 + } else {
56075 + dwc_otg_request_done(ep, req, 0);
56076 + start_next_request(ep);
56077 + }
56078 + } else {
56079 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
56080 + }
56081 + }
56082 + }
56083 + /* AHB Error */
56084 + if (doepint.b.ahberr) {
56085 + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
56086 + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
56087 + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
56088 + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
56089 + }
56090 + /* Setup Phase Done (contorl EPs) */
56091 + if (doepint.b.setup) {
56092 +#ifdef DEBUG_EP0
56093 + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
56094 +#endif
56095 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
56096 +
56097 + handle_ep0(pcd);
56098 + }
56099 +
56100 + /** OUT EP BNA Intr */
56101 + if (doepint.b.bna) {
56102 + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
56103 + if (core_if->dma_desc_enable) {
56104 +#ifdef DWC_EN_ISOC
56105 + if (dwc_ep->type ==
56106 + DWC_OTG_EP_TYPE_ISOC) {
56107 + /*
56108 + * This checking is performed to prevent first "false" BNA
56109 + * handling occuring right after reconnect
56110 + */
56111 + if (dwc_ep->next_frame !=
56112 + 0xffffffff)
56113 + dwc_otg_pcd_handle_iso_bna(ep);
56114 + } else
56115 +#endif /* DWC_EN_ISOC */
56116 + {
56117 + dwc_otg_pcd_handle_noniso_bna(ep);
56118 + }
56119 + }
56120 + }
56121 + /* Babble Interrupt */
56122 + if (doepint.b.babble) {
56123 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
56124 + epnum);
56125 + handle_out_ep_babble_intr(pcd, epnum);
56126 +
56127 + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
56128 + }
56129 + if (doepint.b.outtknepdis) {
56130 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
56131 + disabled\n",epnum);
56132 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
56133 + doepmsk_data_t doepmsk = {.d32 = 0};
56134 + ep->dwc_ep.frame_num = core_if->frame_num;
56135 + if (ep->dwc_ep.bInterval > 1) {
56136 + depctl_data_t depctl;
56137 + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
56138 + out_ep_regs[epnum]->doepctl);
56139 + if (ep->dwc_ep.frame_num & 0x1) {
56140 + depctl.b.setd1pid = 1;
56141 + depctl.b.setd0pid = 0;
56142 + } else {
56143 + depctl.b.setd0pid = 1;
56144 + depctl.b.setd1pid = 0;
56145 + }
56146 + DWC_WRITE_REG32(&core_if->dev_if->
56147 + out_ep_regs[epnum]->doepctl, depctl.d32);
56148 + }
56149 + start_next_request(ep);
56150 + doepmsk.b.outtknepdis = 1;
56151 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
56152 + doepmsk.d32, 0);
56153 + }
56154 + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
56155 + }
56156 +
56157 + /* NAK Interrutp */
56158 + if (doepint.b.nak) {
56159 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
56160 + handle_out_ep_nak_intr(pcd, epnum);
56161 +
56162 + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
56163 + }
56164 + /* NYET Interrutp */
56165 + if (doepint.b.nyet) {
56166 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
56167 + handle_out_ep_nyet_intr(pcd, epnum);
56168 +
56169 + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
56170 + }
56171 + }
56172 +
56173 + epnum++;
56174 + ep_intr >>= 1;
56175 + }
56176 +
56177 + return 1;
56178 +
56179 +#undef CLEAR_OUT_EP_INTR
56180 +}
56181 +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
56182 +{
56183 + int retval = 0;
56184 + if(!frm_overrun && curr_fr >= trgt_fr)
56185 + retval = 1;
56186 + else if (frm_overrun
56187 + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
56188 + retval = 1;
56189 + return retval;
56190 +}
56191 +/**
56192 + * Incomplete ISO IN Transfer Interrupt.
56193 + * This interrupt indicates one of the following conditions occurred
56194 + * while transmitting an ISOC transaction.
56195 + * - Corrupted IN Token for ISOC EP.
56196 + * - Packet not complete in FIFO.
56197 + * The follow actions will be taken:
56198 + * -# Determine the EP
56199 + * -# Set incomplete flag in dwc_ep structure
56200 + * -# Disable EP; when "Endpoint Disabled" interrupt is received
56201 + * Flush FIFO
56202 + */
56203 +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
56204 +{
56205 + gintsts_data_t gintsts;
56206 +
56207 +#ifdef DWC_EN_ISOC
56208 + dwc_otg_dev_if_t *dev_if;
56209 + deptsiz_data_t deptsiz = {.d32 = 0 };
56210 + depctl_data_t depctl = {.d32 = 0 };
56211 + dsts_data_t dsts = {.d32 = 0 };
56212 + dwc_ep_t *dwc_ep;
56213 + int i;
56214 +
56215 + dev_if = GET_CORE_IF(pcd)->dev_if;
56216 +
56217 + for (i = 1; i <= dev_if->num_in_eps; ++i) {
56218 + dwc_ep = &pcd->in_ep[i].dwc_ep;
56219 + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
56220 + deptsiz.d32 =
56221 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
56222 + depctl.d32 =
56223 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56224 +
56225 + if (depctl.b.epdis && deptsiz.d32) {
56226 + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
56227 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
56228 + dwc_ep->cur_pkt = 0;
56229 + dwc_ep->proc_buf_num =
56230 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
56231 +
56232 + if (dwc_ep->proc_buf_num) {
56233 + dwc_ep->cur_pkt_addr =
56234 + dwc_ep->xfer_buff1;
56235 + dwc_ep->cur_pkt_dma_addr =
56236 + dwc_ep->dma_addr1;
56237 + } else {
56238 + dwc_ep->cur_pkt_addr =
56239 + dwc_ep->xfer_buff0;
56240 + dwc_ep->cur_pkt_dma_addr =
56241 + dwc_ep->dma_addr0;
56242 + }
56243 +
56244 + }
56245 +
56246 + dsts.d32 =
56247 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
56248 + dev_global_regs->dsts);
56249 + dwc_ep->next_frame = dsts.b.soffn;
56250 +
56251 + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
56252 + (pcd),
56253 + dwc_ep);
56254 + }
56255 + }
56256 + }
56257 +
56258 +#else
56259 + depctl_data_t depctl = {.d32 = 0 };
56260 + dwc_ep_t *dwc_ep;
56261 + dwc_otg_dev_if_t *dev_if;
56262 + int i;
56263 + dev_if = GET_CORE_IF(pcd)->dev_if;
56264 +
56265 + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
56266 +
56267 + for (i = 1; i <= dev_if->num_in_eps; ++i) {
56268 + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
56269 + depctl.d32 =
56270 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56271 + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
56272 + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
56273 + dwc_ep->frm_overrun))
56274 + {
56275 + depctl.d32 =
56276 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56277 + depctl.b.snak = 1;
56278 + depctl.b.epdis = 1;
56279 + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
56280 + }
56281 + }
56282 + }
56283 +
56284 + /*intr_mask.b.incomplisoin = 1;
56285 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56286 + intr_mask.d32, 0); */
56287 +#endif //DWC_EN_ISOC
56288 +
56289 + /* Clear interrupt */
56290 + gintsts.d32 = 0;
56291 + gintsts.b.incomplisoin = 1;
56292 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56293 + gintsts.d32);
56294 +
56295 + return 1;
56296 +}
56297 +
56298 +/**
56299 + * Incomplete ISO OUT Transfer Interrupt.
56300 + *
56301 + * This interrupt indicates that the core has dropped an ISO OUT
56302 + * packet. The following conditions can be the cause:
56303 + * - FIFO Full, the entire packet would not fit in the FIFO.
56304 + * - CRC Error
56305 + * - Corrupted Token
56306 + * The follow actions will be taken:
56307 + * -# Determine the EP
56308 + * -# Set incomplete flag in dwc_ep structure
56309 + * -# Read any data from the FIFO
56310 + * -# Disable EP. When "Endpoint Disabled" interrupt is received
56311 + * re-enable EP.
56312 + */
56313 +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
56314 +{
56315 +
56316 + gintsts_data_t gintsts;
56317 +
56318 +#ifdef DWC_EN_ISOC
56319 + dwc_otg_dev_if_t *dev_if;
56320 + deptsiz_data_t deptsiz = {.d32 = 0 };
56321 + depctl_data_t depctl = {.d32 = 0 };
56322 + dsts_data_t dsts = {.d32 = 0 };
56323 + dwc_ep_t *dwc_ep;
56324 + int i;
56325 +
56326 + dev_if = GET_CORE_IF(pcd)->dev_if;
56327 +
56328 + for (i = 1; i <= dev_if->num_out_eps; ++i) {
56329 + dwc_ep = &pcd->in_ep[i].dwc_ep;
56330 + if (pcd->out_ep[i].dwc_ep.active &&
56331 + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
56332 + deptsiz.d32 =
56333 + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
56334 + depctl.d32 =
56335 + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
56336 +
56337 + if (depctl.b.epdis && deptsiz.d32) {
56338 + set_current_pkt_info(GET_CORE_IF(pcd),
56339 + &pcd->out_ep[i].dwc_ep);
56340 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
56341 + dwc_ep->cur_pkt = 0;
56342 + dwc_ep->proc_buf_num =
56343 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
56344 +
56345 + if (dwc_ep->proc_buf_num) {
56346 + dwc_ep->cur_pkt_addr =
56347 + dwc_ep->xfer_buff1;
56348 + dwc_ep->cur_pkt_dma_addr =
56349 + dwc_ep->dma_addr1;
56350 + } else {
56351 + dwc_ep->cur_pkt_addr =
56352 + dwc_ep->xfer_buff0;
56353 + dwc_ep->cur_pkt_dma_addr =
56354 + dwc_ep->dma_addr0;
56355 + }
56356 +
56357 + }
56358 +
56359 + dsts.d32 =
56360 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
56361 + dev_global_regs->dsts);
56362 + dwc_ep->next_frame = dsts.b.soffn;
56363 +
56364 + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
56365 + (pcd),
56366 + dwc_ep);
56367 + }
56368 + }
56369 + }
56370 +#else
56371 + /** @todo implement ISR */
56372 + gintmsk_data_t intr_mask = {.d32 = 0 };
56373 + dwc_otg_core_if_t *core_if;
56374 + deptsiz_data_t deptsiz = {.d32 = 0 };
56375 + depctl_data_t depctl = {.d32 = 0 };
56376 + dctl_data_t dctl = {.d32 = 0 };
56377 + dwc_ep_t *dwc_ep = NULL;
56378 + int i;
56379 + core_if = GET_CORE_IF(pcd);
56380 +
56381 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
56382 + dwc_ep = &pcd->out_ep[i].dwc_ep;
56383 + depctl.d32 =
56384 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
56385 + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
56386 + core_if->dev_if->isoc_ep = dwc_ep;
56387 + deptsiz.d32 =
56388 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
56389 + break;
56390 + }
56391 + }
56392 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
56393 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
56394 + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
56395 +
56396 + if (!intr_mask.b.goutnakeff) {
56397 + /* Unmask it */
56398 + intr_mask.b.goutnakeff = 1;
56399 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
56400 + }
56401 + if (!gintsts.b.goutnakeff) {
56402 + dctl.b.sgoutnak = 1;
56403 + }
56404 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
56405 +
56406 + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
56407 + if (depctl.b.epena) {
56408 + depctl.b.epdis = 1;
56409 + depctl.b.snak = 1;
56410 + }
56411 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
56412 +
56413 + intr_mask.d32 = 0;
56414 + intr_mask.b.incomplisoout = 1;
56415 +
56416 +#endif /* DWC_EN_ISOC */
56417 +
56418 + /* Clear interrupt */
56419 + gintsts.d32 = 0;
56420 + gintsts.b.incomplisoout = 1;
56421 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56422 + gintsts.d32);
56423 +
56424 + return 1;
56425 +}
56426 +
56427 +/**
56428 + * This function handles the Global IN NAK Effective interrupt.
56429 + *
56430 + */
56431 +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
56432 +{
56433 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
56434 + depctl_data_t diepctl = {.d32 = 0 };
56435 + gintmsk_data_t intr_mask = {.d32 = 0 };
56436 + gintsts_data_t gintsts;
56437 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
56438 + int i;
56439 +
56440 + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
56441 +
56442 + /* Disable all active IN EPs */
56443 + for (i = 0; i <= dev_if->num_in_eps; i++) {
56444 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56445 + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
56446 + if (core_if->start_predict > 0)
56447 + core_if->start_predict++;
56448 + diepctl.b.epdis = 1;
56449 + diepctl.b.snak = 1;
56450 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
56451 + }
56452 + }
56453 +
56454 +
56455 + /* Disable the Global IN NAK Effective Interrupt */
56456 + intr_mask.b.ginnakeff = 1;
56457 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56458 + intr_mask.d32, 0);
56459 +
56460 + /* Clear interrupt */
56461 + gintsts.d32 = 0;
56462 + gintsts.b.ginnakeff = 1;
56463 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56464 + gintsts.d32);
56465 +
56466 + return 1;
56467 +}
56468 +
56469 +/**
56470 + * OUT NAK Effective.
56471 + *
56472 + */
56473 +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
56474 +{
56475 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
56476 + gintmsk_data_t intr_mask = {.d32 = 0 };
56477 + gintsts_data_t gintsts;
56478 + depctl_data_t doepctl;
56479 + int i;
56480 +
56481 + /* Disable the Global OUT NAK Effective Interrupt */
56482 + intr_mask.b.goutnakeff = 1;
56483 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56484 + intr_mask.d32, 0);
56485 +
56486 + /* If DEV OUT NAK enabled*/
56487 + if (pcd->core_if->core_params->dev_out_nak) {
56488 + /* Run over all out endpoints to determine the ep number on
56489 + * which the timeout has happened
56490 + */
56491 + for (i = 0; i <= dev_if->num_out_eps; i++) {
56492 + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
56493 + break;
56494 + }
56495 + if (i > dev_if->num_out_eps) {
56496 + dctl_data_t dctl;
56497 + dctl.d32 =
56498 + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
56499 + dctl.b.cgoutnak = 1;
56500 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
56501 + dctl.d32);
56502 + goto out;
56503 + }
56504 +
56505 + /* Disable the endpoint */
56506 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
56507 + if (doepctl.b.epena) {
56508 + doepctl.b.epdis = 1;
56509 + doepctl.b.snak = 1;
56510 + }
56511 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
56512 + return 1;
56513 + }
56514 + /* We come here from Incomplete ISO OUT handler */
56515 + if (dev_if->isoc_ep) {
56516 + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
56517 + uint32_t epnum = dwc_ep->num;
56518 + doepint_data_t doepint;
56519 + doepint.d32 =
56520 + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
56521 + dev_if->isoc_ep = NULL;
56522 + doepctl.d32 =
56523 + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
56524 + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
56525 + if (doepctl.b.epena) {
56526 + doepctl.b.epdis = 1;
56527 + doepctl.b.snak = 1;
56528 + }
56529 + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
56530 + doepctl.d32);
56531 + return 1;
56532 + } else
56533 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
56534 + "Global OUT NAK Effective\n");
56535 +
56536 +out:
56537 + /* Clear interrupt */
56538 + gintsts.d32 = 0;
56539 + gintsts.b.goutnakeff = 1;
56540 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56541 + gintsts.d32);
56542 +
56543 + return 1;
56544 +}
56545 +
56546 +/**
56547 + * PCD interrupt handler.
56548 + *
56549 + * The PCD handles the device interrupts. Many conditions can cause a
56550 + * device interrupt. When an interrupt occurs, the device interrupt
56551 + * service routine determines the cause of the interrupt and
56552 + * dispatches handling to the appropriate function. These interrupt
56553 + * handling functions are described below.
56554 + *
56555 + * All interrupt registers are processed from LSB to MSB.
56556 + *
56557 + */
56558 +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
56559 +{
56560 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
56561 +#ifdef VERBOSE
56562 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
56563 +#endif
56564 + gintsts_data_t gintr_status;
56565 + int32_t retval = 0;
56566 +
56567 + /* Exit from ISR if core is hibernated */
56568 + if (core_if->hibernation_suspend == 1) {
56569 + return retval;
56570 + }
56571 +#ifdef VERBOSE
56572 + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
56573 + __func__,
56574 + DWC_READ_REG32(&global_regs->gintsts),
56575 + DWC_READ_REG32(&global_regs->gintmsk));
56576 +#endif
56577 +
56578 + if (dwc_otg_is_device_mode(core_if)) {
56579 + DWC_SPINLOCK(pcd->lock);
56580 +#ifdef VERBOSE
56581 + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
56582 + __func__,
56583 + DWC_READ_REG32(&global_regs->gintsts),
56584 + DWC_READ_REG32(&global_regs->gintmsk));
56585 +#endif
56586 +
56587 + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
56588 +
56589 + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
56590 + __func__, gintr_status.d32);
56591 +
56592 + if (gintr_status.b.sofintr) {
56593 + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
56594 + }
56595 + if (gintr_status.b.rxstsqlvl) {
56596 + retval |=
56597 + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
56598 + }
56599 + if (gintr_status.b.nptxfempty) {
56600 + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
56601 + }
56602 + if (gintr_status.b.goutnakeff) {
56603 + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
56604 + }
56605 + if (gintr_status.b.i2cintr) {
56606 + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
56607 + }
56608 + if (gintr_status.b.erlysuspend) {
56609 + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
56610 + }
56611 + if (gintr_status.b.usbreset) {
56612 + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
56613 + }
56614 + if (gintr_status.b.enumdone) {
56615 + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
56616 + }
56617 + if (gintr_status.b.isooutdrop) {
56618 + retval |=
56619 + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
56620 + (pcd);
56621 + }
56622 + if (gintr_status.b.eopframe) {
56623 + retval |=
56624 + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
56625 + }
56626 + if (gintr_status.b.inepint) {
56627 + if (!core_if->multiproc_int_enable) {
56628 + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
56629 + }
56630 + }
56631 + if (gintr_status.b.outepintr) {
56632 + if (!core_if->multiproc_int_enable) {
56633 + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
56634 + }
56635 + }
56636 + if (gintr_status.b.epmismatch) {
56637 + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
56638 + }
56639 + if (gintr_status.b.fetsusp) {
56640 + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
56641 + }
56642 + if (gintr_status.b.ginnakeff) {
56643 + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
56644 + }
56645 + if (gintr_status.b.incomplisoin) {
56646 + retval |=
56647 + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
56648 + }
56649 + if (gintr_status.b.incomplisoout) {
56650 + retval |=
56651 + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
56652 + }
56653 +
56654 + /* In MPI mode Device Endpoints interrupts are asserted
56655 + * without setting outepintr and inepint bits set, so these
56656 + * Interrupt handlers are called without checking these bit-fields
56657 + */
56658 + if (core_if->multiproc_int_enable) {
56659 + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
56660 + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
56661 + }
56662 +#ifdef VERBOSE
56663 + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
56664 + DWC_READ_REG32(&global_regs->gintsts));
56665 +#endif
56666 + DWC_SPINUNLOCK(pcd->lock);
56667 + }
56668 + return retval;
56669 +}
56670 +
56671 +#endif /* DWC_HOST_ONLY */
56672 --- /dev/null
56673 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
56674 @@ -0,0 +1,1280 @@
56675 + /* ==========================================================================
56676 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
56677 + * $Revision: #21 $
56678 + * $Date: 2012/08/10 $
56679 + * $Change: 2047372 $
56680 + *
56681 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
56682 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
56683 + * otherwise expressly agreed to in writing between Synopsys and you.
56684 + *
56685 + * The Software IS NOT an item of Licensed Software or Licensed Product under
56686 + * any End User Software License Agreement or Agreement for Licensed Product
56687 + * with Synopsys or any supplement thereto. You are permitted to use and
56688 + * redistribute this Software in source and binary forms, with or without
56689 + * modification, provided that redistributions of source code must retain this
56690 + * notice. You may not view, use, disclose, copy or distribute this file or
56691 + * any information contained herein except pursuant to this license grant from
56692 + * Synopsys. If you do not agree with this notice, including the disclaimer
56693 + * below, then you are not authorized to use the Software.
56694 + *
56695 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
56696 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56697 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56698 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
56699 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56700 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56701 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56702 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56703 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56704 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
56705 + * DAMAGE.
56706 + * ========================================================================== */
56707 +#ifndef DWC_HOST_ONLY
56708 +
56709 +/** @file
56710 + * This file implements the Peripheral Controller Driver.
56711 + *
56712 + * The Peripheral Controller Driver (PCD) is responsible for
56713 + * translating requests from the Function Driver into the appropriate
56714 + * actions on the DWC_otg controller. It isolates the Function Driver
56715 + * from the specifics of the controller by providing an API to the
56716 + * Function Driver.
56717 + *
56718 + * The Peripheral Controller Driver for Linux will implement the
56719 + * Gadget API, so that the existing Gadget drivers can be used.
56720 + * (Gadget Driver is the Linux terminology for a Function Driver.)
56721 + *
56722 + * The Linux Gadget API is defined in the header file
56723 + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
56724 + * defined in the structure <code>usb_ep_ops</code> and the USB
56725 + * Controller API is defined in the structure
56726 + * <code>usb_gadget_ops</code>.
56727 + *
56728 + */
56729 +
56730 +#include "dwc_otg_os_dep.h"
56731 +#include "dwc_otg_pcd_if.h"
56732 +#include "dwc_otg_pcd.h"
56733 +#include "dwc_otg_driver.h"
56734 +#include "dwc_otg_dbg.h"
56735 +
56736 +extern bool fiq_enable;
56737 +
56738 +static struct gadget_wrapper {
56739 + dwc_otg_pcd_t *pcd;
56740 +
56741 + struct usb_gadget gadget;
56742 + struct usb_gadget_driver *driver;
56743 +
56744 + struct usb_ep ep0;
56745 + struct usb_ep in_ep[16];
56746 + struct usb_ep out_ep[16];
56747 +
56748 +} *gadget_wrapper;
56749 +
56750 +/* Display the contents of the buffer */
56751 +extern void dump_msg(const u8 * buf, unsigned int length);
56752 +/**
56753 + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
56754 + * if the endpoint is not found
56755 + */
56756 +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
56757 +{
56758 + int i;
56759 + if (pcd->ep0.priv == handle) {
56760 + return &pcd->ep0;
56761 + }
56762 +
56763 + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
56764 + if (pcd->in_ep[i].priv == handle)
56765 + return &pcd->in_ep[i];
56766 + if (pcd->out_ep[i].priv == handle)
56767 + return &pcd->out_ep[i];
56768 + }
56769 +
56770 + return NULL;
56771 +}
56772 +
56773 +/* USB Endpoint Operations */
56774 +/*
56775 + * The following sections briefly describe the behavior of the Gadget
56776 + * API endpoint operations implemented in the DWC_otg driver
56777 + * software. Detailed descriptions of the generic behavior of each of
56778 + * these functions can be found in the Linux header file
56779 + * include/linux/usb_gadget.h.
56780 + *
56781 + * The Gadget API provides wrapper functions for each of the function
56782 + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
56783 + * function, which then calls the underlying PCD function. The
56784 + * following sections are named according to the wrapper
56785 + * functions. Within each section, the corresponding DWC_otg PCD
56786 + * function name is specified.
56787 + *
56788 + */
56789 +
56790 +/**
56791 + * This function is called by the Gadget Driver for each EP to be
56792 + * configured for the current configuration (SET_CONFIGURATION).
56793 + *
56794 + * This function initializes the dwc_otg_ep_t data structure, and then
56795 + * calls dwc_otg_ep_activate.
56796 + */
56797 +static int ep_enable(struct usb_ep *usb_ep,
56798 + const struct usb_endpoint_descriptor *ep_desc)
56799 +{
56800 + int retval;
56801 +
56802 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
56803 +
56804 + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
56805 + DWC_WARN("%s, bad ep or descriptor\n", __func__);
56806 + return -EINVAL;
56807 + }
56808 + if (usb_ep == &gadget_wrapper->ep0) {
56809 + DWC_WARN("%s, bad ep(0)\n", __func__);
56810 + return -EINVAL;
56811 + }
56812 +
56813 + /* Check FIFO size? */
56814 + if (!ep_desc->wMaxPacketSize) {
56815 + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
56816 + return -ERANGE;
56817 + }
56818 +
56819 + if (!gadget_wrapper->driver ||
56820 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56821 + DWC_WARN("%s, bogus device state\n", __func__);
56822 + return -ESHUTDOWN;
56823 + }
56824 +
56825 + /* Delete after check - MAS */
56826 +#if 0
56827 + nat = (uint32_t) ep_desc->wMaxPacketSize;
56828 + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
56829 + nat = (nat >> 11) & 0x03;
56830 + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
56831 +#endif
56832 + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
56833 + (const uint8_t *)ep_desc,
56834 + (void *)usb_ep);
56835 + if (retval) {
56836 + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
56837 + return -EINVAL;
56838 + }
56839 +
56840 + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
56841 +
56842 + return 0;
56843 +}
56844 +
56845 +/**
56846 + * This function is called when an EP is disabled due to disconnect or
56847 + * change in configuration. Any pending requests will terminate with a
56848 + * status of -ESHUTDOWN.
56849 + *
56850 + * This function modifies the dwc_otg_ep_t data structure for this EP,
56851 + * and then calls dwc_otg_ep_deactivate.
56852 + */
56853 +static int ep_disable(struct usb_ep *usb_ep)
56854 +{
56855 + int retval;
56856 +
56857 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
56858 + if (!usb_ep) {
56859 + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
56860 + usb_ep ? usb_ep->name : NULL);
56861 + return -EINVAL;
56862 + }
56863 +
56864 + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
56865 + if (retval) {
56866 + retval = -EINVAL;
56867 + }
56868 +
56869 + return retval;
56870 +}
56871 +
56872 +/**
56873 + * This function allocates a request object to use with the specified
56874 + * endpoint.
56875 + *
56876 + * @param ep The endpoint to be used with with the request
56877 + * @param gfp_flags the GFP_* flags to use.
56878 + */
56879 +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
56880 + gfp_t gfp_flags)
56881 +{
56882 + struct usb_request *usb_req;
56883 +
56884 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
56885 + if (0 == ep) {
56886 + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
56887 + return 0;
56888 + }
56889 + usb_req = kzalloc(sizeof(*usb_req), gfp_flags);
56890 + if (0 == usb_req) {
56891 + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
56892 + return 0;
56893 + }
56894 + usb_req->dma = DWC_DMA_ADDR_INVALID;
56895 +
56896 + return usb_req;
56897 +}
56898 +
56899 +/**
56900 + * This function frees a request object.
56901 + *
56902 + * @param ep The endpoint associated with the request
56903 + * @param req The request being freed
56904 + */
56905 +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
56906 +{
56907 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
56908 +
56909 + if (0 == ep || 0 == req) {
56910 + DWC_WARN("%s() %s\n", __func__,
56911 + "Invalid ep or req argument!\n");
56912 + return;
56913 + }
56914 +
56915 + kfree(req);
56916 +}
56917 +
56918 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56919 +/**
56920 + * This function allocates an I/O buffer to be used for a transfer
56921 + * to/from the specified endpoint.
56922 + *
56923 + * @param usb_ep The endpoint to be used with with the request
56924 + * @param bytes The desired number of bytes for the buffer
56925 + * @param dma Pointer to the buffer's DMA address; must be valid
56926 + * @param gfp_flags the GFP_* flags to use.
56927 + * @return address of a new buffer or null is buffer could not be allocated.
56928 + */
56929 +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
56930 + dma_addr_t * dma, gfp_t gfp_flags)
56931 +{
56932 + void *buf;
56933 + dwc_otg_pcd_t *pcd = 0;
56934 +
56935 + pcd = gadget_wrapper->pcd;
56936 +
56937 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
56938 + dma, gfp_flags);
56939 +
56940 + /* Check dword alignment */
56941 + if ((bytes & 0x3UL) != 0) {
56942 + DWC_WARN("%s() Buffer size is not a multiple of"
56943 + "DWORD size (%d)", __func__, bytes);
56944 + }
56945 +
56946 + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
56947 + WARN_ON(!buf);
56948 +
56949 + /* Check dword alignment */
56950 + if (((int)buf & 0x3UL) != 0) {
56951 + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
56952 + __func__, buf);
56953 + }
56954 +
56955 + return buf;
56956 +}
56957 +
56958 +/**
56959 + * This function frees an I/O buffer that was allocated by alloc_buffer.
56960 + *
56961 + * @param usb_ep the endpoint associated with the buffer
56962 + * @param buf address of the buffer
56963 + * @param dma The buffer's DMA address
56964 + * @param bytes The number of bytes of the buffer
56965 + */
56966 +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
56967 + dma_addr_t dma, unsigned bytes)
56968 +{
56969 + dwc_otg_pcd_t *pcd = 0;
56970 +
56971 + pcd = gadget_wrapper->pcd;
56972 +
56973 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
56974 +
56975 + dma_free_coherent(NULL, bytes, buf, dma);
56976 +}
56977 +#endif
56978 +
56979 +/**
56980 + * This function is used to submit an I/O Request to an EP.
56981 + *
56982 + * - When the request completes the request's completion callback
56983 + * is called to return the request to the driver.
56984 + * - An EP, except control EPs, may have multiple requests
56985 + * pending.
56986 + * - Once submitted the request cannot be examined or modified.
56987 + * - Each request is turned into one or more packets.
56988 + * - A BULK EP can queue any amount of data; the transfer is
56989 + * packetized.
56990 + * - Zero length Packets are specified with the request 'zero'
56991 + * flag.
56992 + */
56993 +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
56994 + gfp_t gfp_flags)
56995 +{
56996 + dwc_otg_pcd_t *pcd;
56997 + struct dwc_otg_pcd_ep *ep = NULL;
56998 + int retval = 0, is_isoc_ep = 0;
56999 + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
57000 +
57001 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
57002 + __func__, usb_ep, usb_req, gfp_flags);
57003 +
57004 + if (!usb_req || !usb_req->complete || !usb_req->buf) {
57005 + DWC_WARN("bad params\n");
57006 + return -EINVAL;
57007 + }
57008 +
57009 + if (!usb_ep) {
57010 + DWC_WARN("bad ep\n");
57011 + return -EINVAL;
57012 + }
57013 +
57014 + pcd = gadget_wrapper->pcd;
57015 + if (!gadget_wrapper->driver ||
57016 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
57017 + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
57018 + gadget_wrapper->gadget.speed);
57019 + DWC_WARN("bogus device state\n");
57020 + return -ESHUTDOWN;
57021 + }
57022 +
57023 + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
57024 + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
57025 +
57026 + usb_req->status = -EINPROGRESS;
57027 + usb_req->actual = 0;
57028 +
57029 + ep = ep_from_handle(pcd, usb_ep);
57030 + if (ep == NULL)
57031 + is_isoc_ep = 0;
57032 + else
57033 + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
57034 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
57035 + dma_addr = usb_req->dma;
57036 +#else
57037 + if (GET_CORE_IF(pcd)->dma_enable) {
57038 + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
57039 + struct device *dev = NULL;
57040 +
57041 + if (otg_dev != NULL)
57042 + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
57043 +
57044 + if (usb_req->length != 0 &&
57045 + usb_req->dma == DWC_DMA_ADDR_INVALID) {
57046 + dma_addr = dma_map_single(dev, usb_req->buf,
57047 + usb_req->length,
57048 + ep->dwc_ep.is_in ?
57049 + DMA_TO_DEVICE:
57050 + DMA_FROM_DEVICE);
57051 + }
57052 + }
57053 +#endif
57054 +
57055 +#ifdef DWC_UTE_PER_IO
57056 + if (is_isoc_ep == 1) {
57057 + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
57058 + usb_req->length, usb_req->zero, usb_req,
57059 + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
57060 + if (retval)
57061 + return -EINVAL;
57062 +
57063 + return 0;
57064 + }
57065 +#endif
57066 + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
57067 + usb_req->length, usb_req->zero, usb_req,
57068 + gfp_flags == GFP_ATOMIC ? 1 : 0);
57069 + if (retval) {
57070 + return -EINVAL;
57071 + }
57072 +
57073 + return 0;
57074 +}
57075 +
57076 +/**
57077 + * This function cancels an I/O request from an EP.
57078 + */
57079 +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
57080 +{
57081 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
57082 +
57083 + if (!usb_ep || !usb_req) {
57084 + DWC_WARN("bad argument\n");
57085 + return -EINVAL;
57086 + }
57087 + if (!gadget_wrapper->driver ||
57088 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
57089 + DWC_WARN("bogus device state\n");
57090 + return -ESHUTDOWN;
57091 + }
57092 + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
57093 + return -EINVAL;
57094 + }
57095 +
57096 + return 0;
57097 +}
57098 +
57099 +/**
57100 + * usb_ep_set_halt stalls an endpoint.
57101 + *
57102 + * usb_ep_clear_halt clears an endpoint halt and resets its data
57103 + * toggle.
57104 + *
57105 + * Both of these functions are implemented with the same underlying
57106 + * function. The behavior depends on the value argument.
57107 + *
57108 + * @param[in] usb_ep the Endpoint to halt or clear halt.
57109 + * @param[in] value
57110 + * - 0 means clear_halt.
57111 + * - 1 means set_halt,
57112 + * - 2 means clear stall lock flag.
57113 + * - 3 means set stall lock flag.
57114 + */
57115 +static int ep_halt(struct usb_ep *usb_ep, int value)
57116 +{
57117 + int retval = 0;
57118 +
57119 + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
57120 +
57121 + if (!usb_ep) {
57122 + DWC_WARN("bad ep\n");
57123 + return -EINVAL;
57124 + }
57125 +
57126 + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
57127 + if (retval == -DWC_E_AGAIN) {
57128 + return -EAGAIN;
57129 + } else if (retval) {
57130 + retval = -EINVAL;
57131 + }
57132 +
57133 + return retval;
57134 +}
57135 +
57136 +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
57137 +#if 0
57138 +/**
57139 + * ep_wedge: sets the halt feature and ignores clear requests
57140 + *
57141 + * @usb_ep: the endpoint being wedged
57142 + *
57143 + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
57144 + * requests. If the gadget driver clears the halt status, it will
57145 + * automatically unwedge the endpoint.
57146 + *
57147 + * Returns zero on success, else negative errno. *
57148 + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
57149 + */
57150 +static int ep_wedge(struct usb_ep *usb_ep)
57151 +{
57152 + int retval = 0;
57153 +
57154 + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
57155 +
57156 + if (!usb_ep) {
57157 + DWC_WARN("bad ep\n");
57158 + return -EINVAL;
57159 + }
57160 +
57161 + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
57162 + if (retval == -DWC_E_AGAIN) {
57163 + retval = -EAGAIN;
57164 + } else if (retval) {
57165 + retval = -EINVAL;
57166 + }
57167 +
57168 + return retval;
57169 +}
57170 +#endif
57171 +
57172 +#ifdef DWC_EN_ISOC
57173 +/**
57174 + * This function is used to submit an ISOC Transfer Request to an EP.
57175 + *
57176 + * - Every time a sync period completes the request's completion callback
57177 + * is called to provide data to the gadget driver.
57178 + * - Once submitted the request cannot be modified.
57179 + * - Each request is turned into periodic data packets untill ISO
57180 + * Transfer is stopped..
57181 + */
57182 +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
57183 + gfp_t gfp_flags)
57184 +{
57185 + int retval = 0;
57186 +
57187 + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
57188 + DWC_WARN("bad params\n");
57189 + return -EINVAL;
57190 + }
57191 +
57192 + if (!usb_ep) {
57193 + DWC_PRINTF("bad params\n");
57194 + return -EINVAL;
57195 + }
57196 +
57197 + req->status = -EINPROGRESS;
57198 +
57199 + retval =
57200 + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
57201 + req->buf1, req->dma0, req->dma1,
57202 + req->sync_frame, req->data_pattern_frame,
57203 + req->data_per_frame,
57204 + req->
57205 + flags & USB_REQ_ISO_ASAP ? -1 :
57206 + req->start_frame, req->buf_proc_intrvl,
57207 + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
57208 +
57209 + if (retval) {
57210 + return -EINVAL;
57211 + }
57212 +
57213 + return retval;
57214 +}
57215 +
57216 +/**
57217 + * This function stops ISO EP Periodic Data Transfer.
57218 + */
57219 +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
57220 +{
57221 + int retval = 0;
57222 + if (!usb_ep) {
57223 + DWC_WARN("bad ep\n");
57224 + }
57225 +
57226 + if (!gadget_wrapper->driver ||
57227 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
57228 + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
57229 + gadget_wrapper->gadget.speed);
57230 + DWC_WARN("bogus device state\n");
57231 + }
57232 +
57233 + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
57234 + if (retval) {
57235 + retval = -EINVAL;
57236 + }
57237 +
57238 + return retval;
57239 +}
57240 +
57241 +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
57242 + int packets, gfp_t gfp_flags)
57243 +{
57244 + struct usb_iso_request *pReq = NULL;
57245 + uint32_t req_size;
57246 +
57247 + req_size = sizeof(struct usb_iso_request);
57248 + req_size +=
57249 + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
57250 +
57251 + pReq = kmalloc(req_size, gfp_flags);
57252 + if (!pReq) {
57253 + DWC_WARN("Can't allocate Iso Request\n");
57254 + return 0;
57255 + }
57256 + pReq->iso_packet_desc0 = (void *)(pReq + 1);
57257 +
57258 + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
57259 +
57260 + return pReq;
57261 +}
57262 +
57263 +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
57264 +{
57265 + kfree(req);
57266 +}
57267 +
57268 +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
57269 + .ep_ops = {
57270 + .enable = ep_enable,
57271 + .disable = ep_disable,
57272 +
57273 + .alloc_request = dwc_otg_pcd_alloc_request,
57274 + .free_request = dwc_otg_pcd_free_request,
57275 +
57276 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
57277 + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
57278 + .free_buffer = dwc_otg_pcd_free_buffer,
57279 +#endif
57280 +
57281 + .queue = ep_queue,
57282 + .dequeue = ep_dequeue,
57283 +
57284 + .set_halt = ep_halt,
57285 + .fifo_status = 0,
57286 + .fifo_flush = 0,
57287 + },
57288 + .iso_ep_start = iso_ep_start,
57289 + .iso_ep_stop = iso_ep_stop,
57290 + .alloc_iso_request = alloc_iso_request,
57291 + .free_iso_request = free_iso_request,
57292 +};
57293 +
57294 +#else
57295 +
57296 + int (*enable) (struct usb_ep *ep,
57297 + const struct usb_endpoint_descriptor *desc);
57298 + int (*disable) (struct usb_ep *ep);
57299 +
57300 + struct usb_request *(*alloc_request) (struct usb_ep *ep,
57301 + gfp_t gfp_flags);
57302 + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
57303 +
57304 + int (*queue) (struct usb_ep *ep, struct usb_request *req,
57305 + gfp_t gfp_flags);
57306 + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
57307 +
57308 + int (*set_halt) (struct usb_ep *ep, int value);
57309 + int (*set_wedge) (struct usb_ep *ep);
57310 +
57311 + int (*fifo_status) (struct usb_ep *ep);
57312 + void (*fifo_flush) (struct usb_ep *ep);
57313 +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
57314 + .enable = ep_enable,
57315 + .disable = ep_disable,
57316 +
57317 + .alloc_request = dwc_otg_pcd_alloc_request,
57318 + .free_request = dwc_otg_pcd_free_request,
57319 +
57320 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
57321 + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
57322 + .free_buffer = dwc_otg_pcd_free_buffer,
57323 +#else
57324 + /* .set_wedge = ep_wedge, */
57325 + .set_wedge = NULL, /* uses set_halt instead */
57326 +#endif
57327 +
57328 + .queue = ep_queue,
57329 + .dequeue = ep_dequeue,
57330 +
57331 + .set_halt = ep_halt,
57332 + .fifo_status = 0,
57333 + .fifo_flush = 0,
57334 +
57335 +};
57336 +
57337 +#endif /* _EN_ISOC_ */
57338 +/* Gadget Operations */
57339 +/**
57340 + * The following gadget operations will be implemented in the DWC_otg
57341 + * PCD. Functions in the API that are not described below are not
57342 + * implemented.
57343 + *
57344 + * The Gadget API provides wrapper functions for each of the function
57345 + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
57346 + * wrapper function, which then calls the underlying PCD function. The
57347 + * following sections are named according to the wrapper functions
57348 + * (except for ioctl, which doesn't have a wrapper function). Within
57349 + * each section, the corresponding DWC_otg PCD function name is
57350 + * specified.
57351 + *
57352 + */
57353 +
57354 +/**
57355 + *Gets the USB Frame number of the last SOF.
57356 + */
57357 +static int get_frame_number(struct usb_gadget *gadget)
57358 +{
57359 + struct gadget_wrapper *d;
57360 +
57361 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
57362 +
57363 + if (gadget == 0) {
57364 + return -ENODEV;
57365 + }
57366 +
57367 + d = container_of(gadget, struct gadget_wrapper, gadget);
57368 + return dwc_otg_pcd_get_frame_number(d->pcd);
57369 +}
57370 +
57371 +#ifdef CONFIG_USB_DWC_OTG_LPM
57372 +static int test_lpm_enabled(struct usb_gadget *gadget)
57373 +{
57374 + struct gadget_wrapper *d;
57375 +
57376 + d = container_of(gadget, struct gadget_wrapper, gadget);
57377 +
57378 + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
57379 +}
57380 +#endif
57381 +
57382 +/**
57383 + * Initiates Session Request Protocol (SRP) to wakeup the host if no
57384 + * session is in progress. If a session is already in progress, but
57385 + * the device is suspended, remote wakeup signaling is started.
57386 + *
57387 + */
57388 +static int wakeup(struct usb_gadget *gadget)
57389 +{
57390 + struct gadget_wrapper *d;
57391 +
57392 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
57393 +
57394 + if (gadget == 0) {
57395 + return -ENODEV;
57396 + } else {
57397 + d = container_of(gadget, struct gadget_wrapper, gadget);
57398 + }
57399 + dwc_otg_pcd_wakeup(d->pcd);
57400 + return 0;
57401 +}
57402 +
57403 +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
57404 + .get_frame = get_frame_number,
57405 + .wakeup = wakeup,
57406 +#ifdef CONFIG_USB_DWC_OTG_LPM
57407 + .lpm_support = test_lpm_enabled,
57408 +#endif
57409 + // current versions must always be self-powered
57410 +};
57411 +
57412 +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
57413 +{
57414 + int retval = -DWC_E_NOT_SUPPORTED;
57415 + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
57416 + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
57417 + (struct usb_ctrlrequest
57418 + *)bytes);
57419 + }
57420 +
57421 + if (retval == -ENOTSUPP) {
57422 + retval = -DWC_E_NOT_SUPPORTED;
57423 + } else if (retval < 0) {
57424 + retval = -DWC_E_INVALID;
57425 + }
57426 +
57427 + return retval;
57428 +}
57429 +
57430 +#ifdef DWC_EN_ISOC
57431 +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57432 + void *req_handle, int proc_buf_num)
57433 +{
57434 + int i, packet_count;
57435 + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
57436 + struct usb_iso_request *iso_req = req_handle;
57437 +
57438 + if (proc_buf_num) {
57439 + iso_packet = iso_req->iso_packet_desc1;
57440 + } else {
57441 + iso_packet = iso_req->iso_packet_desc0;
57442 + }
57443 + packet_count =
57444 + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
57445 + for (i = 0; i < packet_count; ++i) {
57446 + int status;
57447 + int actual;
57448 + int offset;
57449 + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
57450 + i, &status, &actual, &offset);
57451 + switch (status) {
57452 + case -DWC_E_NO_DATA:
57453 + status = -ENODATA;
57454 + break;
57455 + default:
57456 + if (status) {
57457 + DWC_PRINTF("unknown status in isoc packet\n");
57458 + }
57459 +
57460 + }
57461 + iso_packet[i].status = status;
57462 + iso_packet[i].offset = offset;
57463 + iso_packet[i].actual_length = actual;
57464 + }
57465 +
57466 + iso_req->status = 0;
57467 + iso_req->process_buffer(ep_handle, iso_req);
57468 +
57469 + return 0;
57470 +}
57471 +#endif /* DWC_EN_ISOC */
57472 +
57473 +#ifdef DWC_UTE_PER_IO
57474 +/**
57475 + * Copy the contents of the extended request to the Linux usb_request's
57476 + * extended part and call the gadget's completion.
57477 + *
57478 + * @param pcd Pointer to the pcd structure
57479 + * @param ep_handle Void pointer to the usb_ep structure
57480 + * @param req_handle Void pointer to the usb_request structure
57481 + * @param status Request status returned from the portable logic
57482 + * @param ereq_port Void pointer to the extended request structure
57483 + * created in the the portable part that contains the
57484 + * results of the processed iso packets.
57485 + */
57486 +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57487 + void *req_handle, int32_t status, void *ereq_port)
57488 +{
57489 + struct dwc_ute_iso_req_ext *ereqorg = NULL;
57490 + struct dwc_iso_xreq_port *ereqport = NULL;
57491 + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
57492 + int i;
57493 + struct usb_request *req;
57494 + //struct dwc_ute_iso_packet_descriptor *
57495 + //int status = 0;
57496 +
57497 + req = (struct usb_request *)req_handle;
57498 + ereqorg = &req->ext_req;
57499 + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
57500 + desc_org = ereqorg->per_io_frame_descs;
57501 +
57502 + if (req && req->complete) {
57503 + /* Copy the request data from the portable logic to our request */
57504 + for (i = 0; i < ereqport->pio_pkt_count; i++) {
57505 + desc_org[i].actual_length =
57506 + ereqport->per_io_frame_descs[i].actual_length;
57507 + desc_org[i].status =
57508 + ereqport->per_io_frame_descs[i].status;
57509 + }
57510 +
57511 + switch (status) {
57512 + case -DWC_E_SHUTDOWN:
57513 + req->status = -ESHUTDOWN;
57514 + break;
57515 + case -DWC_E_RESTART:
57516 + req->status = -ECONNRESET;
57517 + break;
57518 + case -DWC_E_INVALID:
57519 + req->status = -EINVAL;
57520 + break;
57521 + case -DWC_E_TIMEOUT:
57522 + req->status = -ETIMEDOUT;
57523 + break;
57524 + default:
57525 + req->status = status;
57526 + }
57527 +
57528 + /* And call the gadget's completion */
57529 + req->complete(ep_handle, req);
57530 + }
57531 +
57532 + return 0;
57533 +}
57534 +#endif /* DWC_UTE_PER_IO */
57535 +
57536 +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57537 + void *req_handle, int32_t status, uint32_t actual)
57538 +{
57539 + struct usb_request *req = (struct usb_request *)req_handle;
57540 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
57541 + struct dwc_otg_pcd_ep *ep = NULL;
57542 +#endif
57543 +
57544 + if (req && req->complete) {
57545 + switch (status) {
57546 + case -DWC_E_SHUTDOWN:
57547 + req->status = -ESHUTDOWN;
57548 + break;
57549 + case -DWC_E_RESTART:
57550 + req->status = -ECONNRESET;
57551 + break;
57552 + case -DWC_E_INVALID:
57553 + req->status = -EINVAL;
57554 + break;
57555 + case -DWC_E_TIMEOUT:
57556 + req->status = -ETIMEDOUT;
57557 + break;
57558 + default:
57559 + req->status = status;
57560 +
57561 + }
57562 +
57563 + req->actual = actual;
57564 + DWC_SPINUNLOCK(pcd->lock);
57565 + req->complete(ep_handle, req);
57566 + DWC_SPINLOCK(pcd->lock);
57567 + }
57568 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
57569 + ep = ep_from_handle(pcd, ep_handle);
57570 + if (GET_CORE_IF(pcd)->dma_enable) {
57571 + if (req->length != 0) {
57572 + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
57573 + struct device *dev = NULL;
57574 +
57575 + if (otg_dev != NULL)
57576 + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
57577 +
57578 + dma_unmap_single(dev, req->dma, req->length,
57579 + ep->dwc_ep.is_in ?
57580 + DMA_TO_DEVICE: DMA_FROM_DEVICE);
57581 + }
57582 + }
57583 +#endif
57584 +
57585 + return 0;
57586 +}
57587 +
57588 +static int _connect(dwc_otg_pcd_t * pcd, int speed)
57589 +{
57590 + gadget_wrapper->gadget.speed = speed;
57591 + return 0;
57592 +}
57593 +
57594 +static int _disconnect(dwc_otg_pcd_t * pcd)
57595 +{
57596 + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
57597 + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
57598 + }
57599 + return 0;
57600 +}
57601 +
57602 +static int _resume(dwc_otg_pcd_t * pcd)
57603 +{
57604 + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
57605 + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
57606 + }
57607 +
57608 + return 0;
57609 +}
57610 +
57611 +static int _suspend(dwc_otg_pcd_t * pcd)
57612 +{
57613 + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
57614 + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
57615 + }
57616 + return 0;
57617 +}
57618 +
57619 +/**
57620 + * This function updates the otg values in the gadget structure.
57621 + */
57622 +static int _hnp_changed(dwc_otg_pcd_t * pcd)
57623 +{
57624 +
57625 + if (!gadget_wrapper->gadget.is_otg)
57626 + return 0;
57627 +
57628 + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
57629 + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
57630 + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
57631 + return 0;
57632 +}
57633 +
57634 +static int _reset(dwc_otg_pcd_t * pcd)
57635 +{
57636 + return 0;
57637 +}
57638 +
57639 +#ifdef DWC_UTE_CFI
57640 +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
57641 +{
57642 + int retval = -DWC_E_INVALID;
57643 + if (gadget_wrapper->driver->cfi_feature_setup) {
57644 + retval =
57645 + gadget_wrapper->driver->
57646 + cfi_feature_setup(&gadget_wrapper->gadget,
57647 + (struct cfi_usb_ctrlrequest *)cfi_req);
57648 + }
57649 +
57650 + return retval;
57651 +}
57652 +#endif
57653 +
57654 +static const struct dwc_otg_pcd_function_ops fops = {
57655 + .complete = _complete,
57656 +#ifdef DWC_EN_ISOC
57657 + .isoc_complete = _isoc_complete,
57658 +#endif
57659 + .setup = _setup,
57660 + .disconnect = _disconnect,
57661 + .connect = _connect,
57662 + .resume = _resume,
57663 + .suspend = _suspend,
57664 + .hnp_changed = _hnp_changed,
57665 + .reset = _reset,
57666 +#ifdef DWC_UTE_CFI
57667 + .cfi_setup = _cfi_setup,
57668 +#endif
57669 +#ifdef DWC_UTE_PER_IO
57670 + .xisoc_complete = _xisoc_complete,
57671 +#endif
57672 +};
57673 +
57674 +/**
57675 + * This function is the top level PCD interrupt handler.
57676 + */
57677 +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
57678 +{
57679 + dwc_otg_pcd_t *pcd = dev;
57680 + int32_t retval = IRQ_NONE;
57681 +
57682 + retval = dwc_otg_pcd_handle_intr(pcd);
57683 + if (retval != 0) {
57684 + S3C2410X_CLEAR_EINTPEND();
57685 + }
57686 + return IRQ_RETVAL(retval);
57687 +}
57688 +
57689 +/**
57690 + * This function initialized the usb_ep structures to there default
57691 + * state.
57692 + *
57693 + * @param d Pointer on gadget_wrapper.
57694 + */
57695 +void gadget_add_eps(struct gadget_wrapper *d)
57696 +{
57697 + static const char *names[] = {
57698 +
57699 + "ep0",
57700 + "ep1in",
57701 + "ep2in",
57702 + "ep3in",
57703 + "ep4in",
57704 + "ep5in",
57705 + "ep6in",
57706 + "ep7in",
57707 + "ep8in",
57708 + "ep9in",
57709 + "ep10in",
57710 + "ep11in",
57711 + "ep12in",
57712 + "ep13in",
57713 + "ep14in",
57714 + "ep15in",
57715 + "ep1out",
57716 + "ep2out",
57717 + "ep3out",
57718 + "ep4out",
57719 + "ep5out",
57720 + "ep6out",
57721 + "ep7out",
57722 + "ep8out",
57723 + "ep9out",
57724 + "ep10out",
57725 + "ep11out",
57726 + "ep12out",
57727 + "ep13out",
57728 + "ep14out",
57729 + "ep15out"
57730 + };
57731 +
57732 + int i;
57733 + struct usb_ep *ep;
57734 + int8_t dev_endpoints;
57735 +
57736 + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
57737 +
57738 + INIT_LIST_HEAD(&d->gadget.ep_list);
57739 + d->gadget.ep0 = &d->ep0;
57740 + d->gadget.speed = USB_SPEED_UNKNOWN;
57741 +
57742 + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
57743 +
57744 + /**
57745 + * Initialize the EP0 structure.
57746 + */
57747 + ep = &d->ep0;
57748 +
57749 + /* Init the usb_ep structure. */
57750 + ep->name = names[0];
57751 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57752 +
57753 + /**
57754 + * @todo NGS: What should the max packet size be set to
57755 + * here? Before EP type is set?
57756 + */
57757 + ep->maxpacket = MAX_PACKET_SIZE;
57758 + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
57759 +
57760 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57761 +
57762 + /**
57763 + * Initialize the EP structures.
57764 + */
57765 + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
57766 +
57767 + for (i = 0; i < dev_endpoints; i++) {
57768 + ep = &d->in_ep[i];
57769 +
57770 + /* Init the usb_ep structure. */
57771 + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
57772 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57773 +
57774 + /**
57775 + * @todo NGS: What should the max packet size be set to
57776 + * here? Before EP type is set?
57777 + */
57778 + ep->maxpacket = MAX_PACKET_SIZE;
57779 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57780 + }
57781 +
57782 + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
57783 +
57784 + for (i = 0; i < dev_endpoints; i++) {
57785 + ep = &d->out_ep[i];
57786 +
57787 + /* Init the usb_ep structure. */
57788 + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
57789 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57790 +
57791 + /**
57792 + * @todo NGS: What should the max packet size be set to
57793 + * here? Before EP type is set?
57794 + */
57795 + ep->maxpacket = MAX_PACKET_SIZE;
57796 +
57797 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57798 + }
57799 +
57800 + /* remove ep0 from the list. There is a ep0 pointer. */
57801 + list_del_init(&d->ep0.ep_list);
57802 +
57803 + d->ep0.maxpacket = MAX_EP0_SIZE;
57804 +}
57805 +
57806 +/**
57807 + * This function releases the Gadget device.
57808 + * required by device_unregister().
57809 + *
57810 + * @todo Should this do something? Should it free the PCD?
57811 + */
57812 +static void dwc_otg_pcd_gadget_release(struct device *dev)
57813 +{
57814 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
57815 +}
57816 +
57817 +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
57818 +{
57819 + static char pcd_name[] = "dwc_otg_pcd";
57820 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57821 + struct gadget_wrapper *d;
57822 + int retval;
57823 +
57824 + d = DWC_ALLOC(sizeof(*d));
57825 + if (d == NULL) {
57826 + return NULL;
57827 + }
57828 +
57829 + memset(d, 0, sizeof(*d));
57830 +
57831 + d->gadget.name = pcd_name;
57832 + d->pcd = otg_dev->pcd;
57833 +
57834 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
57835 + strcpy(d->gadget.dev.bus_id, "gadget");
57836 +#else
57837 + dev_set_name(&d->gadget.dev, "%s", "gadget");
57838 +#endif
57839 +
57840 + d->gadget.dev.parent = &_dev->dev;
57841 + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
57842 + d->gadget.ops = &dwc_otg_pcd_ops;
57843 + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
57844 + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
57845 +
57846 + d->driver = 0;
57847 + /* Register the gadget device */
57848 + retval = device_register(&d->gadget.dev);
57849 + if (retval != 0) {
57850 + DWC_ERROR("device_register failed\n");
57851 + DWC_FREE(d);
57852 + return NULL;
57853 + }
57854 +
57855 + return d;
57856 +}
57857 +
57858 +static void free_wrapper(struct gadget_wrapper *d)
57859 +{
57860 + if (d->driver) {
57861 + /* should have been done already by driver model core */
57862 + DWC_WARN("driver '%s' is still registered\n",
57863 + d->driver->driver.name);
57864 +#ifdef CONFIG_USB_GADGET
57865 + usb_gadget_unregister_driver(d->driver);
57866 +#endif
57867 + }
57868 +
57869 + device_unregister(&d->gadget.dev);
57870 + DWC_FREE(d);
57871 +}
57872 +
57873 +/**
57874 + * This function initialized the PCD portion of the driver.
57875 + *
57876 + */
57877 +int pcd_init(dwc_bus_dev_t *_dev)
57878 +{
57879 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57880 + int retval = 0;
57881 +
57882 + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
57883 +
57884 + otg_dev->pcd = dwc_otg_pcd_init(otg_dev);
57885 +
57886 + if (!otg_dev->pcd) {
57887 + DWC_ERROR("dwc_otg_pcd_init failed\n");
57888 + return -ENOMEM;
57889 + }
57890 +
57891 + otg_dev->pcd->otg_dev = otg_dev;
57892 + gadget_wrapper = alloc_wrapper(_dev);
57893 +
57894 + /*
57895 + * Initialize EP structures
57896 + */
57897 + gadget_add_eps(gadget_wrapper);
57898 + /*
57899 + * Setup interupt handler
57900 + */
57901 +#ifdef PLATFORM_INTERFACE
57902 + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
57903 + platform_get_irq(_dev, fiq_enable ? 0 : 1));
57904 + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
57905 + IRQF_SHARED, gadget_wrapper->gadget.name,
57906 + otg_dev->pcd);
57907 + if (retval != 0) {
57908 + DWC_ERROR("request of irq%d failed\n",
57909 + platform_get_irq(_dev, fiq_enable ? 0 : 1));
57910 + free_wrapper(gadget_wrapper);
57911 + return -EBUSY;
57912 + }
57913 +#else
57914 + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
57915 + _dev->irq);
57916 + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
57917 + IRQF_SHARED | IRQF_DISABLED,
57918 + gadget_wrapper->gadget.name, otg_dev->pcd);
57919 + if (retval != 0) {
57920 + DWC_ERROR("request of irq%d failed\n", _dev->irq);
57921 + free_wrapper(gadget_wrapper);
57922 + return -EBUSY;
57923 + }
57924 +#endif
57925 +
57926 + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
57927 +
57928 + return retval;
57929 +}
57930 +
57931 +/**
57932 + * Cleanup the PCD.
57933 + */
57934 +void pcd_remove(dwc_bus_dev_t *_dev)
57935 +{
57936 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57937 + dwc_otg_pcd_t *pcd = otg_dev->pcd;
57938 +
57939 + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
57940 +
57941 + /*
57942 + * Free the IRQ
57943 + */
57944 +#ifdef PLATFORM_INTERFACE
57945 + free_irq(platform_get_irq(_dev, 0), pcd);
57946 +#else
57947 + free_irq(_dev->irq, pcd);
57948 +#endif
57949 + dwc_otg_pcd_remove(otg_dev->pcd);
57950 + free_wrapper(gadget_wrapper);
57951 + otg_dev->pcd = 0;
57952 +}
57953 +
57954 +#endif /* DWC_HOST_ONLY */
57955 --- /dev/null
57956 +++ b/drivers/usb/host/dwc_otg/dwc_otg_regs.h
57957 @@ -0,0 +1,2550 @@
57958 +/* ==========================================================================
57959 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
57960 + * $Revision: #98 $
57961 + * $Date: 2012/08/10 $
57962 + * $Change: 2047372 $
57963 + *
57964 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
57965 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
57966 + * otherwise expressly agreed to in writing between Synopsys and you.
57967 + *
57968 + * The Software IS NOT an item of Licensed Software or Licensed Product under
57969 + * any End User Software License Agreement or Agreement for Licensed Product
57970 + * with Synopsys or any supplement thereto. You are permitted to use and
57971 + * redistribute this Software in source and binary forms, with or without
57972 + * modification, provided that redistributions of source code must retain this
57973 + * notice. You may not view, use, disclose, copy or distribute this file or
57974 + * any information contained herein except pursuant to this license grant from
57975 + * Synopsys. If you do not agree with this notice, including the disclaimer
57976 + * below, then you are not authorized to use the Software.
57977 + *
57978 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
57979 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57980 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57981 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
57982 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57983 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57984 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57985 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57986 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57987 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
57988 + * DAMAGE.
57989 + * ========================================================================== */
57990 +
57991 +#ifndef __DWC_OTG_REGS_H__
57992 +#define __DWC_OTG_REGS_H__
57993 +
57994 +#include "dwc_otg_core_if.h"
57995 +
57996 +/**
57997 + * @file
57998 + *
57999 + * This file contains the data structures for accessing the DWC_otg core registers.
58000 + *
58001 + * The application interfaces with the HS OTG core by reading from and
58002 + * writing to the Control and Status Register (CSR) space through the
58003 + * AHB Slave interface. These registers are 32 bits wide, and the
58004 + * addresses are 32-bit-block aligned.
58005 + * CSRs are classified as follows:
58006 + * - Core Global Registers
58007 + * - Device Mode Registers
58008 + * - Device Global Registers
58009 + * - Device Endpoint Specific Registers
58010 + * - Host Mode Registers
58011 + * - Host Global Registers
58012 + * - Host Port CSRs
58013 + * - Host Channel Specific Registers
58014 + *
58015 + * Only the Core Global registers can be accessed in both Device and
58016 + * Host modes. When the HS OTG core is operating in one mode, either
58017 + * Device or Host, the application must not access registers from the
58018 + * other mode. When the core switches from one mode to another, the
58019 + * registers in the new mode of operation must be reprogrammed as they
58020 + * would be after a power-on reset.
58021 + */
58022 +
58023 +/****************************************************************************/
58024 +/** DWC_otg Core registers .
58025 + * The dwc_otg_core_global_regs structure defines the size
58026 + * and relative field offsets for the Core Global registers.
58027 + */
58028 +typedef struct dwc_otg_core_global_regs {
58029 + /** OTG Control and Status Register. <i>Offset: 000h</i> */
58030 + volatile uint32_t gotgctl;
58031 + /** OTG Interrupt Register. <i>Offset: 004h</i> */
58032 + volatile uint32_t gotgint;
58033 + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
58034 + volatile uint32_t gahbcfg;
58035 +
58036 +#define DWC_GLBINTRMASK 0x0001
58037 +#define DWC_DMAENABLE 0x0020
58038 +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
58039 +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
58040 +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
58041 +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
58042 +
58043 + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
58044 + volatile uint32_t gusbcfg;
58045 + /**Core Reset Register. <i>Offset: 010h</i> */
58046 + volatile uint32_t grstctl;
58047 + /**Core Interrupt Register. <i>Offset: 014h</i> */
58048 + volatile uint32_t gintsts;
58049 + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
58050 + volatile uint32_t gintmsk;
58051 + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
58052 + volatile uint32_t grxstsr;
58053 + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
58054 + volatile uint32_t grxstsp;
58055 + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
58056 + volatile uint32_t grxfsiz;
58057 + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
58058 + volatile uint32_t gnptxfsiz;
58059 + /**Non Periodic Transmit FIFO/Queue Status Register (Read
58060 + * Only). <i>Offset: 02Ch</i> */
58061 + volatile uint32_t gnptxsts;
58062 + /**I2C Access Register. <i>Offset: 030h</i> */
58063 + volatile uint32_t gi2cctl;
58064 + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
58065 + volatile uint32_t gpvndctl;
58066 + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
58067 + volatile uint32_t ggpio;
58068 + /**User ID Register. <i>Offset: 03Ch</i> */
58069 + volatile uint32_t guid;
58070 + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
58071 + volatile uint32_t gsnpsid;
58072 + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
58073 + volatile uint32_t ghwcfg1;
58074 + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
58075 + volatile uint32_t ghwcfg2;
58076 +#define DWC_SLAVE_ONLY_ARCH 0
58077 +#define DWC_EXT_DMA_ARCH 1
58078 +#define DWC_INT_DMA_ARCH 2
58079 +
58080 +#define DWC_MODE_HNP_SRP_CAPABLE 0
58081 +#define DWC_MODE_SRP_ONLY_CAPABLE 1
58082 +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
58083 +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
58084 +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
58085 +#define DWC_MODE_SRP_CAPABLE_HOST 5
58086 +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
58087 +
58088 + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
58089 + volatile uint32_t ghwcfg3;
58090 + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
58091 + volatile uint32_t ghwcfg4;
58092 + /** Core LPM Configuration register <i>Offset: 054h</i>*/
58093 + volatile uint32_t glpmcfg;
58094 + /** Global PowerDn Register <i>Offset: 058h</i> */
58095 + volatile uint32_t gpwrdn;
58096 + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
58097 + volatile uint32_t gdfifocfg;
58098 + /** ADP Control Register <i>Offset: 060h</i> */
58099 + volatile uint32_t adpctl;
58100 + /** Reserved <i>Offset: 064h-0FFh</i> */
58101 + volatile uint32_t reserved39[39];
58102 + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
58103 + volatile uint32_t hptxfsiz;
58104 + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
58105 + otherwise Device Transmit FIFO#n Register.
58106 + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
58107 + volatile uint32_t dtxfsiz[15];
58108 +} dwc_otg_core_global_regs_t;
58109 +
58110 +/**
58111 + * This union represents the bit fields of the Core OTG Control
58112 + * and Status Register (GOTGCTL). Set the bits using the bit
58113 + * fields then write the <i>d32</i> value to the register.
58114 + */
58115 +typedef union gotgctl_data {
58116 + /** raw register data */
58117 + uint32_t d32;
58118 + /** register bits */
58119 + struct {
58120 + unsigned sesreqscs:1;
58121 + unsigned sesreq:1;
58122 + unsigned vbvalidoven:1;
58123 + unsigned vbvalidovval:1;
58124 + unsigned avalidoven:1;
58125 + unsigned avalidovval:1;
58126 + unsigned bvalidoven:1;
58127 + unsigned bvalidovval:1;
58128 + unsigned hstnegscs:1;
58129 + unsigned hnpreq:1;
58130 + unsigned hstsethnpen:1;
58131 + unsigned devhnpen:1;
58132 + unsigned reserved12_15:4;
58133 + unsigned conidsts:1;
58134 + unsigned dbnctime:1;
58135 + unsigned asesvld:1;
58136 + unsigned bsesvld:1;
58137 + unsigned otgver:1;
58138 + unsigned reserved1:1;
58139 + unsigned multvalidbc:5;
58140 + unsigned chirpen:1;
58141 + unsigned reserved28_31:4;
58142 + } b;
58143 +} gotgctl_data_t;
58144 +
58145 +/**
58146 + * This union represents the bit fields of the Core OTG Interrupt Register
58147 + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
58148 + * value to the register.
58149 + */
58150 +typedef union gotgint_data {
58151 + /** raw register data */
58152 + uint32_t d32;
58153 + /** register bits */
58154 + struct {
58155 + /** Current Mode */
58156 + unsigned reserved0_1:2;
58157 +
58158 + /** Session End Detected */
58159 + unsigned sesenddet:1;
58160 +
58161 + unsigned reserved3_7:5;
58162 +
58163 + /** Session Request Success Status Change */
58164 + unsigned sesreqsucstschng:1;
58165 + /** Host Negotiation Success Status Change */
58166 + unsigned hstnegsucstschng:1;
58167 +
58168 + unsigned reserved10_16:7;
58169 +
58170 + /** Host Negotiation Detected */
58171 + unsigned hstnegdet:1;
58172 + /** A-Device Timeout Change */
58173 + unsigned adevtoutchng:1;
58174 + /** Debounce Done */
58175 + unsigned debdone:1;
58176 + /** Multi-Valued input changed */
58177 + unsigned mvic:1;
58178 +
58179 + unsigned reserved31_21:11;
58180 +
58181 + } b;
58182 +} gotgint_data_t;
58183 +
58184 +/**
58185 + * This union represents the bit fields of the Core AHB Configuration
58186 + * Register (GAHBCFG). Set/clear the bits using the bit fields then
58187 + * write the <i>d32</i> value to the register.
58188 + */
58189 +typedef union gahbcfg_data {
58190 + /** raw register data */
58191 + uint32_t d32;
58192 + /** register bits */
58193 + struct {
58194 + unsigned glblintrmsk:1;
58195 +#define DWC_GAHBCFG_GLBINT_ENABLE 1
58196 +
58197 + unsigned hburstlen:4;
58198 +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
58199 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
58200 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
58201 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
58202 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
58203 +
58204 + unsigned dmaenable:1;
58205 +#define DWC_GAHBCFG_DMAENABLE 1
58206 + unsigned reserved:1;
58207 + unsigned nptxfemplvl_txfemplvl:1;
58208 + unsigned ptxfemplvl:1;
58209 +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
58210 +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
58211 + unsigned reserved9_20:12;
58212 + unsigned remmemsupp:1;
58213 + unsigned notialldmawrit:1;
58214 + unsigned ahbsingle:1;
58215 + unsigned reserved24_31:8;
58216 + } b;
58217 +} gahbcfg_data_t;
58218 +
58219 +/**
58220 + * This union represents the bit fields of the Core USB Configuration
58221 + * Register (GUSBCFG). Set the bits using the bit fields then write
58222 + * the <i>d32</i> value to the register.
58223 + */
58224 +typedef union gusbcfg_data {
58225 + /** raw register data */
58226 + uint32_t d32;
58227 + /** register bits */
58228 + struct {
58229 + unsigned toutcal:3;
58230 + unsigned phyif:1;
58231 + unsigned ulpi_utmi_sel:1;
58232 + unsigned fsintf:1;
58233 + unsigned physel:1;
58234 + unsigned ddrsel:1;
58235 + unsigned srpcap:1;
58236 + unsigned hnpcap:1;
58237 + unsigned usbtrdtim:4;
58238 + unsigned reserved1:1;
58239 + unsigned phylpwrclksel:1;
58240 + unsigned otgutmifssel:1;
58241 + unsigned ulpi_fsls:1;
58242 + unsigned ulpi_auto_res:1;
58243 + unsigned ulpi_clk_sus_m:1;
58244 + unsigned ulpi_ext_vbus_drv:1;
58245 + unsigned ulpi_int_vbus_indicator:1;
58246 + unsigned term_sel_dl_pulse:1;
58247 + unsigned indicator_complement:1;
58248 + unsigned indicator_pass_through:1;
58249 + unsigned ulpi_int_prot_dis:1;
58250 + unsigned ic_usb_cap:1;
58251 + unsigned ic_traffic_pull_remove:1;
58252 + unsigned tx_end_delay:1;
58253 + unsigned force_host_mode:1;
58254 + unsigned force_dev_mode:1;
58255 + unsigned reserved31:1;
58256 + } b;
58257 +} gusbcfg_data_t;
58258 +
58259 +/**
58260 + * This union represents the bit fields of the Core Reset Register
58261 + * (GRSTCTL). Set/clear the bits using the bit fields then write the
58262 + * <i>d32</i> value to the register.
58263 + */
58264 +typedef union grstctl_data {
58265 + /** raw register data */
58266 + uint32_t d32;
58267 + /** register bits */
58268 + struct {
58269 + /** Core Soft Reset (CSftRst) (Device and Host)
58270 + *
58271 + * The application can flush the control logic in the
58272 + * entire core using this bit. This bit resets the
58273 + * pipelines in the AHB Clock domain as well as the
58274 + * PHY Clock domain.
58275 + *
58276 + * The state machines are reset to an IDLE state, the
58277 + * control bits in the CSRs are cleared, all the
58278 + * transmit FIFOs and the receive FIFO are flushed.
58279 + *
58280 + * The status mask bits that control the generation of
58281 + * the interrupt, are cleared, to clear the
58282 + * interrupt. The interrupt status bits are not
58283 + * cleared, so the application can get the status of
58284 + * any events that occurred in the core after it has
58285 + * set this bit.
58286 + *
58287 + * Any transactions on the AHB are terminated as soon
58288 + * as possible following the protocol. Any
58289 + * transactions on the USB are terminated immediately.
58290 + *
58291 + * The configuration settings in the CSRs are
58292 + * unchanged, so the software doesn't have to
58293 + * reprogram these registers (Device
58294 + * Configuration/Host Configuration/Core System
58295 + * Configuration/Core PHY Configuration).
58296 + *
58297 + * The application can write to this bit, any time it
58298 + * wants to reset the core. This is a self clearing
58299 + * bit and the core clears this bit after all the
58300 + * necessary logic is reset in the core, which may
58301 + * take several clocks, depending on the current state
58302 + * of the core.
58303 + */
58304 + unsigned csftrst:1;
58305 + /** Hclk Soft Reset
58306 + *
58307 + * The application uses this bit to reset the control logic in
58308 + * the AHB clock domain. Only AHB clock domain pipelines are
58309 + * reset.
58310 + */
58311 + unsigned hsftrst:1;
58312 + /** Host Frame Counter Reset (Host Only)<br>
58313 + *
58314 + * The application can reset the (micro)frame number
58315 + * counter inside the core, using this bit. When the
58316 + * (micro)frame counter is reset, the subsequent SOF
58317 + * sent out by the core, will have a (micro)frame
58318 + * number of 0.
58319 + */
58320 + unsigned hstfrm:1;
58321 + /** In Token Sequence Learning Queue Flush
58322 + * (INTknQFlsh) (Device Only)
58323 + */
58324 + unsigned intknqflsh:1;
58325 + /** RxFIFO Flush (RxFFlsh) (Device and Host)
58326 + *
58327 + * The application can flush the entire Receive FIFO
58328 + * using this bit. The application must first
58329 + * ensure that the core is not in the middle of a
58330 + * transaction. The application should write into
58331 + * this bit, only after making sure that neither the
58332 + * DMA engine is reading from the RxFIFO nor the MAC
58333 + * is writing the data in to the FIFO. The
58334 + * application should wait until the bit is cleared
58335 + * before performing any other operations. This bit
58336 + * will takes 8 clocks (slowest of PHY or AHB clock)
58337 + * to clear.
58338 + */
58339 + unsigned rxfflsh:1;
58340 + /** TxFIFO Flush (TxFFlsh) (Device and Host).
58341 + *
58342 + * This bit is used to selectively flush a single or
58343 + * all transmit FIFOs. The application must first
58344 + * ensure that the core is not in the middle of a
58345 + * transaction. The application should write into
58346 + * this bit, only after making sure that neither the
58347 + * DMA engine is writing into the TxFIFO nor the MAC
58348 + * is reading the data out of the FIFO. The
58349 + * application should wait until the core clears this
58350 + * bit, before performing any operations. This bit
58351 + * will takes 8 clocks (slowest of PHY or AHB clock)
58352 + * to clear.
58353 + */
58354 + unsigned txfflsh:1;
58355 +
58356 + /** TxFIFO Number (TxFNum) (Device and Host).
58357 + *
58358 + * This is the FIFO number which needs to be flushed,
58359 + * using the TxFIFO Flush bit. This field should not
58360 + * be changed until the TxFIFO Flush bit is cleared by
58361 + * the core.
58362 + * - 0x0 : Non Periodic TxFIFO Flush
58363 + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
58364 + * or Periodic TxFIFO in host mode
58365 + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
58366 + * - ...
58367 + * - 0xF : Periodic TxFIFO #15 Flush in device mode
58368 + * - 0x10: Flush all the Transmit NonPeriodic and
58369 + * Transmit Periodic FIFOs in the core
58370 + */
58371 + unsigned txfnum:5;
58372 + /** Reserved */
58373 + unsigned reserved11_29:19;
58374 + /** DMA Request Signal. Indicated DMA request is in
58375 + * probress. Used for debug purpose. */
58376 + unsigned dmareq:1;
58377 + /** AHB Master Idle. Indicates the AHB Master State
58378 + * Machine is in IDLE condition. */
58379 + unsigned ahbidle:1;
58380 + } b;
58381 +} grstctl_t;
58382 +
58383 +/**
58384 + * This union represents the bit fields of the Core Interrupt Mask
58385 + * Register (GINTMSK). Set/clear the bits using the bit fields then
58386 + * write the <i>d32</i> value to the register.
58387 + */
58388 +typedef union gintmsk_data {
58389 + /** raw register data */
58390 + uint32_t d32;
58391 + /** register bits */
58392 + struct {
58393 + unsigned reserved0:1;
58394 + unsigned modemismatch:1;
58395 + unsigned otgintr:1;
58396 + unsigned sofintr:1;
58397 + unsigned rxstsqlvl:1;
58398 + unsigned nptxfempty:1;
58399 + unsigned ginnakeff:1;
58400 + unsigned goutnakeff:1;
58401 + unsigned ulpickint:1;
58402 + unsigned i2cintr:1;
58403 + unsigned erlysuspend:1;
58404 + unsigned usbsuspend:1;
58405 + unsigned usbreset:1;
58406 + unsigned enumdone:1;
58407 + unsigned isooutdrop:1;
58408 + unsigned eopframe:1;
58409 + unsigned restoredone:1;
58410 + unsigned epmismatch:1;
58411 + unsigned inepintr:1;
58412 + unsigned outepintr:1;
58413 + unsigned incomplisoin:1;
58414 + unsigned incomplisoout:1;
58415 + unsigned fetsusp:1;
58416 + unsigned resetdet:1;
58417 + unsigned portintr:1;
58418 + unsigned hcintr:1;
58419 + unsigned ptxfempty:1;
58420 + unsigned lpmtranrcvd:1;
58421 + unsigned conidstschng:1;
58422 + unsigned disconnect:1;
58423 + unsigned sessreqintr:1;
58424 + unsigned wkupintr:1;
58425 + } b;
58426 +} gintmsk_data_t;
58427 +/**
58428 + * This union represents the bit fields of the Core Interrupt Register
58429 + * (GINTSTS). Set/clear the bits using the bit fields then write the
58430 + * <i>d32</i> value to the register.
58431 + */
58432 +typedef union gintsts_data {
58433 + /** raw register data */
58434 + uint32_t d32;
58435 +#define DWC_SOF_INTR_MASK 0x0008
58436 + /** register bits */
58437 + struct {
58438 +#define DWC_HOST_MODE 1
58439 + unsigned curmode:1;
58440 + unsigned modemismatch:1;
58441 + unsigned otgintr:1;
58442 + unsigned sofintr:1;
58443 + unsigned rxstsqlvl:1;
58444 + unsigned nptxfempty:1;
58445 + unsigned ginnakeff:1;
58446 + unsigned goutnakeff:1;
58447 + unsigned ulpickint:1;
58448 + unsigned i2cintr:1;
58449 + unsigned erlysuspend:1;
58450 + unsigned usbsuspend:1;
58451 + unsigned usbreset:1;
58452 + unsigned enumdone:1;
58453 + unsigned isooutdrop:1;
58454 + unsigned eopframe:1;
58455 + unsigned restoredone:1;
58456 + unsigned epmismatch:1;
58457 + unsigned inepint:1;
58458 + unsigned outepintr:1;
58459 + unsigned incomplisoin:1;
58460 + unsigned incomplisoout:1;
58461 + unsigned fetsusp:1;
58462 + unsigned resetdet:1;
58463 + unsigned portintr:1;
58464 + unsigned hcintr:1;
58465 + unsigned ptxfempty:1;
58466 + unsigned lpmtranrcvd:1;
58467 + unsigned conidstschng:1;
58468 + unsigned disconnect:1;
58469 + unsigned sessreqintr:1;
58470 + unsigned wkupintr:1;
58471 + } b;
58472 +} gintsts_data_t;
58473 +
58474 +/**
58475 + * This union represents the bit fields in the Device Receive Status Read and
58476 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
58477 + * element then read out the bits using the <i>b</i>it elements.
58478 + */
58479 +typedef union device_grxsts_data {
58480 + /** raw register data */
58481 + uint32_t d32;
58482 + /** register bits */
58483 + struct {
58484 + unsigned epnum:4;
58485 + unsigned bcnt:11;
58486 + unsigned dpid:2;
58487 +
58488 +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
58489 +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
58490 +
58491 +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
58492 +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
58493 +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
58494 + unsigned pktsts:4;
58495 + unsigned fn:4;
58496 + unsigned reserved25_31:7;
58497 + } b;
58498 +} device_grxsts_data_t;
58499 +
58500 +/**
58501 + * This union represents the bit fields in the Host Receive Status Read and
58502 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
58503 + * element then read out the bits using the <i>b</i>it elements.
58504 + */
58505 +typedef union host_grxsts_data {
58506 + /** raw register data */
58507 + uint32_t d32;
58508 + /** register bits */
58509 + struct {
58510 + unsigned chnum:4;
58511 + unsigned bcnt:11;
58512 + unsigned dpid:2;
58513 +
58514 + unsigned pktsts:4;
58515 +#define DWC_GRXSTS_PKTSTS_IN 0x2
58516 +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
58517 +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
58518 +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
58519 +
58520 + unsigned reserved21_31:11;
58521 + } b;
58522 +} host_grxsts_data_t;
58523 +
58524 +/**
58525 + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
58526 + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
58527 + * then read out the bits using the <i>b</i>it elements.
58528 + */
58529 +typedef union fifosize_data {
58530 + /** raw register data */
58531 + uint32_t d32;
58532 + /** register bits */
58533 + struct {
58534 + unsigned startaddr:16;
58535 + unsigned depth:16;
58536 + } b;
58537 +} fifosize_data_t;
58538 +
58539 +/**
58540 + * This union represents the bit fields in the Non-Periodic Transmit
58541 + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
58542 + * <i>d32</i> element then read out the bits using the <i>b</i>it
58543 + * elements.
58544 + */
58545 +typedef union gnptxsts_data {
58546 + /** raw register data */
58547 + uint32_t d32;
58548 + /** register bits */
58549 + struct {
58550 + unsigned nptxfspcavail:16;
58551 + unsigned nptxqspcavail:8;
58552 + /** Top of the Non-Periodic Transmit Request Queue
58553 + * - bit 24 - Terminate (Last entry for the selected
58554 + * channel/EP)
58555 + * - bits 26:25 - Token Type
58556 + * - 2'b00 - IN/OUT
58557 + * - 2'b01 - Zero Length OUT
58558 + * - 2'b10 - PING/Complete Split
58559 + * - 2'b11 - Channel Halt
58560 + * - bits 30:27 - Channel/EP Number
58561 + */
58562 + unsigned nptxqtop_terminate:1;
58563 + unsigned nptxqtop_token:2;
58564 + unsigned nptxqtop_chnep:4;
58565 + unsigned reserved:1;
58566 + } b;
58567 +} gnptxsts_data_t;
58568 +
58569 +/**
58570 + * This union represents the bit fields in the Transmit
58571 + * FIFO Status Register (DTXFSTS). Read the register into the
58572 + * <i>d32</i> element then read out the bits using the <i>b</i>it
58573 + * elements.
58574 + */
58575 +typedef union dtxfsts_data {
58576 + /** raw register data */
58577 + uint32_t d32;
58578 + /** register bits */
58579 + struct {
58580 + unsigned txfspcavail:16;
58581 + unsigned reserved:16;
58582 + } b;
58583 +} dtxfsts_data_t;
58584 +
58585 +/**
58586 + * This union represents the bit fields in the I2C Control Register
58587 + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
58588 + * bits using the <i>b</i>it elements.
58589 + */
58590 +typedef union gi2cctl_data {
58591 + /** raw register data */
58592 + uint32_t d32;
58593 + /** register bits */
58594 + struct {
58595 + unsigned rwdata:8;
58596 + unsigned regaddr:8;
58597 + unsigned addr:7;
58598 + unsigned i2cen:1;
58599 + unsigned ack:1;
58600 + unsigned i2csuspctl:1;
58601 + unsigned i2cdevaddr:2;
58602 + unsigned i2cdatse0:1;
58603 + unsigned reserved:1;
58604 + unsigned rw:1;
58605 + unsigned bsydne:1;
58606 + } b;
58607 +} gi2cctl_data_t;
58608 +
58609 +/**
58610 + * This union represents the bit fields in the PHY Vendor Control Register
58611 + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
58612 + * bits using the <i>b</i>it elements.
58613 + */
58614 +typedef union gpvndctl_data {
58615 + /** raw register data */
58616 + uint32_t d32;
58617 + /** register bits */
58618 + struct {
58619 + unsigned regdata:8;
58620 + unsigned vctrl:8;
58621 + unsigned regaddr16_21:6;
58622 + unsigned regwr:1;
58623 + unsigned reserved23_24:2;
58624 + unsigned newregreq:1;
58625 + unsigned vstsbsy:1;
58626 + unsigned vstsdone:1;
58627 + unsigned reserved28_30:3;
58628 + unsigned disulpidrvr:1;
58629 + } b;
58630 +} gpvndctl_data_t;
58631 +
58632 +/**
58633 + * This union represents the bit fields in the General Purpose
58634 + * Input/Output Register (GGPIO).
58635 + * Read the register into the <i>d32</i> element then read out the
58636 + * bits using the <i>b</i>it elements.
58637 + */
58638 +typedef union ggpio_data {
58639 + /** raw register data */
58640 + uint32_t d32;
58641 + /** register bits */
58642 + struct {
58643 + unsigned gpi:16;
58644 + unsigned gpo:16;
58645 + } b;
58646 +} ggpio_data_t;
58647 +
58648 +/**
58649 + * This union represents the bit fields in the User ID Register
58650 + * (GUID). Read the register into the <i>d32</i> element then read out the
58651 + * bits using the <i>b</i>it elements.
58652 + */
58653 +typedef union guid_data {
58654 + /** raw register data */
58655 + uint32_t d32;
58656 + /** register bits */
58657 + struct {
58658 + unsigned rwdata:32;
58659 + } b;
58660 +} guid_data_t;
58661 +
58662 +/**
58663 + * This union represents the bit fields in the Synopsys ID Register
58664 + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
58665 + * bits using the <i>b</i>it elements.
58666 + */
58667 +typedef union gsnpsid_data {
58668 + /** raw register data */
58669 + uint32_t d32;
58670 + /** register bits */
58671 + struct {
58672 + unsigned rwdata:32;
58673 + } b;
58674 +} gsnpsid_data_t;
58675 +
58676 +/**
58677 + * This union represents the bit fields in the User HW Config1
58678 + * Register. Read the register into the <i>d32</i> element then read
58679 + * out the bits using the <i>b</i>it elements.
58680 + */
58681 +typedef union hwcfg1_data {
58682 + /** raw register data */
58683 + uint32_t d32;
58684 + /** register bits */
58685 + struct {
58686 + unsigned ep_dir0:2;
58687 + unsigned ep_dir1:2;
58688 + unsigned ep_dir2:2;
58689 + unsigned ep_dir3:2;
58690 + unsigned ep_dir4:2;
58691 + unsigned ep_dir5:2;
58692 + unsigned ep_dir6:2;
58693 + unsigned ep_dir7:2;
58694 + unsigned ep_dir8:2;
58695 + unsigned ep_dir9:2;
58696 + unsigned ep_dir10:2;
58697 + unsigned ep_dir11:2;
58698 + unsigned ep_dir12:2;
58699 + unsigned ep_dir13:2;
58700 + unsigned ep_dir14:2;
58701 + unsigned ep_dir15:2;
58702 + } b;
58703 +} hwcfg1_data_t;
58704 +
58705 +/**
58706 + * This union represents the bit fields in the User HW Config2
58707 + * Register. Read the register into the <i>d32</i> element then read
58708 + * out the bits using the <i>b</i>it elements.
58709 + */
58710 +typedef union hwcfg2_data {
58711 + /** raw register data */
58712 + uint32_t d32;
58713 + /** register bits */
58714 + struct {
58715 + /* GHWCFG2 */
58716 + unsigned op_mode:3;
58717 +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
58718 +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
58719 +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
58720 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
58721 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
58722 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
58723 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
58724 +
58725 + unsigned architecture:2;
58726 + unsigned point2point:1;
58727 + unsigned hs_phy_type:2;
58728 +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
58729 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
58730 +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
58731 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
58732 +
58733 + unsigned fs_phy_type:2;
58734 + unsigned num_dev_ep:4;
58735 + unsigned num_host_chan:4;
58736 + unsigned perio_ep_supported:1;
58737 + unsigned dynamic_fifo:1;
58738 + unsigned multi_proc_int:1;
58739 + unsigned reserved21:1;
58740 + unsigned nonperio_tx_q_depth:2;
58741 + unsigned host_perio_tx_q_depth:2;
58742 + unsigned dev_token_q_depth:5;
58743 + unsigned otg_enable_ic_usb:1;
58744 + } b;
58745 +} hwcfg2_data_t;
58746 +
58747 +/**
58748 + * This union represents the bit fields in the User HW Config3
58749 + * Register. Read the register into the <i>d32</i> element then read
58750 + * out the bits using the <i>b</i>it elements.
58751 + */
58752 +typedef union hwcfg3_data {
58753 + /** raw register data */
58754 + uint32_t d32;
58755 + /** register bits */
58756 + struct {
58757 + /* GHWCFG3 */
58758 + unsigned xfer_size_cntr_width:4;
58759 + unsigned packet_size_cntr_width:3;
58760 + unsigned otg_func:1;
58761 + unsigned i2c:1;
58762 + unsigned vendor_ctrl_if:1;
58763 + unsigned optional_features:1;
58764 + unsigned synch_reset_type:1;
58765 + unsigned adp_supp:1;
58766 + unsigned otg_enable_hsic:1;
58767 + unsigned bc_support:1;
58768 + unsigned otg_lpm_en:1;
58769 + unsigned dfifo_depth:16;
58770 + } b;
58771 +} hwcfg3_data_t;
58772 +
58773 +/**
58774 + * This union represents the bit fields in the User HW Config4
58775 + * Register. Read the register into the <i>d32</i> element then read
58776 + * out the bits using the <i>b</i>it elements.
58777 + */
58778 +typedef union hwcfg4_data {
58779 + /** raw register data */
58780 + uint32_t d32;
58781 + /** register bits */
58782 + struct {
58783 + unsigned num_dev_perio_in_ep:4;
58784 + unsigned power_optimiz:1;
58785 + unsigned min_ahb_freq:1;
58786 + unsigned hiber:1;
58787 + unsigned xhiber:1;
58788 + unsigned reserved:6;
58789 + unsigned utmi_phy_data_width:2;
58790 + unsigned num_dev_mode_ctrl_ep:4;
58791 + unsigned iddig_filt_en:1;
58792 + unsigned vbus_valid_filt_en:1;
58793 + unsigned a_valid_filt_en:1;
58794 + unsigned b_valid_filt_en:1;
58795 + unsigned session_end_filt_en:1;
58796 + unsigned ded_fifo_en:1;
58797 + unsigned num_in_eps:4;
58798 + unsigned desc_dma:1;
58799 + unsigned desc_dma_dyn:1;
58800 + } b;
58801 +} hwcfg4_data_t;
58802 +
58803 +/**
58804 + * This union represents the bit fields of the Core LPM Configuration
58805 + * Register (GLPMCFG). Set the bits using bit fields then write
58806 + * the <i>d32</i> value to the register.
58807 + */
58808 +typedef union glpmctl_data {
58809 + /** raw register data */
58810 + uint32_t d32;
58811 + /** register bits */
58812 + struct {
58813 + /** LPM-Capable (LPMCap) (Device and Host)
58814 + * The application uses this bit to control
58815 + * the DWC_otg core LPM capabilities.
58816 + */
58817 + unsigned lpm_cap_en:1;
58818 + /** LPM response programmed by application (AppL1Res) (Device)
58819 + * Handshake response to LPM token pre-programmed
58820 + * by device application software.
58821 + */
58822 + unsigned appl_resp:1;
58823 + /** Host Initiated Resume Duration (HIRD) (Device and Host)
58824 + * In Host mode this field indicates the value of HIRD
58825 + * to be sent in an LPM transaction.
58826 + * In Device mode this field is updated with the
58827 + * Received LPM Token HIRD bmAttribute
58828 + * when an ACK/NYET/STALL response is sent
58829 + * to an LPM transaction.
58830 + */
58831 + unsigned hird:4;
58832 + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
58833 + * In Host mode this bit indicates the value of remote
58834 + * wake up to be sent in wIndex field of LPM transaction.
58835 + * In Device mode this field is updated with the
58836 + * Received LPM Token bRemoteWake bmAttribute
58837 + * when an ACK/NYET/STALL response is sent
58838 + * to an LPM transaction.
58839 + */
58840 + unsigned rem_wkup_en:1;
58841 + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
58842 + * The application uses this bit to control
58843 + * the utmi_sleep_n assertion to the PHY when in L1 state.
58844 + */
58845 + unsigned en_utmi_sleep:1;
58846 + /** HIRD Threshold (HIRD_Thres) (Device and Host)
58847 + */
58848 + unsigned hird_thres:5;
58849 + /** LPM Response (CoreL1Res) (Device and Host)
58850 + * In Host mode this bit contains handsake response to
58851 + * LPM transaction.
58852 + * In Device mode the response of the core to
58853 + * LPM transaction received is reflected in these two bits.
58854 + - 0x0 : ERROR (No handshake response)
58855 + - 0x1 : STALL
58856 + - 0x2 : NYET
58857 + - 0x3 : ACK
58858 + */
58859 + unsigned lpm_resp:2;
58860 + /** Port Sleep Status (SlpSts) (Device and Host)
58861 + * This bit is set as long as a Sleep condition
58862 + * is present on the USB bus.
58863 + */
58864 + unsigned prt_sleep_sts:1;
58865 + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
58866 + * Indicates that the application or host
58867 + * can start resume from Sleep state.
58868 + */
58869 + unsigned sleep_state_resumeok:1;
58870 + /** LPM channel Index (LPM_Chnl_Indx) (Host)
58871 + * The channel number on which the LPM transaction
58872 + * has to be applied while sending
58873 + * an LPM transaction to the local device.
58874 + */
58875 + unsigned lpm_chan_index:4;
58876 + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
58877 + * Number host retries that would be performed
58878 + * if the device response was not valid response.
58879 + */
58880 + unsigned retry_count:3;
58881 + /** Send LPM Transaction (SndLPM) (Host)
58882 + * When set by application software,
58883 + * an LPM transaction containing two tokens
58884 + * is sent.
58885 + */
58886 + unsigned send_lpm:1;
58887 + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
58888 + * Number of LPM Host Retries still remaining
58889 + * to be transmitted for the current LPM sequence
58890 + */
58891 + unsigned retry_count_sts:3;
58892 + unsigned reserved28_29:2;
58893 + /** In host mode once this bit is set, the host
58894 + * configures to drive the HSIC Idle state on the bus.
58895 + * It then waits for the device to initiate the Connect sequence.
58896 + * In device mode once this bit is set, the device waits for
58897 + * the HSIC Idle line state on the bus. Upon receving the Idle
58898 + * line state, it initiates the HSIC Connect sequence.
58899 + */
58900 + unsigned hsic_connect:1;
58901 + /** This bit overrides and functionally inverts
58902 + * the if_select_hsic input port signal.
58903 + */
58904 + unsigned inv_sel_hsic:1;
58905 + } b;
58906 +} glpmcfg_data_t;
58907 +
58908 +/**
58909 + * This union represents the bit fields of the Core ADP Timer, Control and
58910 + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
58911 + * the <i>d32</i> value to the register.
58912 + */
58913 +typedef union adpctl_data {
58914 + /** raw register data */
58915 + uint32_t d32;
58916 + /** register bits */
58917 + struct {
58918 + /** Probe Discharge (PRB_DSCHG)
58919 + * These bits set the times for TADP_DSCHG.
58920 + * These bits are defined as follows:
58921 + * 2'b00 - 4 msec
58922 + * 2'b01 - 8 msec
58923 + * 2'b10 - 16 msec
58924 + * 2'b11 - 32 msec
58925 + */
58926 + unsigned prb_dschg:2;
58927 + /** Probe Delta (PRB_DELTA)
58928 + * These bits set the resolution for RTIM value.
58929 + * The bits are defined in units of 32 kHz clock cycles as follows:
58930 + * 2'b00 - 1 cycles
58931 + * 2'b01 - 2 cycles
58932 + * 2'b10 - 3 cycles
58933 + * 2'b11 - 4 cycles
58934 + * For example if this value is chosen to 2'b01, it means that RTIM
58935 + * increments for every 3(three) 32Khz clock cycles.
58936 + */
58937 + unsigned prb_delta:2;
58938 + /** Probe Period (PRB_PER)
58939 + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
58940 + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
58941 + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
58942 + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
58943 + * 2'b11 - Reserved
58944 + */
58945 + unsigned prb_per:2;
58946 + /** These bits capture the latest time it took for VBUS to ramp from
58947 + * VADP_SINK to VADP_PRB.
58948 + * 0x000 - 1 cycles
58949 + * 0x001 - 2 cycles
58950 + * 0x002 - 3 cycles
58951 + * etc
58952 + * 0x7FF - 2048 cycles
58953 + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
58954 + */
58955 + unsigned rtim:11;
58956 + /** Enable Probe (EnaPrb)
58957 + * When programmed to 1'b1, the core performs a probe operation.
58958 + * This bit is valid only if OTG_Ver = 1'b1.
58959 + */
58960 + unsigned enaprb:1;
58961 + /** Enable Sense (EnaSns)
58962 + * When programmed to 1'b1, the core performs a Sense operation.
58963 + * This bit is valid only if OTG_Ver = 1'b1.
58964 + */
58965 + unsigned enasns:1;
58966 + /** ADP Reset (ADPRes)
58967 + * When set, ADP controller is reset.
58968 + * This bit is valid only if OTG_Ver = 1'b1.
58969 + */
58970 + unsigned adpres:1;
58971 + /** ADP Enable (ADPEn)
58972 + * When set, the core performs either ADP probing or sensing
58973 + * based on EnaPrb or EnaSns.
58974 + * This bit is valid only if OTG_Ver = 1'b1.
58975 + */
58976 + unsigned adpen:1;
58977 + /** ADP Probe Interrupt (ADP_PRB_INT)
58978 + * When this bit is set, it means that the VBUS
58979 + * voltage is greater than VADP_PRB or VADP_PRB is reached.
58980 + * This bit is valid only if OTG_Ver = 1'b1.
58981 + */
58982 + unsigned adp_prb_int:1;
58983 + /**
58984 + * ADP Sense Interrupt (ADP_SNS_INT)
58985 + * When this bit is set, it means that the VBUS voltage is greater than
58986 + * VADP_SNS value or VADP_SNS is reached.
58987 + * This bit is valid only if OTG_Ver = 1'b1.
58988 + */
58989 + unsigned adp_sns_int:1;
58990 + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
58991 + * This bit is relevant only for an ADP probe.
58992 + * When this bit is set, it means that the ramp time has
58993 + * completed ie ADPCTL.RTIM has reached its terminal value
58994 + * of 0x7FF. This is a debug feature that allows software
58995 + * to read the ramp time after each cycle.
58996 + * This bit is valid only if OTG_Ver = 1'b1.
58997 + */
58998 + unsigned adp_tmout_int:1;
58999 + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
59000 + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
59001 + * This bit is valid only if OTG_Ver = 1'b1.
59002 + */
59003 + unsigned adp_prb_int_msk:1;
59004 + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
59005 + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
59006 + * This bit is valid only if OTG_Ver = 1'b1.
59007 + */
59008 + unsigned adp_sns_int_msk:1;
59009 + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
59010 + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
59011 + * This bit is valid only if OTG_Ver = 1'b1.
59012 + */
59013 + unsigned adp_tmout_int_msk:1;
59014 + /** Access Request
59015 + * 2'b00 - Read/Write Valid (updated by the core)
59016 + * 2'b01 - Read
59017 + * 2'b00 - Write
59018 + * 2'b00 - Reserved
59019 + */
59020 + unsigned ar:2;
59021 + /** Reserved */
59022 + unsigned reserved29_31:3;
59023 + } b;
59024 +} adpctl_data_t;
59025 +
59026 +////////////////////////////////////////////
59027 +// Device Registers
59028 +/**
59029 + * Device Global Registers. <i>Offsets 800h-BFFh</i>
59030 + *
59031 + * The following structures define the size and relative field offsets
59032 + * for the Device Mode Registers.
59033 + *
59034 + * <i>These registers are visible only in Device mode and must not be
59035 + * accessed in Host mode, as the results are unknown.</i>
59036 + */
59037 +typedef struct dwc_otg_dev_global_regs {
59038 + /** Device Configuration Register. <i>Offset 800h</i> */
59039 + volatile uint32_t dcfg;
59040 + /** Device Control Register. <i>Offset: 804h</i> */
59041 + volatile uint32_t dctl;
59042 + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
59043 + volatile uint32_t dsts;
59044 + /** Reserved. <i>Offset: 80Ch</i> */
59045 + uint32_t unused;
59046 + /** Device IN Endpoint Common Interrupt Mask
59047 + * Register. <i>Offset: 810h</i> */
59048 + volatile uint32_t diepmsk;
59049 + /** Device OUT Endpoint Common Interrupt Mask
59050 + * Register. <i>Offset: 814h</i> */
59051 + volatile uint32_t doepmsk;
59052 + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
59053 + volatile uint32_t daint;
59054 + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
59055 + * 81Ch</i> */
59056 + volatile uint32_t daintmsk;
59057 + /** Device IN Token Queue Read Register-1 (Read Only).
59058 + * <i>Offset: 820h</i> */
59059 + volatile uint32_t dtknqr1;
59060 + /** Device IN Token Queue Read Register-2 (Read Only).
59061 + * <i>Offset: 824h</i> */
59062 + volatile uint32_t dtknqr2;
59063 + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
59064 + volatile uint32_t dvbusdis;
59065 + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
59066 + volatile uint32_t dvbuspulse;
59067 + /** Device IN Token Queue Read Register-3 (Read Only). /
59068 + * Device Thresholding control register (Read/Write)
59069 + * <i>Offset: 830h</i> */
59070 + volatile uint32_t dtknqr3_dthrctl;
59071 + /** Device IN Token Queue Read Register-4 (Read Only). /
59072 + * Device IN EPs empty Inr. Mask Register (Read/Write)
59073 + * <i>Offset: 834h</i> */
59074 + volatile uint32_t dtknqr4_fifoemptymsk;
59075 + /** Device Each Endpoint Interrupt Register (Read Only). /
59076 + * <i>Offset: 838h</i> */
59077 + volatile uint32_t deachint;
59078 + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
59079 + * <i>Offset: 83Ch</i> */
59080 + volatile uint32_t deachintmsk;
59081 + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
59082 + * <i>Offset: 840h</i> */
59083 + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
59084 + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
59085 + * <i>Offset: 880h</i> */
59086 + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
59087 +} dwc_otg_device_global_regs_t;
59088 +
59089 +/**
59090 + * This union represents the bit fields in the Device Configuration
59091 + * Register. Read the register into the <i>d32</i> member then
59092 + * set/clear the bits using the <i>b</i>it elements. Write the
59093 + * <i>d32</i> member to the dcfg register.
59094 + */
59095 +typedef union dcfg_data {
59096 + /** raw register data */
59097 + uint32_t d32;
59098 + /** register bits */
59099 + struct {
59100 + /** Device Speed */
59101 + unsigned devspd:2;
59102 + /** Non Zero Length Status OUT Handshake */
59103 + unsigned nzstsouthshk:1;
59104 +#define DWC_DCFG_SEND_STALL 1
59105 +
59106 + unsigned ena32khzs:1;
59107 + /** Device Addresses */
59108 + unsigned devaddr:7;
59109 + /** Periodic Frame Interval */
59110 + unsigned perfrint:2;
59111 +#define DWC_DCFG_FRAME_INTERVAL_80 0
59112 +#define DWC_DCFG_FRAME_INTERVAL_85 1
59113 +#define DWC_DCFG_FRAME_INTERVAL_90 2
59114 +#define DWC_DCFG_FRAME_INTERVAL_95 3
59115 +
59116 + /** Enable Device OUT NAK for bulk in DDMA mode */
59117 + unsigned endevoutnak:1;
59118 +
59119 + unsigned reserved14_17:4;
59120 + /** In Endpoint Mis-match count */
59121 + unsigned epmscnt:5;
59122 + /** Enable Descriptor DMA in Device mode */
59123 + unsigned descdma:1;
59124 + unsigned perschintvl:2;
59125 + unsigned resvalid:6;
59126 + } b;
59127 +} dcfg_data_t;
59128 +
59129 +/**
59130 + * This union represents the bit fields in the Device Control
59131 + * Register. Read the register into the <i>d32</i> member then
59132 + * set/clear the bits using the <i>b</i>it elements.
59133 + */
59134 +typedef union dctl_data {
59135 + /** raw register data */
59136 + uint32_t d32;
59137 + /** register bits */
59138 + struct {
59139 + /** Remote Wakeup */
59140 + unsigned rmtwkupsig:1;
59141 + /** Soft Disconnect */
59142 + unsigned sftdiscon:1;
59143 + /** Global Non-Periodic IN NAK Status */
59144 + unsigned gnpinnaksts:1;
59145 + /** Global OUT NAK Status */
59146 + unsigned goutnaksts:1;
59147 + /** Test Control */
59148 + unsigned tstctl:3;
59149 + /** Set Global Non-Periodic IN NAK */
59150 + unsigned sgnpinnak:1;
59151 + /** Clear Global Non-Periodic IN NAK */
59152 + unsigned cgnpinnak:1;
59153 + /** Set Global OUT NAK */
59154 + unsigned sgoutnak:1;
59155 + /** Clear Global OUT NAK */
59156 + unsigned cgoutnak:1;
59157 + /** Power-On Programming Done */
59158 + unsigned pwronprgdone:1;
59159 + /** Reserved */
59160 + unsigned reserved:1;
59161 + /** Global Multi Count */
59162 + unsigned gmc:2;
59163 + /** Ignore Frame Number for ISOC EPs */
59164 + unsigned ifrmnum:1;
59165 + /** NAK on Babble */
59166 + unsigned nakonbble:1;
59167 + /** Enable Continue on BNA */
59168 + unsigned encontonbna:1;
59169 +
59170 + unsigned reserved18_31:14;
59171 + } b;
59172 +} dctl_data_t;
59173 +
59174 +/**
59175 + * This union represents the bit fields in the Device Status
59176 + * Register. Read the register into the <i>d32</i> member then
59177 + * set/clear the bits using the <i>b</i>it elements.
59178 + */
59179 +typedef union dsts_data {
59180 + /** raw register data */
59181 + uint32_t d32;
59182 + /** register bits */
59183 + struct {
59184 + /** Suspend Status */
59185 + unsigned suspsts:1;
59186 + /** Enumerated Speed */
59187 + unsigned enumspd:2;
59188 +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
59189 +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
59190 +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
59191 +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
59192 + /** Erratic Error */
59193 + unsigned errticerr:1;
59194 + unsigned reserved4_7:4;
59195 + /** Frame or Microframe Number of the received SOF */
59196 + unsigned soffn:14;
59197 + unsigned reserved22_31:10;
59198 + } b;
59199 +} dsts_data_t;
59200 +
59201 +/**
59202 + * This union represents the bit fields in the Device IN EP Interrupt
59203 + * Register and the Device IN EP Common Mask Register.
59204 + *
59205 + * - Read the register into the <i>d32</i> member then set/clear the
59206 + * bits using the <i>b</i>it elements.
59207 + */
59208 +typedef union diepint_data {
59209 + /** raw register data */
59210 + uint32_t d32;
59211 + /** register bits */
59212 + struct {
59213 + /** Transfer complete mask */
59214 + unsigned xfercompl:1;
59215 + /** Endpoint disable mask */
59216 + unsigned epdisabled:1;
59217 + /** AHB Error mask */
59218 + unsigned ahberr:1;
59219 + /** TimeOUT Handshake mask (non-ISOC EPs) */
59220 + unsigned timeout:1;
59221 + /** IN Token received with TxF Empty mask */
59222 + unsigned intktxfemp:1;
59223 + /** IN Token Received with EP mismatch mask */
59224 + unsigned intknepmis:1;
59225 + /** IN Endpoint NAK Effective mask */
59226 + unsigned inepnakeff:1;
59227 + /** Reserved */
59228 + unsigned emptyintr:1;
59229 +
59230 + unsigned txfifoundrn:1;
59231 +
59232 + /** BNA Interrupt mask */
59233 + unsigned bna:1;
59234 +
59235 + unsigned reserved10_12:3;
59236 + /** BNA Interrupt mask */
59237 + unsigned nak:1;
59238 +
59239 + unsigned reserved14_31:18;
59240 + } b;
59241 +} diepint_data_t;
59242 +
59243 +/**
59244 + * This union represents the bit fields in the Device IN EP
59245 + * Common/Dedicated Interrupt Mask Register.
59246 + */
59247 +typedef union diepint_data diepmsk_data_t;
59248 +
59249 +/**
59250 + * This union represents the bit fields in the Device OUT EP Interrupt
59251 + * Registerand Device OUT EP Common Interrupt Mask Register.
59252 + *
59253 + * - Read the register into the <i>d32</i> member then set/clear the
59254 + * bits using the <i>b</i>it elements.
59255 + */
59256 +typedef union doepint_data {
59257 + /** raw register data */
59258 + uint32_t d32;
59259 + /** register bits */
59260 + struct {
59261 + /** Transfer complete */
59262 + unsigned xfercompl:1;
59263 + /** Endpoint disable */
59264 + unsigned epdisabled:1;
59265 + /** AHB Error */
59266 + unsigned ahberr:1;
59267 + /** Setup Phase Done (contorl EPs) */
59268 + unsigned setup:1;
59269 + /** OUT Token Received when Endpoint Disabled */
59270 + unsigned outtknepdis:1;
59271 +
59272 + unsigned stsphsercvd:1;
59273 + /** Back-to-Back SETUP Packets Received */
59274 + unsigned back2backsetup:1;
59275 +
59276 + unsigned reserved7:1;
59277 + /** OUT packet Error */
59278 + unsigned outpkterr:1;
59279 + /** BNA Interrupt */
59280 + unsigned bna:1;
59281 +
59282 + unsigned reserved10:1;
59283 + /** Packet Drop Status */
59284 + unsigned pktdrpsts:1;
59285 + /** Babble Interrupt */
59286 + unsigned babble:1;
59287 + /** NAK Interrupt */
59288 + unsigned nak:1;
59289 + /** NYET Interrupt */
59290 + unsigned nyet:1;
59291 + /** Bit indicating setup packet received */
59292 + unsigned sr:1;
59293 +
59294 + unsigned reserved16_31:16;
59295 + } b;
59296 +} doepint_data_t;
59297 +
59298 +/**
59299 + * This union represents the bit fields in the Device OUT EP
59300 + * Common/Dedicated Interrupt Mask Register.
59301 + */
59302 +typedef union doepint_data doepmsk_data_t;
59303 +
59304 +/**
59305 + * This union represents the bit fields in the Device All EP Interrupt
59306 + * and Mask Registers.
59307 + * - Read the register into the <i>d32</i> member then set/clear the
59308 + * bits using the <i>b</i>it elements.
59309 + */
59310 +typedef union daint_data {
59311 + /** raw register data */
59312 + uint32_t d32;
59313 + /** register bits */
59314 + struct {
59315 + /** IN Endpoint bits */
59316 + unsigned in:16;
59317 + /** OUT Endpoint bits */
59318 + unsigned out:16;
59319 + } ep;
59320 + struct {
59321 + /** IN Endpoint bits */
59322 + unsigned inep0:1;
59323 + unsigned inep1:1;
59324 + unsigned inep2:1;
59325 + unsigned inep3:1;
59326 + unsigned inep4:1;
59327 + unsigned inep5:1;
59328 + unsigned inep6:1;
59329 + unsigned inep7:1;
59330 + unsigned inep8:1;
59331 + unsigned inep9:1;
59332 + unsigned inep10:1;
59333 + unsigned inep11:1;
59334 + unsigned inep12:1;
59335 + unsigned inep13:1;
59336 + unsigned inep14:1;
59337 + unsigned inep15:1;
59338 + /** OUT Endpoint bits */
59339 + unsigned outep0:1;
59340 + unsigned outep1:1;
59341 + unsigned outep2:1;
59342 + unsigned outep3:1;
59343 + unsigned outep4:1;
59344 + unsigned outep5:1;
59345 + unsigned outep6:1;
59346 + unsigned outep7:1;
59347 + unsigned outep8:1;
59348 + unsigned outep9:1;
59349 + unsigned outep10:1;
59350 + unsigned outep11:1;
59351 + unsigned outep12:1;
59352 + unsigned outep13:1;
59353 + unsigned outep14:1;
59354 + unsigned outep15:1;
59355 + } b;
59356 +} daint_data_t;
59357 +
59358 +/**
59359 + * This union represents the bit fields in the Device IN Token Queue
59360 + * Read Registers.
59361 + * - Read the register into the <i>d32</i> member.
59362 + * - READ-ONLY Register
59363 + */
59364 +typedef union dtknq1_data {
59365 + /** raw register data */
59366 + uint32_t d32;
59367 + /** register bits */
59368 + struct {
59369 + /** In Token Queue Write Pointer */
59370 + unsigned intknwptr:5;
59371 + /** Reserved */
59372 + unsigned reserved05_06:2;
59373 + /** write pointer has wrapped. */
59374 + unsigned wrap_bit:1;
59375 + /** EP Numbers of IN Tokens 0 ... 4 */
59376 + unsigned epnums0_5:24;
59377 + } b;
59378 +} dtknq1_data_t;
59379 +
59380 +/**
59381 + * This union represents Threshold control Register
59382 + * - Read and write the register into the <i>d32</i> member.
59383 + * - READ-WRITABLE Register
59384 + */
59385 +typedef union dthrctl_data {
59386 + /** raw register data */
59387 + uint32_t d32;
59388 + /** register bits */
59389 + struct {
59390 + /** non ISO Tx Thr. Enable */
59391 + unsigned non_iso_thr_en:1;
59392 + /** ISO Tx Thr. Enable */
59393 + unsigned iso_thr_en:1;
59394 + /** Tx Thr. Length */
59395 + unsigned tx_thr_len:9;
59396 + /** AHB Threshold ratio */
59397 + unsigned ahb_thr_ratio:2;
59398 + /** Reserved */
59399 + unsigned reserved13_15:3;
59400 + /** Rx Thr. Enable */
59401 + unsigned rx_thr_en:1;
59402 + /** Rx Thr. Length */
59403 + unsigned rx_thr_len:9;
59404 + unsigned reserved26:1;
59405 + /** Arbiter Parking Enable*/
59406 + unsigned arbprken:1;
59407 + /** Reserved */
59408 + unsigned reserved28_31:4;
59409 + } b;
59410 +} dthrctl_data_t;
59411 +
59412 +/**
59413 + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
59414 + * 900h-AFCh</i>
59415 + *
59416 + * There will be one set of endpoint registers per logical endpoint
59417 + * implemented.
59418 + *
59419 + * <i>These registers are visible only in Device mode and must not be
59420 + * accessed in Host mode, as the results are unknown.</i>
59421 + */
59422 +typedef struct dwc_otg_dev_in_ep_regs {
59423 + /** Device IN Endpoint Control Register. <i>Offset:900h +
59424 + * (ep_num * 20h) + 00h</i> */
59425 + volatile uint32_t diepctl;
59426 + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
59427 + uint32_t reserved04;
59428 + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
59429 + * (ep_num * 20h) + 08h</i> */
59430 + volatile uint32_t diepint;
59431 + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
59432 + uint32_t reserved0C;
59433 + /** Device IN Endpoint Transfer Size
59434 + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
59435 + volatile uint32_t dieptsiz;
59436 + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
59437 + * (ep_num * 20h) + 14h</i> */
59438 + volatile uint32_t diepdma;
59439 + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
59440 + * (ep_num * 20h) + 18h</i> */
59441 + volatile uint32_t dtxfsts;
59442 + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
59443 + * (ep_num * 20h) + 1Ch</i> */
59444 + volatile uint32_t diepdmab;
59445 +} dwc_otg_dev_in_ep_regs_t;
59446 +
59447 +/**
59448 + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
59449 + * B00h-CFCh</i>
59450 + *
59451 + * There will be one set of endpoint registers per logical endpoint
59452 + * implemented.
59453 + *
59454 + * <i>These registers are visible only in Device mode and must not be
59455 + * accessed in Host mode, as the results are unknown.</i>
59456 + */
59457 +typedef struct dwc_otg_dev_out_ep_regs {
59458 + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
59459 + * (ep_num * 20h) + 00h</i> */
59460 + volatile uint32_t doepctl;
59461 + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
59462 + uint32_t reserved04;
59463 + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
59464 + * (ep_num * 20h) + 08h</i> */
59465 + volatile uint32_t doepint;
59466 + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
59467 + uint32_t reserved0C;
59468 + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
59469 + * B00h + (ep_num * 20h) + 10h</i> */
59470 + volatile uint32_t doeptsiz;
59471 + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
59472 + * + (ep_num * 20h) + 14h</i> */
59473 + volatile uint32_t doepdma;
59474 + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
59475 + uint32_t unused;
59476 + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
59477 + * + (ep_num * 20h) + 1Ch</i> */
59478 + uint32_t doepdmab;
59479 +} dwc_otg_dev_out_ep_regs_t;
59480 +
59481 +/**
59482 + * This union represents the bit fields in the Device EP Control
59483 + * Register. Read the register into the <i>d32</i> member then
59484 + * set/clear the bits using the <i>b</i>it elements.
59485 + */
59486 +typedef union depctl_data {
59487 + /** raw register data */
59488 + uint32_t d32;
59489 + /** register bits */
59490 + struct {
59491 + /** Maximum Packet Size
59492 + * IN/OUT EPn
59493 + * IN/OUT EP0 - 2 bits
59494 + * 2'b00: 64 Bytes
59495 + * 2'b01: 32
59496 + * 2'b10: 16
59497 + * 2'b11: 8 */
59498 + unsigned mps:11;
59499 +#define DWC_DEP0CTL_MPS_64 0
59500 +#define DWC_DEP0CTL_MPS_32 1
59501 +#define DWC_DEP0CTL_MPS_16 2
59502 +#define DWC_DEP0CTL_MPS_8 3
59503 +
59504 + /** Next Endpoint
59505 + * IN EPn/IN EP0
59506 + * OUT EPn/OUT EP0 - reserved */
59507 + unsigned nextep:4;
59508 +
59509 + /** USB Active Endpoint */
59510 + unsigned usbactep:1;
59511 +
59512 + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
59513 + * This field contains the PID of the packet going to
59514 + * be received or transmitted on this endpoint. The
59515 + * application should program the PID of the first
59516 + * packet going to be received or transmitted on this
59517 + * endpoint , after the endpoint is
59518 + * activated. Application use the SetD1PID and
59519 + * SetD0PID fields of this register to program either
59520 + * D0 or D1 PID.
59521 + *
59522 + * The encoding for this field is
59523 + * - 0: D0
59524 + * - 1: D1
59525 + */
59526 + unsigned dpid:1;
59527 +
59528 + /** NAK Status */
59529 + unsigned naksts:1;
59530 +
59531 + /** Endpoint Type
59532 + * 2'b00: Control
59533 + * 2'b01: Isochronous
59534 + * 2'b10: Bulk
59535 + * 2'b11: Interrupt */
59536 + unsigned eptype:2;
59537 +
59538 + /** Snoop Mode
59539 + * OUT EPn/OUT EP0
59540 + * IN EPn/IN EP0 - reserved */
59541 + unsigned snp:1;
59542 +
59543 + /** Stall Handshake */
59544 + unsigned stall:1;
59545 +
59546 + /** Tx Fifo Number
59547 + * IN EPn/IN EP0
59548 + * OUT EPn/OUT EP0 - reserved */
59549 + unsigned txfnum:4;
59550 +
59551 + /** Clear NAK */
59552 + unsigned cnak:1;
59553 + /** Set NAK */
59554 + unsigned snak:1;
59555 + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
59556 + * Writing to this field sets the Endpoint DPID (DPID)
59557 + * field in this register to DATA0. Set Even
59558 + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
59559 + * Writing to this field sets the Even/Odd
59560 + * (micro)frame (EO_FrNum) field to even (micro)
59561 + * frame.
59562 + */
59563 + unsigned setd0pid:1;
59564 + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
59565 + * Writing to this field sets the Endpoint DPID (DPID)
59566 + * field in this register to DATA1 Set Odd
59567 + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
59568 + * Writing to this field sets the Even/Odd
59569 + * (micro)frame (EO_FrNum) field to odd (micro) frame.
59570 + */
59571 + unsigned setd1pid:1;
59572 +
59573 + /** Endpoint Disable */
59574 + unsigned epdis:1;
59575 + /** Endpoint Enable */
59576 + unsigned epena:1;
59577 + } b;
59578 +} depctl_data_t;
59579 +
59580 +/**
59581 + * This union represents the bit fields in the Device EP Transfer
59582 + * Size Register. Read the register into the <i>d32</i> member then
59583 + * set/clear the bits using the <i>b</i>it elements.
59584 + */
59585 +typedef union deptsiz_data {
59586 + /** raw register data */
59587 + uint32_t d32;
59588 + /** register bits */
59589 + struct {
59590 + /** Transfer size */
59591 + unsigned xfersize:19;
59592 +/** Max packet count for EP (pow(2,10)-1) */
59593 +#define MAX_PKT_CNT 1023
59594 + /** Packet Count */
59595 + unsigned pktcnt:10;
59596 + /** Multi Count - Periodic IN endpoints */
59597 + unsigned mc:2;
59598 + unsigned reserved:1;
59599 + } b;
59600 +} deptsiz_data_t;
59601 +
59602 +/**
59603 + * This union represents the bit fields in the Device EP 0 Transfer
59604 + * Size Register. Read the register into the <i>d32</i> member then
59605 + * set/clear the bits using the <i>b</i>it elements.
59606 + */
59607 +typedef union deptsiz0_data {
59608 + /** raw register data */
59609 + uint32_t d32;
59610 + /** register bits */
59611 + struct {
59612 + /** Transfer size */
59613 + unsigned xfersize:7;
59614 + /** Reserved */
59615 + unsigned reserved7_18:12;
59616 + /** Packet Count */
59617 + unsigned pktcnt:2;
59618 + /** Reserved */
59619 + unsigned reserved21_28:8;
59620 + /**Setup Packet Count (DOEPTSIZ0 Only) */
59621 + unsigned supcnt:2;
59622 + unsigned reserved31;
59623 + } b;
59624 +} deptsiz0_data_t;
59625 +
59626 +/////////////////////////////////////////////////
59627 +// DMA Descriptor Specific Structures
59628 +//
59629 +
59630 +/** Buffer status definitions */
59631 +
59632 +#define BS_HOST_READY 0x0
59633 +#define BS_DMA_BUSY 0x1
59634 +#define BS_DMA_DONE 0x2
59635 +#define BS_HOST_BUSY 0x3
59636 +
59637 +/** Receive/Transmit status definitions */
59638 +
59639 +#define RTS_SUCCESS 0x0
59640 +#define RTS_BUFFLUSH 0x1
59641 +#define RTS_RESERVED 0x2
59642 +#define RTS_BUFERR 0x3
59643 +
59644 +/**
59645 + * This union represents the bit fields in the DMA Descriptor
59646 + * status quadlet. Read the quadlet into the <i>d32</i> member then
59647 + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
59648 + * <i>b_iso_in</i> elements.
59649 + */
59650 +typedef union dev_dma_desc_sts {
59651 + /** raw register data */
59652 + uint32_t d32;
59653 + /** quadlet bits */
59654 + struct {
59655 + /** Received number of bytes */
59656 + unsigned bytes:16;
59657 + /** NAK bit - only for OUT EPs */
59658 + unsigned nak:1;
59659 + unsigned reserved17_22:6;
59660 + /** Multiple Transfer - only for OUT EPs */
59661 + unsigned mtrf:1;
59662 + /** Setup Packet received - only for OUT EPs */
59663 + unsigned sr:1;
59664 + /** Interrupt On Complete */
59665 + unsigned ioc:1;
59666 + /** Short Packet */
59667 + unsigned sp:1;
59668 + /** Last */
59669 + unsigned l:1;
59670 + /** Receive Status */
59671 + unsigned sts:2;
59672 + /** Buffer Status */
59673 + unsigned bs:2;
59674 + } b;
59675 +
59676 +//#ifdef DWC_EN_ISOC
59677 + /** iso out quadlet bits */
59678 + struct {
59679 + /** Received number of bytes */
59680 + unsigned rxbytes:11;
59681 +
59682 + unsigned reserved11:1;
59683 + /** Frame Number */
59684 + unsigned framenum:11;
59685 + /** Received ISO Data PID */
59686 + unsigned pid:2;
59687 + /** Interrupt On Complete */
59688 + unsigned ioc:1;
59689 + /** Short Packet */
59690 + unsigned sp:1;
59691 + /** Last */
59692 + unsigned l:1;
59693 + /** Receive Status */
59694 + unsigned rxsts:2;
59695 + /** Buffer Status */
59696 + unsigned bs:2;
59697 + } b_iso_out;
59698 +
59699 + /** iso in quadlet bits */
59700 + struct {
59701 + /** Transmited number of bytes */
59702 + unsigned txbytes:12;
59703 + /** Frame Number */
59704 + unsigned framenum:11;
59705 + /** Transmited ISO Data PID */
59706 + unsigned pid:2;
59707 + /** Interrupt On Complete */
59708 + unsigned ioc:1;
59709 + /** Short Packet */
59710 + unsigned sp:1;
59711 + /** Last */
59712 + unsigned l:1;
59713 + /** Transmit Status */
59714 + unsigned txsts:2;
59715 + /** Buffer Status */
59716 + unsigned bs:2;
59717 + } b_iso_in;
59718 +//#endif /* DWC_EN_ISOC */
59719 +} dev_dma_desc_sts_t;
59720 +
59721 +/**
59722 + * DMA Descriptor structure
59723 + *
59724 + * DMA Descriptor structure contains two quadlets:
59725 + * Status quadlet and Data buffer pointer.
59726 + */
59727 +typedef struct dwc_otg_dev_dma_desc {
59728 + /** DMA Descriptor status quadlet */
59729 + dev_dma_desc_sts_t status;
59730 + /** DMA Descriptor data buffer pointer */
59731 + uint32_t buf;
59732 +} dwc_otg_dev_dma_desc_t;
59733 +
59734 +/**
59735 + * The dwc_otg_dev_if structure contains information needed to manage
59736 + * the DWC_otg controller acting in device mode. It represents the
59737 + * programming view of the device-specific aspects of the controller.
59738 + */
59739 +typedef struct dwc_otg_dev_if {
59740 + /** Pointer to device Global registers.
59741 + * Device Global Registers starting at offset 800h
59742 + */
59743 + dwc_otg_device_global_regs_t *dev_global_regs;
59744 +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
59745 +
59746 + /**
59747 + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
59748 + */
59749 + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
59750 +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
59751 +#define DWC_EP_REG_OFFSET 0x20
59752 +
59753 + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
59754 + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
59755 +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
59756 +
59757 + /* Device configuration information */
59758 + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
59759 + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
59760 + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
59761 +
59762 + /** Size of periodic FIFOs (Bytes) */
59763 + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
59764 +
59765 + /** Size of Tx FIFOs (Bytes) */
59766 + uint16_t tx_fifo_size[MAX_TX_FIFOS];
59767 +
59768 + /** Thresholding enable flags and length varaiables **/
59769 + uint16_t rx_thr_en;
59770 + uint16_t iso_tx_thr_en;
59771 + uint16_t non_iso_tx_thr_en;
59772 +
59773 + uint16_t rx_thr_length;
59774 + uint16_t tx_thr_length;
59775 +
59776 + /**
59777 + * Pointers to the DMA Descriptors for EP0 Control
59778 + * transfers (virtual and physical)
59779 + */
59780 +
59781 + /** 2 descriptors for SETUP packets */
59782 + dwc_dma_t dma_setup_desc_addr[2];
59783 + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
59784 +
59785 + /** Pointer to Descriptor with latest SETUP packet */
59786 + dwc_otg_dev_dma_desc_t *psetup;
59787 +
59788 + /** Index of current SETUP handler descriptor */
59789 + uint32_t setup_desc_index;
59790 +
59791 + /** Descriptor for Data In or Status In phases */
59792 + dwc_dma_t dma_in_desc_addr;
59793 + dwc_otg_dev_dma_desc_t *in_desc_addr;
59794 +
59795 + /** Descriptor for Data Out or Status Out phases */
59796 + dwc_dma_t dma_out_desc_addr;
59797 + dwc_otg_dev_dma_desc_t *out_desc_addr;
59798 +
59799 + /** Setup Packet Detected - if set clear NAK when queueing */
59800 + uint32_t spd;
59801 + /** Isoc ep pointer on which incomplete happens */
59802 + void *isoc_ep;
59803 +
59804 +} dwc_otg_dev_if_t;
59805 +
59806 +/////////////////////////////////////////////////
59807 +// Host Mode Register Structures
59808 +//
59809 +/**
59810 + * The Host Global Registers structure defines the size and relative
59811 + * field offsets for the Host Mode Global Registers. Host Global
59812 + * Registers offsets 400h-7FFh.
59813 +*/
59814 +typedef struct dwc_otg_host_global_regs {
59815 + /** Host Configuration Register. <i>Offset: 400h</i> */
59816 + volatile uint32_t hcfg;
59817 + /** Host Frame Interval Register. <i>Offset: 404h</i> */
59818 + volatile uint32_t hfir;
59819 + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
59820 + volatile uint32_t hfnum;
59821 + /** Reserved. <i>Offset: 40Ch</i> */
59822 + uint32_t reserved40C;
59823 + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
59824 + volatile uint32_t hptxsts;
59825 + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
59826 + volatile uint32_t haint;
59827 + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
59828 + volatile uint32_t haintmsk;
59829 + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
59830 + volatile uint32_t hflbaddr;
59831 +} dwc_otg_host_global_regs_t;
59832 +
59833 +/**
59834 + * This union represents the bit fields in the Host Configuration Register.
59835 + * Read the register into the <i>d32</i> member then set/clear the bits using
59836 + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
59837 + */
59838 +typedef union hcfg_data {
59839 + /** raw register data */
59840 + uint32_t d32;
59841 +
59842 + /** register bits */
59843 + struct {
59844 + /** FS/LS Phy Clock Select */
59845 + unsigned fslspclksel:2;
59846 +#define DWC_HCFG_30_60_MHZ 0
59847 +#define DWC_HCFG_48_MHZ 1
59848 +#define DWC_HCFG_6_MHZ 2
59849 +
59850 + /** FS/LS Only Support */
59851 + unsigned fslssupp:1;
59852 + unsigned reserved3_6:4;
59853 + /** Enable 32-KHz Suspend Mode */
59854 + unsigned ena32khzs:1;
59855 + /** Resume Validation Periiod */
59856 + unsigned resvalid:8;
59857 + unsigned reserved16_22:7;
59858 + /** Enable Scatter/gather DMA in Host mode */
59859 + unsigned descdma:1;
59860 + /** Frame List Entries */
59861 + unsigned frlisten:2;
59862 + /** Enable Periodic Scheduling */
59863 + unsigned perschedena:1;
59864 + unsigned reserved27_30:4;
59865 + unsigned modechtimen:1;
59866 + } b;
59867 +} hcfg_data_t;
59868 +
59869 +/**
59870 + * This union represents the bit fields in the Host Frame Remaing/Number
59871 + * Register.
59872 + */
59873 +typedef union hfir_data {
59874 + /** raw register data */
59875 + uint32_t d32;
59876 +
59877 + /** register bits */
59878 + struct {
59879 + unsigned frint:16;
59880 + unsigned hfirrldctrl:1;
59881 + unsigned reserved:15;
59882 + } b;
59883 +} hfir_data_t;
59884 +
59885 +/**
59886 + * This union represents the bit fields in the Host Frame Remaing/Number
59887 + * Register.
59888 + */
59889 +typedef union hfnum_data {
59890 + /** raw register data */
59891 + uint32_t d32;
59892 +
59893 + /** register bits */
59894 + struct {
59895 + unsigned frnum:16;
59896 +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
59897 + unsigned frrem:16;
59898 + } b;
59899 +} hfnum_data_t;
59900 +
59901 +typedef union hptxsts_data {
59902 + /** raw register data */
59903 + uint32_t d32;
59904 +
59905 + /** register bits */
59906 + struct {
59907 + unsigned ptxfspcavail:16;
59908 + unsigned ptxqspcavail:8;
59909 + /** Top of the Periodic Transmit Request Queue
59910 + * - bit 24 - Terminate (last entry for the selected channel)
59911 + * - bits 26:25 - Token Type
59912 + * - 2'b00 - Zero length
59913 + * - 2'b01 - Ping
59914 + * - 2'b10 - Disable
59915 + * - bits 30:27 - Channel Number
59916 + * - bit 31 - Odd/even microframe
59917 + */
59918 + unsigned ptxqtop_terminate:1;
59919 + unsigned ptxqtop_token:2;
59920 + unsigned ptxqtop_chnum:4;
59921 + unsigned ptxqtop_odd:1;
59922 + } b;
59923 +} hptxsts_data_t;
59924 +
59925 +/**
59926 + * This union represents the bit fields in the Host Port Control and Status
59927 + * Register. Read the register into the <i>d32</i> member then set/clear the
59928 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59929 + * hprt0 register.
59930 + */
59931 +typedef union hprt0_data {
59932 + /** raw register data */
59933 + uint32_t d32;
59934 + /** register bits */
59935 + struct {
59936 + unsigned prtconnsts:1;
59937 + unsigned prtconndet:1;
59938 + unsigned prtena:1;
59939 + unsigned prtenchng:1;
59940 + unsigned prtovrcurract:1;
59941 + unsigned prtovrcurrchng:1;
59942 + unsigned prtres:1;
59943 + unsigned prtsusp:1;
59944 + unsigned prtrst:1;
59945 + unsigned reserved9:1;
59946 + unsigned prtlnsts:2;
59947 + unsigned prtpwr:1;
59948 + unsigned prttstctl:4;
59949 + unsigned prtspd:2;
59950 +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
59951 +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
59952 +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
59953 + unsigned reserved19_31:13;
59954 + } b;
59955 +} hprt0_data_t;
59956 +
59957 +/**
59958 + * This union represents the bit fields in the Host All Interrupt
59959 + * Register.
59960 + */
59961 +typedef union haint_data {
59962 + /** raw register data */
59963 + uint32_t d32;
59964 + /** register bits */
59965 + struct {
59966 + unsigned ch0:1;
59967 + unsigned ch1:1;
59968 + unsigned ch2:1;
59969 + unsigned ch3:1;
59970 + unsigned ch4:1;
59971 + unsigned ch5:1;
59972 + unsigned ch6:1;
59973 + unsigned ch7:1;
59974 + unsigned ch8:1;
59975 + unsigned ch9:1;
59976 + unsigned ch10:1;
59977 + unsigned ch11:1;
59978 + unsigned ch12:1;
59979 + unsigned ch13:1;
59980 + unsigned ch14:1;
59981 + unsigned ch15:1;
59982 + unsigned reserved:16;
59983 + } b;
59984 +
59985 + struct {
59986 + unsigned chint:16;
59987 + unsigned reserved:16;
59988 + } b2;
59989 +} haint_data_t;
59990 +
59991 +/**
59992 + * This union represents the bit fields in the Host All Interrupt
59993 + * Register.
59994 + */
59995 +typedef union haintmsk_data {
59996 + /** raw register data */
59997 + uint32_t d32;
59998 + /** register bits */
59999 + struct {
60000 + unsigned ch0:1;
60001 + unsigned ch1:1;
60002 + unsigned ch2:1;
60003 + unsigned ch3:1;
60004 + unsigned ch4:1;
60005 + unsigned ch5:1;
60006 + unsigned ch6:1;
60007 + unsigned ch7:1;
60008 + unsigned ch8:1;
60009 + unsigned ch9:1;
60010 + unsigned ch10:1;
60011 + unsigned ch11:1;
60012 + unsigned ch12:1;
60013 + unsigned ch13:1;
60014 + unsigned ch14:1;
60015 + unsigned ch15:1;
60016 + unsigned reserved:16;
60017 + } b;
60018 +
60019 + struct {
60020 + unsigned chint:16;
60021 + unsigned reserved:16;
60022 + } b2;
60023 +} haintmsk_data_t;
60024 +
60025 +/**
60026 + * Host Channel Specific Registers. <i>500h-5FCh</i>
60027 + */
60028 +typedef struct dwc_otg_hc_regs {
60029 + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
60030 + volatile uint32_t hcchar;
60031 + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
60032 + volatile uint32_t hcsplt;
60033 + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
60034 + volatile uint32_t hcint;
60035 + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
60036 + volatile uint32_t hcintmsk;
60037 + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
60038 + volatile uint32_t hctsiz;
60039 + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
60040 + volatile uint32_t hcdma;
60041 + volatile uint32_t reserved;
60042 + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
60043 + volatile uint32_t hcdmab;
60044 +} dwc_otg_hc_regs_t;
60045 +
60046 +/**
60047 + * This union represents the bit fields in the Host Channel Characteristics
60048 + * Register. Read the register into the <i>d32</i> member then set/clear the
60049 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
60050 + * hcchar register.
60051 + */
60052 +typedef union hcchar_data {
60053 + /** raw register data */
60054 + uint32_t d32;
60055 +
60056 + /** register bits */
60057 + struct {
60058 + /** Maximum packet size in bytes */
60059 + unsigned mps:11;
60060 +
60061 + /** Endpoint number */
60062 + unsigned epnum:4;
60063 +
60064 + /** 0: OUT, 1: IN */
60065 + unsigned epdir:1;
60066 +
60067 + unsigned reserved:1;
60068 +
60069 + /** 0: Full/high speed device, 1: Low speed device */
60070 + unsigned lspddev:1;
60071 +
60072 + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
60073 + unsigned eptype:2;
60074 +
60075 + /** Packets per frame for periodic transfers. 0 is reserved. */
60076 + unsigned multicnt:2;
60077 +
60078 + /** Device address */
60079 + unsigned devaddr:7;
60080 +
60081 + /**
60082 + * Frame to transmit periodic transaction.
60083 + * 0: even, 1: odd
60084 + */
60085 + unsigned oddfrm:1;
60086 +
60087 + /** Channel disable */
60088 + unsigned chdis:1;
60089 +
60090 + /** Channel enable */
60091 + unsigned chen:1;
60092 + } b;
60093 +} hcchar_data_t;
60094 +
60095 +typedef union hcsplt_data {
60096 + /** raw register data */
60097 + uint32_t d32;
60098 +
60099 + /** register bits */
60100 + struct {
60101 + /** Port Address */
60102 + unsigned prtaddr:7;
60103 +
60104 + /** Hub Address */
60105 + unsigned hubaddr:7;
60106 +
60107 + /** Transaction Position */
60108 + unsigned xactpos:2;
60109 +#define DWC_HCSPLIT_XACTPOS_MID 0
60110 +#define DWC_HCSPLIT_XACTPOS_END 1
60111 +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
60112 +#define DWC_HCSPLIT_XACTPOS_ALL 3
60113 +
60114 + /** Do Complete Split */
60115 + unsigned compsplt:1;
60116 +
60117 + /** Reserved */
60118 + unsigned reserved:14;
60119 +
60120 + /** Split Enble */
60121 + unsigned spltena:1;
60122 + } b;
60123 +} hcsplt_data_t;
60124 +
60125 +/**
60126 + * This union represents the bit fields in the Host All Interrupt
60127 + * Register.
60128 + */
60129 +typedef union hcint_data {
60130 + /** raw register data */
60131 + uint32_t d32;
60132 + /** register bits */
60133 + struct {
60134 + /** Transfer Complete */
60135 + unsigned xfercomp:1;
60136 + /** Channel Halted */
60137 + unsigned chhltd:1;
60138 + /** AHB Error */
60139 + unsigned ahberr:1;
60140 + /** STALL Response Received */
60141 + unsigned stall:1;
60142 + /** NAK Response Received */
60143 + unsigned nak:1;
60144 + /** ACK Response Received */
60145 + unsigned ack:1;
60146 + /** NYET Response Received */
60147 + unsigned nyet:1;
60148 + /** Transaction Err */
60149 + unsigned xacterr:1;
60150 + /** Babble Error */
60151 + unsigned bblerr:1;
60152 + /** Frame Overrun */
60153 + unsigned frmovrun:1;
60154 + /** Data Toggle Error */
60155 + unsigned datatglerr:1;
60156 + /** Buffer Not Available (only for DDMA mode) */
60157 + unsigned bna:1;
60158 + /** Exessive transaction error (only for DDMA mode) */
60159 + unsigned xcs_xact:1;
60160 + /** Frame List Rollover interrupt */
60161 + unsigned frm_list_roll:1;
60162 + /** Reserved */
60163 + unsigned reserved14_31:18;
60164 + } b;
60165 +} hcint_data_t;
60166 +
60167 +/**
60168 + * This union represents the bit fields in the Host Channel Interrupt Mask
60169 + * Register. Read the register into the <i>d32</i> member then set/clear the
60170 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
60171 + * hcintmsk register.
60172 + */
60173 +typedef union hcintmsk_data {
60174 + /** raw register data */
60175 + uint32_t d32;
60176 +
60177 + /** register bits */
60178 + struct {
60179 + unsigned xfercompl:1;
60180 + unsigned chhltd:1;
60181 + unsigned ahberr:1;
60182 + unsigned stall:1;
60183 + unsigned nak:1;
60184 + unsigned ack:1;
60185 + unsigned nyet:1;
60186 + unsigned xacterr:1;
60187 + unsigned bblerr:1;
60188 + unsigned frmovrun:1;
60189 + unsigned datatglerr:1;
60190 + unsigned bna:1;
60191 + unsigned xcs_xact:1;
60192 + unsigned frm_list_roll:1;
60193 + unsigned reserved14_31:18;
60194 + } b;
60195 +} hcintmsk_data_t;
60196 +
60197 +/**
60198 + * This union represents the bit fields in the Host Channel Transfer Size
60199 + * Register. Read the register into the <i>d32</i> member then set/clear the
60200 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
60201 + * hcchar register.
60202 + */
60203 +
60204 +typedef union hctsiz_data {
60205 + /** raw register data */
60206 + uint32_t d32;
60207 +
60208 + /** register bits */
60209 + struct {
60210 + /** Total transfer size in bytes */
60211 + unsigned xfersize:19;
60212 +
60213 + /** Data packets to transfer */
60214 + unsigned pktcnt:10;
60215 +
60216 + /**
60217 + * Packet ID for next data packet
60218 + * 0: DATA0
60219 + * 1: DATA2
60220 + * 2: DATA1
60221 + * 3: MDATA (non-Control), SETUP (Control)
60222 + */
60223 + unsigned pid:2;
60224 +#define DWC_HCTSIZ_DATA0 0
60225 +#define DWC_HCTSIZ_DATA1 2
60226 +#define DWC_HCTSIZ_DATA2 1
60227 +#define DWC_HCTSIZ_MDATA 3
60228 +#define DWC_HCTSIZ_SETUP 3
60229 +
60230 + /** Do PING protocol when 1 */
60231 + unsigned dopng:1;
60232 + } b;
60233 +
60234 + /** register bits */
60235 + struct {
60236 + /** Scheduling information */
60237 + unsigned schinfo:8;
60238 +
60239 + /** Number of transfer descriptors.
60240 + * Max value:
60241 + * 64 in general,
60242 + * 256 only for HS isochronous endpoint.
60243 + */
60244 + unsigned ntd:8;
60245 +
60246 + /** Data packets to transfer */
60247 + unsigned reserved16_28:13;
60248 +
60249 + /**
60250 + * Packet ID for next data packet
60251 + * 0: DATA0
60252 + * 1: DATA2
60253 + * 2: DATA1
60254 + * 3: MDATA (non-Control)
60255 + */
60256 + unsigned pid:2;
60257 +
60258 + /** Do PING protocol when 1 */
60259 + unsigned dopng:1;
60260 + } b_ddma;
60261 +} hctsiz_data_t;
60262 +
60263 +/**
60264 + * This union represents the bit fields in the Host DMA Address
60265 + * Register used in Descriptor DMA mode.
60266 + */
60267 +typedef union hcdma_data {
60268 + /** raw register data */
60269 + uint32_t d32;
60270 + /** register bits */
60271 + struct {
60272 + unsigned reserved0_2:3;
60273 + /** Current Transfer Descriptor. Not used for ISOC */
60274 + unsigned ctd:8;
60275 + /** Start Address of Descriptor List */
60276 + unsigned dma_addr:21;
60277 + } b;
60278 +} hcdma_data_t;
60279 +
60280 +/**
60281 + * This union represents the bit fields in the DMA Descriptor
60282 + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
60283 + * set/clear the bits using the <i>b</i>it elements.
60284 + */
60285 +typedef union host_dma_desc_sts {
60286 + /** raw register data */
60287 + uint32_t d32;
60288 + /** quadlet bits */
60289 +
60290 + /* for non-isochronous */
60291 + struct {
60292 + /** Number of bytes */
60293 + unsigned n_bytes:17;
60294 + /** QTD offset to jump when Short Packet received - only for IN EPs */
60295 + unsigned qtd_offset:6;
60296 + /**
60297 + * Set to request the core to jump to alternate QTD if
60298 + * Short Packet received - only for IN EPs
60299 + */
60300 + unsigned a_qtd:1;
60301 + /**
60302 + * Setup Packet bit. When set indicates that buffer contains
60303 + * setup packet.
60304 + */
60305 + unsigned sup:1;
60306 + /** Interrupt On Complete */
60307 + unsigned ioc:1;
60308 + /** End of List */
60309 + unsigned eol:1;
60310 + unsigned reserved27:1;
60311 + /** Rx/Tx Status */
60312 + unsigned sts:2;
60313 +#define DMA_DESC_STS_PKTERR 1
60314 + unsigned reserved30:1;
60315 + /** Active Bit */
60316 + unsigned a:1;
60317 + } b;
60318 + /* for isochronous */
60319 + struct {
60320 + /** Number of bytes */
60321 + unsigned n_bytes:12;
60322 + unsigned reserved12_24:13;
60323 + /** Interrupt On Complete */
60324 + unsigned ioc:1;
60325 + unsigned reserved26_27:2;
60326 + /** Rx/Tx Status */
60327 + unsigned sts:2;
60328 + unsigned reserved30:1;
60329 + /** Active Bit */
60330 + unsigned a:1;
60331 + } b_isoc;
60332 +} host_dma_desc_sts_t;
60333 +
60334 +#define MAX_DMA_DESC_SIZE 131071
60335 +#define MAX_DMA_DESC_NUM_GENERIC 64
60336 +#define MAX_DMA_DESC_NUM_HS_ISOC 256
60337 +#define MAX_FRLIST_EN_NUM 64
60338 +/**
60339 + * Host-mode DMA Descriptor structure
60340 + *
60341 + * DMA Descriptor structure contains two quadlets:
60342 + * Status quadlet and Data buffer pointer.
60343 + */
60344 +typedef struct dwc_otg_host_dma_desc {
60345 + /** DMA Descriptor status quadlet */
60346 + host_dma_desc_sts_t status;
60347 + /** DMA Descriptor data buffer pointer */
60348 + uint32_t buf;
60349 +} dwc_otg_host_dma_desc_t;
60350 +
60351 +/** OTG Host Interface Structure.
60352 + *
60353 + * The OTG Host Interface Structure structure contains information
60354 + * needed to manage the DWC_otg controller acting in host mode. It
60355 + * represents the programming view of the host-specific aspects of the
60356 + * controller.
60357 + */
60358 +typedef struct dwc_otg_host_if {
60359 + /** Host Global Registers starting at offset 400h.*/
60360 + dwc_otg_host_global_regs_t *host_global_regs;
60361 +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
60362 +
60363 + /** Host Port 0 Control and Status Register */
60364 + volatile uint32_t *hprt0;
60365 +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
60366 +
60367 + /** Host Channel Specific Registers at offsets 500h-5FCh. */
60368 + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
60369 +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
60370 +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
60371 +
60372 + /* Host configuration information */
60373 + /** Number of Host Channels (range: 1-16) */
60374 + uint8_t num_host_channels;
60375 + /** Periodic EPs supported (0: no, 1: yes) */
60376 + uint8_t perio_eps_supported;
60377 + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
60378 + uint16_t perio_tx_fifo_size;
60379 +
60380 +} dwc_otg_host_if_t;
60381 +
60382 +/**
60383 + * This union represents the bit fields in the Power and Clock Gating Control
60384 + * Register. Read the register into the <i>d32</i> member then set/clear the
60385 + * bits using the <i>b</i>it elements.
60386 + */
60387 +typedef union pcgcctl_data {
60388 + /** raw register data */
60389 + uint32_t d32;
60390 +
60391 + /** register bits */
60392 + struct {
60393 + /** Stop Pclk */
60394 + unsigned stoppclk:1;
60395 + /** Gate Hclk */
60396 + unsigned gatehclk:1;
60397 + /** Power Clamp */
60398 + unsigned pwrclmp:1;
60399 + /** Reset Power Down Modules */
60400 + unsigned rstpdwnmodule:1;
60401 + /** Reserved */
60402 + unsigned reserved:1;
60403 + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
60404 + unsigned enbl_sleep_gating:1;
60405 + /** PHY In Sleep (PhySleep) */
60406 + unsigned phy_in_sleep:1;
60407 + /** Deep Sleep*/
60408 + unsigned deep_sleep:1;
60409 + unsigned resetaftsusp:1;
60410 + unsigned restoremode:1;
60411 + unsigned enbl_extnd_hiber:1;
60412 + unsigned extnd_hiber_pwrclmp:1;
60413 + unsigned extnd_hiber_switch:1;
60414 + unsigned ess_reg_restored:1;
60415 + unsigned prt_clk_sel:2;
60416 + unsigned port_power:1;
60417 + unsigned max_xcvrselect:2;
60418 + unsigned max_termsel:1;
60419 + unsigned mac_dev_addr:7;
60420 + unsigned p2hd_dev_enum_spd:2;
60421 + unsigned p2hd_prt_spd:2;
60422 + unsigned if_dev_mode:1;
60423 + } b;
60424 +} pcgcctl_data_t;
60425 +
60426 +/**
60427 + * This union represents the bit fields in the Global Data FIFO Software
60428 + * Configuration Register. Read the register into the <i>d32</i> member then
60429 + * set/clear the bits using the <i>b</i>it elements.
60430 + */
60431 +typedef union gdfifocfg_data {
60432 + /* raw register data */
60433 + uint32_t d32;
60434 + /** register bits */
60435 + struct {
60436 + /** OTG Data FIFO depth */
60437 + unsigned gdfifocfg:16;
60438 + /** Start address of EP info controller */
60439 + unsigned epinfobase:16;
60440 + } b;
60441 +} gdfifocfg_data_t;
60442 +
60443 +/**
60444 + * This union represents the bit fields in the Global Power Down Register
60445 + * Register. Read the register into the <i>d32</i> member then set/clear the
60446 + * bits using the <i>b</i>it elements.
60447 + */
60448 +typedef union gpwrdn_data {
60449 + /* raw register data */
60450 + uint32_t d32;
60451 +
60452 + /** register bits */
60453 + struct {
60454 + /** PMU Interrupt Select */
60455 + unsigned pmuintsel:1;
60456 + /** PMU Active */
60457 + unsigned pmuactv:1;
60458 + /** Restore */
60459 + unsigned restore:1;
60460 + /** Power Down Clamp */
60461 + unsigned pwrdnclmp:1;
60462 + /** Power Down Reset */
60463 + unsigned pwrdnrstn:1;
60464 + /** Power Down Switch */
60465 + unsigned pwrdnswtch:1;
60466 + /** Disable VBUS */
60467 + unsigned dis_vbus:1;
60468 + /** Line State Change */
60469 + unsigned lnstschng:1;
60470 + /** Line state change mask */
60471 + unsigned lnstchng_msk:1;
60472 + /** Reset Detected */
60473 + unsigned rst_det:1;
60474 + /** Reset Detect mask */
60475 + unsigned rst_det_msk:1;
60476 + /** Disconnect Detected */
60477 + unsigned disconn_det:1;
60478 + /** Disconnect Detect mask */
60479 + unsigned disconn_det_msk:1;
60480 + /** Connect Detected*/
60481 + unsigned connect_det:1;
60482 + /** Connect Detected Mask*/
60483 + unsigned connect_det_msk:1;
60484 + /** SRP Detected */
60485 + unsigned srp_det:1;
60486 + /** SRP Detect mask */
60487 + unsigned srp_det_msk:1;
60488 + /** Status Change Interrupt */
60489 + unsigned sts_chngint:1;
60490 + /** Status Change Interrupt Mask */
60491 + unsigned sts_chngint_msk:1;
60492 + /** Line State */
60493 + unsigned linestate:2;
60494 + /** Indicates current mode(status of IDDIG signal) */
60495 + unsigned idsts:1;
60496 + /** B Session Valid signal status*/
60497 + unsigned bsessvld:1;
60498 + /** ADP Event Detected */
60499 + unsigned adp_int:1;
60500 + /** Multi Valued ID pin */
60501 + unsigned mult_val_id_bc:5;
60502 + /** Reserved 24_31 */
60503 + unsigned reserved29_31:3;
60504 + } b;
60505 +} gpwrdn_data_t;
60506 +
60507 +#endif
60508 --- /dev/null
60509 +++ b/drivers/usb/host/dwc_otg/test/Makefile
60510 @@ -0,0 +1,16 @@
60511 +
60512 +PERL=/usr/bin/perl
60513 +PL_TESTS=test_sysfs.pl test_mod_param.pl
60514 +
60515 +.PHONY : test
60516 +test : perl_tests
60517 +
60518 +perl_tests :
60519 + @echo
60520 + @echo Running perl tests
60521 + @for test in $(PL_TESTS); do \
60522 + if $(PERL) ./$$test ; then \
60523 + echo "=======> $$test, PASSED" ; \
60524 + else echo "=======> $$test, FAILED" ; \
60525 + fi \
60526 + done
60527 --- /dev/null
60528 +++ b/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
60529 @@ -0,0 +1,337 @@
60530 +package dwc_otg_test;
60531 +
60532 +use strict;
60533 +use Exporter ();
60534 +
60535 +use vars qw(@ISA @EXPORT
60536 +$sysfsdir $paramdir $errors $params
60537 +);
60538 +
60539 +@ISA = qw(Exporter);
60540 +
60541 +#
60542 +# Globals
60543 +#
60544 +$sysfsdir = "/sys/devices/lm0";
60545 +$paramdir = "/sys/module/dwc_otg";
60546 +$errors = 0;
60547 +
60548 +$params = [
60549 + {
60550 + NAME => "otg_cap",
60551 + DEFAULT => 0,
60552 + ENUM => [],
60553 + LOW => 0,
60554 + HIGH => 2
60555 + },
60556 + {
60557 + NAME => "dma_enable",
60558 + DEFAULT => 0,
60559 + ENUM => [],
60560 + LOW => 0,
60561 + HIGH => 1
60562 + },
60563 + {
60564 + NAME => "dma_burst_size",
60565 + DEFAULT => 32,
60566 + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
60567 + LOW => 1,
60568 + HIGH => 256
60569 + },
60570 + {
60571 + NAME => "host_speed",
60572 + DEFAULT => 0,
60573 + ENUM => [],
60574 + LOW => 0,
60575 + HIGH => 1
60576 + },
60577 + {
60578 + NAME => "host_support_fs_ls_low_power",
60579 + DEFAULT => 0,
60580 + ENUM => [],
60581 + LOW => 0,
60582 + HIGH => 1
60583 + },
60584 + {
60585 + NAME => "host_ls_low_power_phy_clk",
60586 + DEFAULT => 0,
60587 + ENUM => [],
60588 + LOW => 0,
60589 + HIGH => 1
60590 + },
60591 + {
60592 + NAME => "dev_speed",
60593 + DEFAULT => 0,
60594 + ENUM => [],
60595 + LOW => 0,
60596 + HIGH => 1
60597 + },
60598 + {
60599 + NAME => "enable_dynamic_fifo",
60600 + DEFAULT => 1,
60601 + ENUM => [],
60602 + LOW => 0,
60603 + HIGH => 1
60604 + },
60605 + {
60606 + NAME => "data_fifo_size",
60607 + DEFAULT => 8192,
60608 + ENUM => [],
60609 + LOW => 32,
60610 + HIGH => 32768
60611 + },
60612 + {
60613 + NAME => "dev_rx_fifo_size",
60614 + DEFAULT => 1064,
60615 + ENUM => [],
60616 + LOW => 16,
60617 + HIGH => 32768
60618 + },
60619 + {
60620 + NAME => "dev_nperio_tx_fifo_size",
60621 + DEFAULT => 1024,
60622 + ENUM => [],
60623 + LOW => 16,
60624 + HIGH => 32768
60625 + },
60626 + {
60627 + NAME => "dev_perio_tx_fifo_size_1",
60628 + DEFAULT => 256,
60629 + ENUM => [],
60630 + LOW => 4,
60631 + HIGH => 768
60632 + },
60633 + {
60634 + NAME => "dev_perio_tx_fifo_size_2",
60635 + DEFAULT => 256,
60636 + ENUM => [],
60637 + LOW => 4,
60638 + HIGH => 768
60639 + },
60640 + {
60641 + NAME => "dev_perio_tx_fifo_size_3",
60642 + DEFAULT => 256,
60643 + ENUM => [],
60644 + LOW => 4,
60645 + HIGH => 768
60646 + },
60647 + {
60648 + NAME => "dev_perio_tx_fifo_size_4",
60649 + DEFAULT => 256,
60650 + ENUM => [],
60651 + LOW => 4,
60652 + HIGH => 768
60653 + },
60654 + {
60655 + NAME => "dev_perio_tx_fifo_size_5",
60656 + DEFAULT => 256,
60657 + ENUM => [],
60658 + LOW => 4,
60659 + HIGH => 768
60660 + },
60661 + {
60662 + NAME => "dev_perio_tx_fifo_size_6",
60663 + DEFAULT => 256,
60664 + ENUM => [],
60665 + LOW => 4,
60666 + HIGH => 768
60667 + },
60668 + {
60669 + NAME => "dev_perio_tx_fifo_size_7",
60670 + DEFAULT => 256,
60671 + ENUM => [],
60672 + LOW => 4,
60673 + HIGH => 768
60674 + },
60675 + {
60676 + NAME => "dev_perio_tx_fifo_size_8",
60677 + DEFAULT => 256,
60678 + ENUM => [],
60679 + LOW => 4,
60680 + HIGH => 768
60681 + },
60682 + {
60683 + NAME => "dev_perio_tx_fifo_size_9",
60684 + DEFAULT => 256,
60685 + ENUM => [],
60686 + LOW => 4,
60687 + HIGH => 768
60688 + },
60689 + {
60690 + NAME => "dev_perio_tx_fifo_size_10",
60691 + DEFAULT => 256,
60692 + ENUM => [],
60693 + LOW => 4,
60694 + HIGH => 768
60695 + },
60696 + {
60697 + NAME => "dev_perio_tx_fifo_size_11",
60698 + DEFAULT => 256,
60699 + ENUM => [],
60700 + LOW => 4,
60701 + HIGH => 768
60702 + },
60703 + {
60704 + NAME => "dev_perio_tx_fifo_size_12",
60705 + DEFAULT => 256,
60706 + ENUM => [],
60707 + LOW => 4,
60708 + HIGH => 768
60709 + },
60710 + {
60711 + NAME => "dev_perio_tx_fifo_size_13",
60712 + DEFAULT => 256,
60713 + ENUM => [],
60714 + LOW => 4,
60715 + HIGH => 768
60716 + },
60717 + {
60718 + NAME => "dev_perio_tx_fifo_size_14",
60719 + DEFAULT => 256,
60720 + ENUM => [],
60721 + LOW => 4,
60722 + HIGH => 768
60723 + },
60724 + {
60725 + NAME => "dev_perio_tx_fifo_size_15",
60726 + DEFAULT => 256,
60727 + ENUM => [],
60728 + LOW => 4,
60729 + HIGH => 768
60730 + },
60731 + {
60732 + NAME => "host_rx_fifo_size",
60733 + DEFAULT => 1024,
60734 + ENUM => [],
60735 + LOW => 16,
60736 + HIGH => 32768
60737 + },
60738 + {
60739 + NAME => "host_nperio_tx_fifo_size",
60740 + DEFAULT => 1024,
60741 + ENUM => [],
60742 + LOW => 16,
60743 + HIGH => 32768
60744 + },
60745 + {
60746 + NAME => "host_perio_tx_fifo_size",
60747 + DEFAULT => 1024,
60748 + ENUM => [],
60749 + LOW => 16,
60750 + HIGH => 32768
60751 + },
60752 + {
60753 + NAME => "max_transfer_size",
60754 + DEFAULT => 65535,
60755 + ENUM => [],
60756 + LOW => 2047,
60757 + HIGH => 65535
60758 + },
60759 + {
60760 + NAME => "max_packet_count",
60761 + DEFAULT => 511,
60762 + ENUM => [],
60763 + LOW => 15,
60764 + HIGH => 511
60765 + },
60766 + {
60767 + NAME => "host_channels",
60768 + DEFAULT => 12,
60769 + ENUM => [],
60770 + LOW => 1,
60771 + HIGH => 16
60772 + },
60773 + {
60774 + NAME => "dev_endpoints",
60775 + DEFAULT => 6,
60776 + ENUM => [],
60777 + LOW => 1,
60778 + HIGH => 15
60779 + },
60780 + {
60781 + NAME => "phy_type",
60782 + DEFAULT => 1,
60783 + ENUM => [],
60784 + LOW => 0,
60785 + HIGH => 2
60786 + },
60787 + {
60788 + NAME => "phy_utmi_width",
60789 + DEFAULT => 16,
60790 + ENUM => [8, 16],
60791 + LOW => 8,
60792 + HIGH => 16
60793 + },
60794 + {
60795 + NAME => "phy_ulpi_ddr",
60796 + DEFAULT => 0,
60797 + ENUM => [],
60798 + LOW => 0,
60799 + HIGH => 1
60800 + },
60801 + ];
60802 +
60803 +
60804 +#
60805 +#
60806 +sub check_arch {
60807 + $_ = `uname -m`;
60808 + chomp;
60809 + unless (m/armv4tl/) {
60810 + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
60811 + return 0;
60812 + }
60813 + return 1;
60814 +}
60815 +
60816 +#
60817 +#
60818 +sub load_module {
60819 + my $params = shift;
60820 + print "\nRemoving Module\n";
60821 + system "rmmod dwc_otg";
60822 + print "Loading Module\n";
60823 + if ($params ne "") {
60824 + print "Module Parameters: $params\n";
60825 + }
60826 + if (system("modprobe dwc_otg $params")) {
60827 + warn "Unable to load module\n";
60828 + return 0;
60829 + }
60830 + return 1;
60831 +}
60832 +
60833 +#
60834 +#
60835 +sub test_status {
60836 + my $arg = shift;
60837 +
60838 + print "\n";
60839 +
60840 + if (defined $arg) {
60841 + warn "WARNING: $arg\n";
60842 + }
60843 +
60844 + if ($errors > 0) {
60845 + warn "TEST FAILED with $errors errors\n";
60846 + return 0;
60847 + } else {
60848 + print "TEST PASSED\n";
60849 + return 0 if (defined $arg);
60850 + }
60851 + return 1;
60852 +}
60853 +
60854 +#
60855 +#
60856 +@EXPORT = qw(
60857 +$sysfsdir
60858 +$paramdir
60859 +$params
60860 +$errors
60861 +check_arch
60862 +load_module
60863 +test_status
60864 +);
60865 +
60866 +1;
60867 --- /dev/null
60868 +++ b/drivers/usb/host/dwc_otg/test/test_mod_param.pl
60869 @@ -0,0 +1,133 @@
60870 +#!/usr/bin/perl -w
60871 +#
60872 +# Run this program on the integrator.
60873 +#
60874 +# - Tests module parameter default values.
60875 +# - Tests setting of valid module parameter values via modprobe.
60876 +# - Tests invalid module parameter values.
60877 +# -----------------------------------------------------------------------------
60878 +use strict;
60879 +use dwc_otg_test;
60880 +
60881 +check_arch() or die;
60882 +
60883 +#
60884 +#
60885 +sub test {
60886 + my ($param,$expected) = @_;
60887 + my $value = get($param);
60888 +
60889 + if ($value == $expected) {
60890 + print "$param = $value, okay\n";
60891 + }
60892 +
60893 + else {
60894 + warn "ERROR: value of $param != $expected, $value\n";
60895 + $errors ++;
60896 + }
60897 +}
60898 +
60899 +#
60900 +#
60901 +sub get {
60902 + my $param = shift;
60903 + my $tmp = `cat $paramdir/$param`;
60904 + chomp $tmp;
60905 + return $tmp;
60906 +}
60907 +
60908 +#
60909 +#
60910 +sub test_main {
60911 +
60912 + print "\nTesting Module Parameters\n";
60913 +
60914 + load_module("") or die;
60915 +
60916 + # Test initial values
60917 + print "\nTesting Default Values\n";
60918 + foreach (@{$params}) {
60919 + test ($_->{NAME}, $_->{DEFAULT});
60920 + }
60921 +
60922 + # Test low value
60923 + print "\nTesting Low Value\n";
60924 + my $cmd_params = "";
60925 + foreach (@{$params}) {
60926 + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
60927 + }
60928 + load_module($cmd_params) or die;
60929 +
60930 + foreach (@{$params}) {
60931 + test ($_->{NAME}, $_->{LOW});
60932 + }
60933 +
60934 + # Test high value
60935 + print "\nTesting High Value\n";
60936 + $cmd_params = "";
60937 + foreach (@{$params}) {
60938 + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
60939 + }
60940 + load_module($cmd_params) or die;
60941 +
60942 + foreach (@{$params}) {
60943 + test ($_->{NAME}, $_->{HIGH});
60944 + }
60945 +
60946 + # Test Enum
60947 + print "\nTesting Enumerated\n";
60948 + foreach (@{$params}) {
60949 + if (defined $_->{ENUM}) {
60950 + my $value;
60951 + foreach $value (@{$_->{ENUM}}) {
60952 + $cmd_params = "$_->{NAME}=$value";
60953 + load_module($cmd_params) or die;
60954 + test ($_->{NAME}, $value);
60955 + }
60956 + }
60957 + }
60958 +
60959 + # Test Invalid Values
60960 + print "\nTesting Invalid Values\n";
60961 + $cmd_params = "";
60962 + foreach (@{$params}) {
60963 + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
60964 + }
60965 + load_module($cmd_params) or die;
60966 +
60967 + foreach (@{$params}) {
60968 + test ($_->{NAME}, $_->{DEFAULT});
60969 + }
60970 +
60971 + $cmd_params = "";
60972 + foreach (@{$params}) {
60973 + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
60974 + }
60975 + load_module($cmd_params) or die;
60976 +
60977 + foreach (@{$params}) {
60978 + test ($_->{NAME}, $_->{DEFAULT});
60979 + }
60980 +
60981 + print "\nTesting Enumerated\n";
60982 + foreach (@{$params}) {
60983 + if (defined $_->{ENUM}) {
60984 + my $value;
60985 + foreach $value (@{$_->{ENUM}}) {
60986 + $value = $value + 1;
60987 + $cmd_params = "$_->{NAME}=$value";
60988 + load_module($cmd_params) or die;
60989 + test ($_->{NAME}, $_->{DEFAULT});
60990 + $value = $value - 2;
60991 + $cmd_params = "$_->{NAME}=$value";
60992 + load_module($cmd_params) or die;
60993 + test ($_->{NAME}, $_->{DEFAULT});
60994 + }
60995 + }
60996 + }
60997 +
60998 + test_status() or die;
60999 +}
61000 +
61001 +test_main();
61002 +0;
61003 --- /dev/null
61004 +++ b/drivers/usb/host/dwc_otg/test/test_sysfs.pl
61005 @@ -0,0 +1,193 @@
61006 +#!/usr/bin/perl -w
61007 +#
61008 +# Run this program on the integrator
61009 +# - Tests select sysfs attributes.
61010 +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
61011 +# -----------------------------------------------------------------------------
61012 +use strict;
61013 +use dwc_otg_test;
61014 +
61015 +check_arch() or die;
61016 +
61017 +#
61018 +#
61019 +sub test {
61020 + my ($attr,$expected) = @_;
61021 + my $string = get($attr);
61022 +
61023 + if ($string eq $expected) {
61024 + printf("$attr = $string, okay\n");
61025 + }
61026 + else {
61027 + warn "ERROR: value of $attr != $expected, $string\n";
61028 + $errors ++;
61029 + }
61030 +}
61031 +
61032 +#
61033 +#
61034 +sub set {
61035 + my ($reg, $value) = @_;
61036 + system "echo $value > $sysfsdir/$reg";
61037 +}
61038 +
61039 +#
61040 +#
61041 +sub get {
61042 + my $attr = shift;
61043 + my $string = `cat $sysfsdir/$attr`;
61044 + chomp $string;
61045 + if ($string =~ m/\s\=\s/) {
61046 + my $tmp;
61047 + ($tmp, $string) = split /\s=\s/, $string;
61048 + }
61049 + return $string;
61050 +}
61051 +
61052 +#
61053 +#
61054 +sub test_main {
61055 + print("\nTesting Sysfs Attributes\n");
61056 +
61057 + load_module("") or die;
61058 +
61059 + # Test initial values of regoffset/regvalue/guid/gsnpsid
61060 + print("\nTesting Default Values\n");
61061 +
61062 + test("regoffset", "0xffffffff");
61063 + test("regvalue", "invalid offset");
61064 + test("guid", "0x12345678"); # this will fail if it has been changed
61065 + test("gsnpsid", "0x4f54200a");
61066 +
61067 + # Test operation of regoffset/regvalue
61068 + print("\nTesting regoffset\n");
61069 + set('regoffset', '5a5a5a5a');
61070 + test("regoffset", "0xffffffff");
61071 +
61072 + set('regoffset', '0');
61073 + test("regoffset", "0x00000000");
61074 +
61075 + set('regoffset', '40000');
61076 + test("regoffset", "0x00000000");
61077 +
61078 + set('regoffset', '3ffff');
61079 + test("regoffset", "0x0003ffff");
61080 +
61081 + set('regoffset', '1');
61082 + test("regoffset", "0x00000001");
61083 +
61084 + print("\nTesting regvalue\n");
61085 + set('regoffset', '3c');
61086 + test("regvalue", "0x12345678");
61087 + set('regvalue', '5a5a5a5a');
61088 + test("regvalue", "0x5a5a5a5a");
61089 + set('regvalue','a5a5a5a5');
61090 + test("regvalue", "0xa5a5a5a5");
61091 + set('guid','12345678');
61092 +
61093 + # Test HNP Capable
61094 + print("\nTesting HNP Capable bit\n");
61095 + set('hnpcapable', '1');
61096 + test("hnpcapable", "0x1");
61097 + set('hnpcapable','0');
61098 + test("hnpcapable", "0x0");
61099 +
61100 + set('regoffset','0c');
61101 +
61102 + my $old = get('gusbcfg');
61103 + print("setting hnpcapable\n");
61104 + set('hnpcapable', '1');
61105 + test("hnpcapable", "0x1");
61106 + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
61107 + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
61108 +
61109 + $old = get('gusbcfg');
61110 + print("clearing hnpcapable\n");
61111 + set('hnpcapable', '0');
61112 + test("hnpcapable", "0x0");
61113 + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
61114 + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
61115 +
61116 + # Test SRP Capable
61117 + print("\nTesting SRP Capable bit\n");
61118 + set('srpcapable', '1');
61119 + test("srpcapable", "0x1");
61120 + set('srpcapable','0');
61121 + test("srpcapable", "0x0");
61122 +
61123 + set('regoffset','0c');
61124 +
61125 + $old = get('gusbcfg');
61126 + print("setting srpcapable\n");
61127 + set('srpcapable', '1');
61128 + test("srpcapable", "0x1");
61129 + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
61130 + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
61131 +
61132 + $old = get('gusbcfg');
61133 + print("clearing srpcapable\n");
61134 + set('srpcapable', '0');
61135 + test("srpcapable", "0x0");
61136 + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
61137 + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
61138 +
61139 + # Test GGPIO
61140 + print("\nTesting GGPIO\n");
61141 + set('ggpio','5a5a5a5a');
61142 + test('ggpio','0x5a5a0000');
61143 + set('ggpio','a5a5a5a5');
61144 + test('ggpio','0xa5a50000');
61145 + set('ggpio','11110000');
61146 + test('ggpio','0x11110000');
61147 + set('ggpio','00001111');
61148 + test('ggpio','0x00000000');
61149 +
61150 + # Test DEVSPEED
61151 + print("\nTesting DEVSPEED\n");
61152 + set('regoffset','800');
61153 + $old = get('regvalue');
61154 + set('devspeed','0');
61155 + test('devspeed','0x0');
61156 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
61157 + set('devspeed','1');
61158 + test('devspeed','0x1');
61159 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
61160 + set('devspeed','2');
61161 + test('devspeed','0x2');
61162 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
61163 + set('devspeed','3');
61164 + test('devspeed','0x3');
61165 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
61166 + set('devspeed','4');
61167 + test('devspeed','0x0');
61168 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
61169 + set('devspeed','5');
61170 + test('devspeed','0x1');
61171 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
61172 +
61173 +
61174 + # mode Returns the current mode:0 for device mode1 for host mode Read
61175 + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
61176 + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
61177 + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
61178 + # bussuspend Suspend the USB bus. Read/Write
61179 + # busconnected Get the connection status of the bus Read
61180 +
61181 + # gotgctl Get or set the Core Control Status Register. Read/Write
61182 + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
61183 + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
61184 + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
61185 + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
61186 + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
61187 + ## guid Get or set the value of the User ID Register Read/Write
61188 + ## gsnpsid Get the value of the Synopsys ID Regester Read
61189 + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
61190 + # enumspeed Gets the device enumeration Speed. Read
61191 + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
61192 + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
61193 +
61194 + test_status("TEST NYI") or die;
61195 +}
61196 +
61197 +test_main();
61198 +0;