dwc_otg_hcd.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /* ==========================================================================
  2. * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  3. * $Revision: #58 $
  4. * $Date: 2011/09/15 $
  5. * $Change: 1846647 $
  6. *
  7. * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  8. * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  9. * otherwise expressly agreed to in writing between Synopsys and you.
  10. *
  11. * The Software IS NOT an item of Licensed Software or Licensed Product under
  12. * any End User Software License Agreement or Agreement for Licensed Product
  13. * with Synopsys or any supplement thereto. You are permitted to use and
  14. * redistribute this Software in source and binary forms, with or without
  15. * modification, provided that redistributions of source code must retain this
  16. * notice. You may not view, use, disclose, copy or distribute this file or
  17. * any information contained herein except pursuant to this license grant from
  18. * Synopsys. If you do not agree with this notice, including the disclaimer
  19. * below, then you are not authorized to use the Software.
  20. *
  21. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31. * DAMAGE.
  32. * ========================================================================== */
  33. #ifndef DWC_DEVICE_ONLY
  34. #ifndef __DWC_HCD_H__
  35. #define __DWC_HCD_H__
  36. #include "dwc_otg_os_dep.h"
  37. #include "usb.h"
  38. #include "dwc_otg_hcd_if.h"
  39. #include "dwc_otg_core_if.h"
  40. #include "dwc_list.h"
  41. #include "dwc_otg_cil.h"
  42. /**
  43. * @file
  44. *
  45. * This file contains the structures, constants, and interfaces for
  46. * the Host Contoller Driver (HCD).
  47. *
  48. * The Host Controller Driver (HCD) is responsible for translating requests
  49. * from the USB Driver into the appropriate actions on the DWC_otg controller.
  50. * It isolates the USBD from the specifics of the controller by providing an
  51. * API to the USBD.
  52. */
  53. struct dwc_otg_hcd_pipe_info {
  54. uint8_t dev_addr;
  55. uint8_t ep_num;
  56. uint8_t pipe_type;
  57. uint8_t pipe_dir;
  58. uint16_t mps;
  59. };
  60. struct dwc_otg_hcd_iso_packet_desc {
  61. uint32_t offset;
  62. uint32_t length;
  63. uint32_t actual_length;
  64. uint32_t status;
  65. };
  66. struct dwc_otg_qtd;
  67. struct dwc_otg_hcd_urb {
  68. void *priv;
  69. struct dwc_otg_qtd *qtd;
  70. void *buf;
  71. dwc_dma_t dma;
  72. void *setup_packet;
  73. dwc_dma_t setup_dma;
  74. uint32_t length;
  75. uint32_t actual_length;
  76. uint32_t status;
  77. uint32_t error_count;
  78. uint32_t packet_count;
  79. uint32_t flags;
  80. uint16_t interval;
  81. struct dwc_otg_hcd_pipe_info pipe_info;
  82. struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  83. };
  84. static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  85. {
  86. return pipe->ep_num;
  87. }
  88. static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  89. *pipe)
  90. {
  91. return pipe->pipe_type;
  92. }
  93. static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  94. {
  95. return pipe->mps;
  96. }
  97. static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  98. *pipe)
  99. {
  100. return pipe->dev_addr;
  101. }
  102. static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  103. *pipe)
  104. {
  105. return (pipe->pipe_type == UE_ISOCHRONOUS);
  106. }
  107. static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  108. *pipe)
  109. {
  110. return (pipe->pipe_type == UE_INTERRUPT);
  111. }
  112. static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  113. *pipe)
  114. {
  115. return (pipe->pipe_type == UE_BULK);
  116. }
  117. static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  118. *pipe)
  119. {
  120. return (pipe->pipe_type == UE_CONTROL);
  121. }
  122. static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  123. {
  124. return (pipe->pipe_dir == UE_DIR_IN);
  125. }
  126. static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  127. *pipe)
  128. {
  129. return (!dwc_otg_hcd_is_pipe_in(pipe));
  130. }
  131. static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  132. uint8_t devaddr, uint8_t ep_num,
  133. uint8_t pipe_type, uint8_t pipe_dir,
  134. uint16_t mps)
  135. {
  136. pipe->dev_addr = devaddr;
  137. pipe->ep_num = ep_num;
  138. pipe->pipe_type = pipe_type;
  139. pipe->pipe_dir = pipe_dir;
  140. pipe->mps = mps;
  141. }
  142. /**
  143. * Phases for control transfers.
  144. */
  145. typedef enum dwc_otg_control_phase {
  146. DWC_OTG_CONTROL_SETUP,
  147. DWC_OTG_CONTROL_DATA,
  148. DWC_OTG_CONTROL_STATUS
  149. } dwc_otg_control_phase_e;
  150. /** Transaction types. */
  151. typedef enum dwc_otg_transaction_type {
  152. DWC_OTG_TRANSACTION_NONE,
  153. DWC_OTG_TRANSACTION_PERIODIC,
  154. DWC_OTG_TRANSACTION_NON_PERIODIC,
  155. DWC_OTG_TRANSACTION_ALL
  156. } dwc_otg_transaction_type_e;
  157. struct dwc_otg_qh;
  158. /**
  159. * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  160. * interrupt, or isochronous transfer. A single QTD is created for each URB
  161. * (of one of these types) submitted to the HCD. The transfer associated with
  162. * a QTD may require one or multiple transactions.
  163. *
  164. * A QTD is linked to a Queue Head, which is entered in either the
  165. * non-periodic or periodic schedule for execution. When a QTD is chosen for
  166. * execution, some or all of its transactions may be executed. After
  167. * execution, the state of the QTD is updated. The QTD may be retired if all
  168. * its transactions are complete or if an error occurred. Otherwise, it
  169. * remains in the schedule so more transactions can be executed later.
  170. */
  171. typedef struct dwc_otg_qtd {
  172. /**
  173. * Determines the PID of the next data packet for the data phase of
  174. * control transfers. Ignored for other transfer types.<br>
  175. * One of the following values:
  176. * - DWC_OTG_HC_PID_DATA0
  177. * - DWC_OTG_HC_PID_DATA1
  178. */
  179. uint8_t data_toggle;
  180. /** Current phase for control transfers (Setup, Data, or Status). */
  181. dwc_otg_control_phase_e control_phase;
  182. /** Keep track of the current split type
  183. * for FS/LS endpoints on a HS Hub */
  184. uint8_t complete_split;
  185. /** How many bytes transferred during SSPLIT OUT */
  186. uint32_t ssplit_out_xfer_count;
  187. /**
  188. * Holds the number of bus errors that have occurred for a transaction
  189. * within this transfer.
  190. */
  191. uint8_t error_count;
  192. /**
  193. * Index of the next frame descriptor for an isochronous transfer. A
  194. * frame descriptor describes the buffer position and length of the
  195. * data to be transferred in the next scheduled (micro)frame of an
  196. * isochronous transfer. It also holds status for that transaction.
  197. * The frame index starts at 0.
  198. */
  199. uint16_t isoc_frame_index;
  200. /** Position of the ISOC split on full/low speed */
  201. uint8_t isoc_split_pos;
  202. /** Position of the ISOC split in the buffer for the current frame */
  203. uint16_t isoc_split_offset;
  204. /** URB for this transfer */
  205. struct dwc_otg_hcd_urb *urb;
  206. struct dwc_otg_qh *qh;
  207. /** This list of QTDs */
  208. DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  209. /** Indicates if this QTD is currently processed by HW. */
  210. uint8_t in_process;
  211. /** Number of DMA descriptors for this QTD */
  212. uint8_t n_desc;
  213. /**
  214. * Last activated frame(packet) index.
  215. * Used in Descriptor DMA mode only.
  216. */
  217. uint16_t isoc_frame_index_last;
  218. } dwc_otg_qtd_t;
  219. DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  220. /**
  221. * A Queue Head (QH) holds the static characteristics of an endpoint and
  222. * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  223. * be entered in either the non-periodic or periodic schedule.
  224. */
  225. typedef struct dwc_otg_qh {
  226. /**
  227. * Endpoint type.
  228. * One of the following values:
  229. * - UE_CONTROL
  230. * - UE_BULK
  231. * - UE_INTERRUPT
  232. * - UE_ISOCHRONOUS
  233. */
  234. uint8_t ep_type;
  235. uint8_t ep_is_in;
  236. /** wMaxPacketSize Field of Endpoint Descriptor. */
  237. uint16_t maxp;
  238. /**
  239. * Device speed.
  240. * One of the following values:
  241. * - DWC_OTG_EP_SPEED_LOW
  242. * - DWC_OTG_EP_SPEED_FULL
  243. * - DWC_OTG_EP_SPEED_HIGH
  244. */
  245. uint8_t dev_speed;
  246. /**
  247. * Determines the PID of the next data packet for non-control
  248. * transfers. Ignored for control transfers.<br>
  249. * One of the following values:
  250. * - DWC_OTG_HC_PID_DATA0
  251. * - DWC_OTG_HC_PID_DATA1
  252. */
  253. uint8_t data_toggle;
  254. /** Ping state if 1. */
  255. uint8_t ping_state;
  256. /**
  257. * List of QTDs for this QH.
  258. */
  259. struct dwc_otg_qtd_list qtd_list;
  260. /** Host channel currently processing transfers for this QH. */
  261. struct dwc_hc *channel;
  262. /** Full/low speed endpoint on high-speed hub requires split. */
  263. uint8_t do_split;
  264. /** @name Periodic schedule information */
  265. /** @{ */
  266. /** Bandwidth in microseconds per (micro)frame. */
  267. uint16_t usecs;
  268. /** Interval between transfers in (micro)frames. */
  269. uint16_t interval;
  270. /**
  271. * (micro)frame to initialize a periodic transfer. The transfer
  272. * executes in the following (micro)frame.
  273. */
  274. uint16_t sched_frame;
  275. /** (micro)frame at which last start split was initialized. */
  276. uint16_t start_split_frame;
  277. /** @} */
  278. /**
  279. * Used instead of original buffer if
  280. * it(physical address) is not dword-aligned.
  281. */
  282. uint8_t *dw_align_buf;
  283. dwc_dma_t dw_align_buf_dma;
  284. /** Entry for QH in either the periodic or non-periodic schedule. */
  285. dwc_list_link_t qh_list_entry;
  286. /** @name Descriptor DMA support */
  287. /** @{ */
  288. /** Descriptor List. */
  289. dwc_otg_host_dma_desc_t *desc_list;
  290. /** Descriptor List physical address. */
  291. dwc_dma_t desc_list_dma;
  292. /**
  293. * Xfer Bytes array.
  294. * Each element corresponds to a descriptor and indicates
  295. * original XferSize size value for the descriptor.
  296. */
  297. uint32_t *n_bytes;
  298. /** Actual number of transfer descriptors in a list. */
  299. uint16_t ntd;
  300. /** First activated isochronous transfer descriptor index. */
  301. uint8_t td_first;
  302. /** Last activated isochronous transfer descriptor index. */
  303. uint8_t td_last;
  304. /** @} */
  305. } dwc_otg_qh_t;
  306. DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  307. /**
  308. * This structure holds the state of the HCD, including the non-periodic and
  309. * periodic schedules.
  310. */
  311. struct dwc_otg_hcd {
  312. /** The DWC otg device pointer */
  313. struct dwc_otg_device *otg_dev;
  314. /** DWC OTG Core Interface Layer */
  315. dwc_otg_core_if_t *core_if;
  316. /** Function HCD driver callbacks */
  317. struct dwc_otg_hcd_function_ops *fops;
  318. /** Internal DWC HCD Flags */
  319. volatile union dwc_otg_hcd_internal_flags {
  320. uint32_t d32;
  321. struct {
  322. unsigned port_connect_status_change:1;
  323. unsigned port_connect_status:1;
  324. unsigned port_reset_change:1;
  325. unsigned port_enable_change:1;
  326. unsigned port_suspend_change:1;
  327. unsigned port_over_current_change:1;
  328. unsigned port_l1_change:1;
  329. unsigned reserved:26;
  330. } b;
  331. } flags;
  332. /**
  333. * Inactive items in the non-periodic schedule. This is a list of
  334. * Queue Heads. Transfers associated with these Queue Heads are not
  335. * currently assigned to a host channel.
  336. */
  337. dwc_list_link_t non_periodic_sched_inactive;
  338. /**
  339. * Active items in the non-periodic schedule. This is a list of
  340. * Queue Heads. Transfers associated with these Queue Heads are
  341. * currently assigned to a host channel.
  342. */
  343. dwc_list_link_t non_periodic_sched_active;
  344. /**
  345. * Pointer to the next Queue Head to process in the active
  346. * non-periodic schedule.
  347. */
  348. dwc_list_link_t *non_periodic_qh_ptr;
  349. /**
  350. * Inactive items in the periodic schedule. This is a list of QHs for
  351. * periodic transfers that are _not_ scheduled for the next frame.
  352. * Each QH in the list has an interval counter that determines when it
  353. * needs to be scheduled for execution. This scheduling mechanism
  354. * allows only a simple calculation for periodic bandwidth used (i.e.
  355. * must assume that all periodic transfers may need to execute in the
  356. * same frame). However, it greatly simplifies scheduling and should
  357. * be sufficient for the vast majority of OTG hosts, which need to
  358. * connect to a small number of peripherals at one time.
  359. *
  360. * Items move from this list to periodic_sched_ready when the QH
  361. * interval counter is 0 at SOF.
  362. */
  363. dwc_list_link_t periodic_sched_inactive;
  364. /**
  365. * List of periodic QHs that are ready for execution in the next
  366. * frame, but have not yet been assigned to host channels.
  367. *
  368. * Items move from this list to periodic_sched_assigned as host
  369. * channels become available during the current frame.
  370. */
  371. dwc_list_link_t periodic_sched_ready;
  372. /**
  373. * List of periodic QHs to be executed in the next frame that are
  374. * assigned to host channels.
  375. *
  376. * Items move from this list to periodic_sched_queued as the
  377. * transactions for the QH are queued to the DWC_otg controller.
  378. */
  379. dwc_list_link_t periodic_sched_assigned;
  380. /**
  381. * List of periodic QHs that have been queued for execution.
  382. *
  383. * Items move from this list to either periodic_sched_inactive or
  384. * periodic_sched_ready when the channel associated with the transfer
  385. * is released. If the interval for the QH is 1, the item moves to
  386. * periodic_sched_ready because it must be rescheduled for the next
  387. * frame. Otherwise, the item moves to periodic_sched_inactive.
  388. */
  389. dwc_list_link_t periodic_sched_queued;
  390. /**
  391. * Total bandwidth claimed so far for periodic transfers. This value
  392. * is in microseconds per (micro)frame. The assumption is that all
  393. * periodic transfers may occur in the same (micro)frame.
  394. */
  395. uint16_t periodic_usecs;
  396. /**
  397. * Frame number read from the core at SOF. The value ranges from 0 to
  398. * DWC_HFNUM_MAX_FRNUM.
  399. */
  400. uint16_t frame_number;
  401. /**
  402. * Count of periodic QHs, if using several eps. For SOF enable/disable.
  403. */
  404. uint16_t periodic_qh_count;
  405. /**
  406. * Free host channels in the controller. This is a list of
  407. * dwc_hc_t items.
  408. */
  409. struct hc_list free_hc_list;
  410. /**
  411. * Number of host channels assigned to periodic transfers. Currently
  412. * assuming that there is a dedicated host channel for each periodic
  413. * transaction and at least one host channel available for
  414. * non-periodic transactions.
  415. */
  416. int periodic_channels;
  417. /**
  418. * Number of host channels assigned to non-periodic transfers.
  419. */
  420. int non_periodic_channels;
  421. /**
  422. * Array of pointers to the host channel descriptors. Allows accessing
  423. * a host channel descriptor given the host channel number. This is
  424. * useful in interrupt handlers.
  425. */
  426. struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  427. /**
  428. * Buffer to use for any data received during the status phase of a
  429. * control transfer. Normally no data is transferred during the status
  430. * phase. This buffer is used as a bit bucket.
  431. */
  432. uint8_t *status_buf;
  433. /**
  434. * DMA address for status_buf.
  435. */
  436. dma_addr_t status_buf_dma;
  437. #define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  438. /**
  439. * Connection timer. An OTG host must display a message if the device
  440. * does not connect. Started when the VBus power is turned on via
  441. * sysfs attribute "buspower".
  442. */
  443. dwc_timer_t *conn_timer;
  444. /* Tasket to do a reset */
  445. dwc_tasklet_t *reset_tasklet;
  446. /* */
  447. dwc_spinlock_t *lock;
  448. /**
  449. * Private data that could be used by OS wrapper.
  450. */
  451. void *priv;
  452. uint8_t otg_port;
  453. /** Frame List */
  454. uint32_t *frame_list;
  455. /** Frame List DMA address */
  456. dma_addr_t frame_list_dma;
  457. #ifdef DEBUG
  458. uint32_t frrem_samples;
  459. uint64_t frrem_accum;
  460. uint32_t hfnum_7_samples_a;
  461. uint64_t hfnum_7_frrem_accum_a;
  462. uint32_t hfnum_0_samples_a;
  463. uint64_t hfnum_0_frrem_accum_a;
  464. uint32_t hfnum_other_samples_a;
  465. uint64_t hfnum_other_frrem_accum_a;
  466. uint32_t hfnum_7_samples_b;
  467. uint64_t hfnum_7_frrem_accum_b;
  468. uint32_t hfnum_0_samples_b;
  469. uint64_t hfnum_0_frrem_accum_b;
  470. uint32_t hfnum_other_samples_b;
  471. uint64_t hfnum_other_frrem_accum_b;
  472. #endif
  473. };
  474. /** @name Transaction Execution Functions */
  475. /** @{ */
  476. extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  477. * hcd);
  478. extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  479. dwc_otg_transaction_type_e tr_type);
  480. /** @} */
  481. /** @name Interrupt Handler Functions */
  482. /** @{ */
  483. extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  484. extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  485. extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  486. dwc_otg_hcd);
  487. extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  488. dwc_otg_hcd);
  489. extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  490. dwc_otg_hcd);
  491. extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  492. dwc_otg_hcd);
  493. extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  494. extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  495. dwc_otg_hcd);
  496. extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  497. extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  498. extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  499. uint32_t num);
  500. extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  501. extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  502. dwc_otg_hcd);
  503. /** @} */
  504. /** @name Schedule Queue Functions */
  505. /** @{ */
  506. /* Implemented in dwc_otg_hcd_queue.c */
  507. extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  508. dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  509. extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  510. extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  511. extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  512. extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  513. int sched_csplit);
  514. /** Remove and free a QH */
  515. static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  516. dwc_otg_qh_t * qh)
  517. {
  518. dwc_irqflags_t flags;
  519. DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  520. dwc_otg_hcd_qh_remove(hcd, qh);
  521. DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  522. dwc_otg_hcd_qh_free(hcd, qh);
  523. }
  524. /** Allocates memory for a QH structure.
  525. * @return Returns the memory allocate or NULL on error. */
  526. static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  527. {
  528. if (atomic_alloc)
  529. return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  530. else
  531. return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  532. }
  533. extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  534. int atomic_alloc);
  535. extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  536. extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  537. dwc_otg_qh_t ** qh, int atomic_alloc);
  538. /** Allocates memory for a QTD structure.
  539. * @return Returns the memory allocate or NULL on error. */
  540. static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  541. {
  542. if (atomic_alloc)
  543. return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  544. else
  545. return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  546. }
  547. /** Frees the memory for a QTD structure. QTD should already be removed from
  548. * list.
  549. * @param qtd QTD to free.*/
  550. static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  551. {
  552. DWC_FREE(qtd);
  553. }
  554. /** Removes a QTD from list.
  555. * @param hcd HCD instance.
  556. * @param qtd QTD to remove from list.
  557. * @param qh QTD belongs to.
  558. */
  559. static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  560. dwc_otg_qtd_t * qtd,
  561. dwc_otg_qh_t * qh)
  562. {
  563. DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  564. }
  565. /** Remove and free a QTD
  566. * Need to disable IRQ and hold hcd lock while calling this function out of
  567. * interrupt servicing chain */
  568. static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  569. dwc_otg_qtd_t * qtd,
  570. dwc_otg_qh_t * qh)
  571. {
  572. dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  573. dwc_otg_hcd_qtd_free(qtd);
  574. }
  575. /** @} */
  576. /** @name Descriptor DMA Supporting Functions */
  577. /** @{ */
  578. extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  579. extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  580. dwc_hc_t * hc,
  581. dwc_otg_hc_regs_t * hc_regs,
  582. dwc_otg_halt_status_e halt_status);
  583. extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  584. extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  585. /** @} */
  586. /** @name Internal Functions */
  587. /** @{ */
  588. dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  589. /** @} */
  590. #ifdef CONFIG_USB_DWC_OTG_LPM
  591. extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  592. uint8_t devaddr);
  593. extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  594. #endif
  595. /** Gets the QH that contains the list_head */
  596. #define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  597. /** Gets the QTD that contains the list_head */
  598. #define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  599. /** Check if QH is non-periodic */
  600. #define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  601. (_qh_ptr_->ep_type == UE_CONTROL))
  602. /** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  603. #define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  604. /** Packet size for any kind of endpoint descriptor */
  605. #define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  606. /**
  607. * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  608. * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  609. * frame number when the max frame number is reached.
  610. */
  611. static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  612. {
  613. return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  614. (DWC_HFNUM_MAX_FRNUM >> 1);
  615. }
  616. /**
  617. * Returns true if _frame1 is greater than _frame2. The comparison is done
  618. * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  619. * number when the max frame number is reached.
  620. */
  621. static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  622. {
  623. return (frame1 != frame2) &&
  624. (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  625. (DWC_HFNUM_MAX_FRNUM >> 1));
  626. }
  627. /**
  628. * Increments _frame by the amount specified by _inc. The addition is done
  629. * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  630. */
  631. static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  632. {
  633. return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  634. }
  635. static inline uint16_t dwc_full_frame_num(uint16_t frame)
  636. {
  637. return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  638. }
  639. static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  640. {
  641. return frame & 0x7;
  642. }
  643. void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  644. dwc_otg_hc_regs_t * hc_regs,
  645. dwc_otg_qtd_t * qtd);
  646. #ifdef DEBUG
  647. /**
  648. * Macro to sample the remaining PHY clocks left in the current frame. This
  649. * may be used during debugging to determine the average time it takes to
  650. * execute sections of code. There are two possible sample points, "a" and
  651. * "b", so the _letter argument must be one of these values.
  652. *
  653. * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  654. * example, "cat /sys/devices/lm0/hcd_frrem".
  655. */
  656. #define dwc_sample_frrem(_hcd, _qh, _letter) \
  657. { \
  658. hfnum_data_t hfnum; \
  659. dwc_otg_qtd_t *qtd; \
  660. qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  661. if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  662. hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  663. switch (hfnum.b.frnum & 0x7) { \
  664. case 7: \
  665. _hcd->hfnum_7_samples_##_letter++; \
  666. _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  667. break; \
  668. case 0: \
  669. _hcd->hfnum_0_samples_##_letter++; \
  670. _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  671. break; \
  672. default: \
  673. _hcd->hfnum_other_samples_##_letter++; \
  674. _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  675. break; \
  676. } \
  677. } \
  678. }
  679. #else
  680. #define dwc_sample_frrem(_hcd, _qh, _letter)
  681. #endif
  682. #endif
  683. #endif /* DWC_DEVICE_ONLY */