x1000_dwc.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * x1000_dwc.h
  3. *
  4. * Created on: 2017Äê2ÔÂ3ÈÕ
  5. * Author: Urey
  6. */
  7. #ifndef _X1000_DWC_H_
  8. #define _X1000_DWC_H_
  9. #define DWC_FORCE_SPEED_FULL 0
  10. #if DWC_FORCE_SPEED_FULL
  11. #define ENDPOINT_PACKET_SIZE 64
  12. #define CONTROL_MAX_PACKET_SIZE 64
  13. #else
  14. #define ENDPOINT_PACKET_SIZE 512
  15. #define CONTROL_MAX_PACKET_SIZE 64
  16. #endif
  17. #define DWC_EP_IN_OFS 0
  18. #define DWC_EP_OUT_OFS 16
  19. #define DWC_EPNO_MASK 0x7f
  20. typedef struct dwc_ep_t
  21. {
  22. uint8_t num; /* ep number used for register address lookup */
  23. #define EP_IDLE 0
  24. #define EP_TRANSFERED 1
  25. #define EP_TRANSFERING 2
  26. #define EP_SETUP 0
  27. #define EP_DATA 1
  28. #define EP_STATUS 2
  29. #define EP_SETUP_PHASEDONE 3
  30. uint32_t ep_state;
  31. uint32_t is_in; /* ep dir 1 = out */
  32. uint32_t active; /* ep active */
  33. uint32_t type; /* ep type */
  34. #define DWC_OTG_EP_TYPE_CONTROL 0
  35. #define DWC_OTG_EP_TYPE_ISOC 1
  36. #define DWC_OTG_EP_TYPE_BULK 2
  37. #define DWC_OTG_EP_TYPE_INTR 3
  38. uint32_t maxpacket; /* max packet bytes */
  39. // uint32_t ctrl_req_addr;
  40. void* xfer_buff; /* pointer to transfer buffer */
  41. uint32_t xfer_len; /* number of bytes to transfer */
  42. uint32_t xfer_count; /* number of bytes transfered */
  43. } dwc_ep;
  44. typedef union hwcfg1_data {
  45. uint32_t d32;
  46. struct {
  47. unsigned ep_dir0:2;
  48. unsigned ep_dir1:2;
  49. unsigned ep_dir2:2;
  50. unsigned ep_dir3:2;
  51. unsigned ep_dir4:2;
  52. unsigned ep_dir5:2;
  53. unsigned ep_dir6:2;
  54. unsigned ep_dir7:2;
  55. unsigned ep_dir8:2;
  56. unsigned ep_dir9:2;
  57. unsigned ep_dir10:2;
  58. unsigned ep_dir11:2;
  59. unsigned ep_dir12:2;
  60. unsigned ep_dir13:2;
  61. unsigned ep_dir14:2;
  62. unsigned ep_dir15:2;
  63. } b;
  64. #define DWC_HWCFG1_DIR_BIDIR 0x0
  65. #define DWC_HWCFG1_DIR_IN 0x1
  66. #define DWC_HWCFG1_DIR_OUT 0x2
  67. } hwcfg1_data_t;
  68. /**
  69. * This union represents the bit fields in the User HW Config2
  70. * Register. Read the register into the <i>d32</i> element then read
  71. * out the bits using the <i>b</i>it elements.
  72. */
  73. typedef union hwcfg2_data {
  74. /** raw register data */
  75. uint32_t d32;
  76. /** register bits */
  77. struct {
  78. /* GHWCFG2 */
  79. unsigned op_mode:3;
  80. #define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  81. #define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  82. #define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  83. #define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  84. #define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  85. #define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  86. #define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  87. unsigned architecture:2;
  88. unsigned point2point:1;
  89. unsigned hs_phy_type:2;
  90. #define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  91. #define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  92. #define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  93. #define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  94. unsigned fs_phy_type:2;
  95. unsigned num_dev_ep:4;
  96. unsigned num_host_chan:4;
  97. unsigned perio_ep_supported:1;
  98. unsigned dynamic_fifo:1;
  99. unsigned multi_proc_int:1;
  100. unsigned reserved21:1;
  101. unsigned nonperio_tx_q_depth:2;
  102. unsigned host_perio_tx_q_depth:2;
  103. unsigned dev_token_q_depth:5;
  104. unsigned otg_enable_ic_usb:1;
  105. } b;
  106. } hwcfg2_data_t;
  107. /**
  108. * This union represents the bit fields in the User HW Config3
  109. * Register. Read the register into the <i>d32</i> element then read
  110. * out the bits using the <i>b</i>it elements.
  111. */
  112. typedef union hwcfg3_data {
  113. /** raw register data */
  114. uint32_t d32;
  115. /** register bits */
  116. struct {
  117. /* GHWCFG3 */
  118. unsigned xfer_size_cntr_width:4;
  119. unsigned packet_size_cntr_width:3;
  120. unsigned otg_func:1;
  121. unsigned i2c:1;
  122. unsigned vendor_ctrl_if:1;
  123. unsigned optional_features:1;
  124. unsigned synch_reset_type:1;
  125. unsigned adp_supp:1;
  126. unsigned otg_enable_hsic:1;
  127. unsigned bc_support:1;
  128. unsigned otg_lpm_en:1;
  129. unsigned dfifo_depth:16;
  130. } b;
  131. } hwcfg3_data_t;
  132. /**
  133. * This union represents the bit fields in the User HW Config4
  134. * Register. Read the register into the <i>d32</i> element then read
  135. * out the bits using the <i>b</i>it elements.
  136. */
  137. typedef union hwcfg4_data {
  138. /** raw register data */
  139. uint32_t d32;
  140. /** register bits */
  141. struct {
  142. unsigned num_dev_perio_in_ep:4;
  143. unsigned power_optimiz:1;
  144. unsigned min_ahb_freq:1;
  145. unsigned part_power_down:1;
  146. unsigned reserved:7;
  147. unsigned utmi_phy_data_width:2;
  148. unsigned num_dev_mode_ctrl_ep:4;
  149. unsigned iddig_filt_en:1;
  150. unsigned vbus_valid_filt_en:1;
  151. unsigned a_valid_filt_en:1;
  152. unsigned b_valid_filt_en:1;
  153. unsigned session_end_filt_en:1;
  154. unsigned ded_fifo_en:1;
  155. unsigned num_in_eps:4;
  156. unsigned desc_dma:1;
  157. unsigned desc_dma_dyn:1;
  158. } b;
  159. } hwcfg4_data_t;
  160. typedef union dwc_state {
  161. uint8_t d8;
  162. struct {
  163. unsigned event:1;
  164. #define USB_CABLE_DISCONNECT 0
  165. #define USB_CABLE_CONNECT 1
  166. #define USB_CABLE_SUSPEND 2
  167. #define USB_CONFIGURED 3
  168. unsigned state:7;
  169. }b;
  170. } dwc_st;
  171. typedef struct dwc_cfg_if_t
  172. {
  173. hwcfg1_data_t hwcfg1;
  174. hwcfg2_data_t hwcfg2;
  175. hwcfg3_data_t hwcfg3;
  176. hwcfg4_data_t hwcfg4;
  177. dwc_st status;
  178. #define USB_SPEED_HIGH 0
  179. #define USB_SPEED_FULL 1
  180. #define USB_SPEED_LOW 2
  181. uint8_t speed;
  182. uint8_t is_dma;
  183. // uint8_t ep0State;
  184. dwc_ep *dep[32];
  185. rt_sem_t isr_sem;
  186. } dwc_handle;
  187. /**
  188. * This union represents the bit fields in the Device Control
  189. * Register. Read the register into the <i>d32</i> member then
  190. * set/clear the bits using the <i>b</i>it elements.
  191. */
  192. typedef union dctl_data {
  193. /** raw register data */
  194. uint32_t d32;
  195. /** register bits */
  196. struct {
  197. /** Remote Wakeup */
  198. unsigned rmtwkupsig:1;
  199. /** Soft Disconnect */
  200. unsigned sftdiscon:1;
  201. /** Global Non-Periodic IN NAK Status */
  202. unsigned gnpinnaksts:1;
  203. /** Global OUT NAK Status */
  204. unsigned goutnaksts:1;
  205. /** Test Control */
  206. unsigned tstctl:3;
  207. /** Set Global Non-Periodic IN NAK */
  208. unsigned sgnpinnak:1;
  209. /** Clear Global Non-Periodic IN NAK */
  210. unsigned cgnpinnak:1;
  211. /** Set Global OUT NAK */
  212. unsigned sgoutnak:1;
  213. /** Clear Global OUT NAK */
  214. unsigned cgoutnak:1;
  215. /** Power-On Programming Done */
  216. unsigned pwronprgdone:1;
  217. /** Reserved */
  218. unsigned reserved:1;
  219. /** Global Multi Count */
  220. unsigned gmc:2;
  221. /** Ignore Frame Number for ISOC EPs */
  222. unsigned ifrmnum:1;
  223. /** NAK on Babble */
  224. unsigned nakonbble:1;
  225. /** Enable Continue on BNA */
  226. unsigned encontonbna:1;
  227. unsigned reserved18_31:14;
  228. } b;
  229. } dctl_data_t;
  230. /**
  231. * This union represents the bit fields of the Core Interrupt Mask
  232. * Register (GINTMSK). Set/clear the bits using the bit fields then
  233. * write the <i>d32</i> value to the register.
  234. */
  235. typedef union gintmsk_data {
  236. /** raw register data */
  237. uint32_t d32;
  238. /** register bits */
  239. struct {
  240. unsigned reserved0:1;
  241. unsigned modemismatch:1;
  242. unsigned otgintr:1;
  243. unsigned sofintr:1;
  244. unsigned rxstsqlvl:1;
  245. unsigned nptxfempty:1;
  246. unsigned ginnakeff:1;
  247. unsigned goutnakeff:1;
  248. unsigned ulpickint:1;
  249. unsigned i2cintr:1;
  250. unsigned erlysuspend:1;
  251. unsigned usbsuspend:1;
  252. unsigned usbreset:1;
  253. unsigned enumdone:1;
  254. unsigned isooutdrop:1;
  255. unsigned eopframe:1;
  256. unsigned restoredone:1;
  257. unsigned epmismatch:1;
  258. unsigned inepintr:1;
  259. unsigned outepintr:1;
  260. unsigned incomplisoin:1;
  261. unsigned incomplisoout:1;
  262. unsigned fetsusp:1;
  263. unsigned resetdet:1;
  264. unsigned portintr:1;
  265. unsigned hcintr:1;
  266. unsigned ptxfempty:1;
  267. unsigned lpmtranrcvd:1;
  268. unsigned conidstschng:1;
  269. unsigned disconnect:1;
  270. unsigned sessreqintr:1;
  271. unsigned wkupintr:1;
  272. } b;
  273. } gintmsk_data_t;
  274. /**
  275. * This union represents the bit fields in the Device EP Control
  276. * Register. Read the register into the <i>d32</i> member then
  277. * set/clear the bits using the <i>b</i>it elements.
  278. */
  279. typedef union depctl_data {
  280. /** raw register data */
  281. uint32_t d32;
  282. /** register bits */
  283. struct {
  284. /** Maximum Packet Size
  285. * IN/OUT EPn
  286. * IN/OUT EP0 - 2 bits
  287. * 2'b00: 64 Bytes
  288. * 2'b01: 32
  289. * 2'b10: 16
  290. * 2'b11: 8 */
  291. unsigned mps:11;
  292. #define DWC_DEP0CTL_MPS_64 0
  293. #define DWC_DEP0CTL_MPS_32 1
  294. #define DWC_DEP0CTL_MPS_16 2
  295. #define DWC_DEP0CTL_MPS_8 3
  296. /** Next Endpoint
  297. * IN EPn/IN EP0
  298. * OUT EPn/OUT EP0 - reserved */
  299. unsigned nextep:4;
  300. /** USB Active Endpoint */
  301. unsigned usbactep:1;
  302. /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  303. * This field contains the PID of the packet going to
  304. * be received or transmitted on this endpoint. The
  305. * application should program the PID of the first
  306. * packet going to be received or transmitted on this
  307. * endpoint , after the endpoint is
  308. * activated. Application use the SetD1PID and
  309. * SetD0PID fields of this register to program either
  310. * D0 or D1 PID.
  311. *
  312. * The encoding for this field is
  313. * - 0: D0
  314. * - 1: D1
  315. */
  316. unsigned dpid:1;
  317. /** NAK Status */
  318. unsigned naksts:1;
  319. /** Endpoint Type
  320. * 2'b00: Control
  321. * 2'b01: Isochronous
  322. * 2'b10: Bulk
  323. * 2'b11: Interrupt */
  324. unsigned eptype:2;
  325. /** Snoop Mode
  326. * OUT EPn/OUT EP0
  327. * IN EPn/IN EP0 - reserved */
  328. unsigned snp:1;
  329. /** Stall Handshake */
  330. unsigned stall:1;
  331. /** Tx Fifo Number
  332. * IN EPn/IN EP0
  333. * OUT EPn/OUT EP0 - reserved */
  334. unsigned txfnum:4;
  335. /** Clear NAK */
  336. unsigned cnak:1;
  337. /** Set NAK */
  338. unsigned snak:1;
  339. /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  340. * Writing to this field sets the Endpoint DPID (DPID)
  341. * field in this register to DATA0. Set Even
  342. * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  343. * Writing to this field sets the Even/Odd
  344. * (micro)frame (EO_FrNum) field to even (micro)
  345. * frame.
  346. */
  347. unsigned setd0pid:1;
  348. /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  349. * Writing to this field sets the Endpoint DPID (DPID)
  350. * field in this register to DATA1 Set Odd
  351. * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  352. * Writing to this field sets the Even/Odd
  353. * (micro)frame (EO_FrNum) field to odd (micro) frame.
  354. */
  355. unsigned setd1pid:1;
  356. /** Endpoint Disable */
  357. unsigned epdis:1;
  358. /** Endpoint Enable */
  359. unsigned epena:1;
  360. } b;
  361. } depctl_data_t;
  362. /**
  363. * This union represents the bit fields in the Device IN EP Interrupt
  364. * Register and the Device IN EP Common Mask Register.
  365. *
  366. * - Read the register into the <i>d32</i> member then set/clear the
  367. * bits using the <i>b</i>it elements.
  368. */
  369. typedef union diepint_data {
  370. /** raw register data */
  371. uint32_t d32;
  372. /** register bits */
  373. struct {
  374. /** Transfer complete mask */
  375. unsigned xfercompl:1;
  376. /** Endpoint disable mask */
  377. unsigned epdisabled:1;
  378. /** AHB Error mask */
  379. unsigned ahberr:1;
  380. /** TimeOUT Handshake mask (non-ISOC EPs) */
  381. unsigned timeout:1;
  382. /** IN Token received with TxF Empty mask */
  383. unsigned intktxfemp:1;
  384. /** IN Token Received with EP mismatch mask */
  385. unsigned intknepmis:1;
  386. /** IN Endpoint NAK Effective mask */
  387. unsigned inepnakeff:1;
  388. /** Reserved */
  389. unsigned emptyintr:1;
  390. unsigned txfifoundrn:1;
  391. /** BNA Interrupt mask */
  392. unsigned bna:1;
  393. unsigned reserved10_12:3;
  394. /** BNA Interrupt mask */
  395. unsigned nak:1;
  396. unsigned reserved14_31:18;
  397. } b;
  398. } diepint_data_t;
  399. /**
  400. * This union represents the bit fields in the Device IN EP
  401. * Common/Dedicated Interrupt Mask Register.
  402. */
  403. typedef union diepint_data diepmsk_data_t;
  404. /**
  405. * This union represents the bit fields in the Device OUT EP Interrupt
  406. * Registerand Device OUT EP Common Interrupt Mask Register.
  407. *
  408. * - Read the register into the <i>d32</i> member then set/clear the
  409. * bits using the <i>b</i>it elements.
  410. */
  411. typedef union doepint_data {
  412. /** raw register data */
  413. uint32_t d32;
  414. /** register bits */
  415. struct {
  416. /** Transfer complete */
  417. unsigned xfercompl:1;
  418. /** Endpoint disable */
  419. unsigned epdisabled:1;
  420. /** AHB Error */
  421. unsigned ahberr:1;
  422. /** Setup Phase Done (contorl EPs) */
  423. unsigned setup:1;
  424. /** OUT Token Received when Endpoint Disabled */
  425. unsigned outtknepdis:1;
  426. unsigned stsphsercvd:1;
  427. /** Back-to-Back SETUP Packets Received */
  428. unsigned back2backsetup:1;
  429. unsigned reserved7:1;
  430. /** OUT packet Error */
  431. unsigned outpkterr:1;
  432. /** BNA Interrupt */
  433. unsigned bna:1;
  434. unsigned reserved10:1;
  435. /** Packet Drop Status */
  436. unsigned pktdrpsts:1;
  437. /** Babble Interrupt */
  438. unsigned babble:1;
  439. /** NAK Interrupt */
  440. unsigned nak:1;
  441. /** NYET Interrupt */
  442. unsigned nyet:1;
  443. unsigned reserved15_31:17;
  444. } b;
  445. } doepint_data_t;
  446. /**
  447. * This union represents the bit fields in the Device OUT EP
  448. * Common/Dedicated Interrupt Mask Register.
  449. */
  450. typedef union doepint_data doepmsk_data_t;
  451. /**
  452. * This union represents the bit fields in the Device All EP Interrupt
  453. * and Mask Registers.
  454. * - Read the register into the <i>d32</i> member then set/clear the
  455. * bits using the <i>b</i>it elements.
  456. */
  457. typedef union daint_data {
  458. /** raw register data */
  459. uint32_t d32;
  460. /** register bits */
  461. struct {
  462. /** IN Endpoint bits */
  463. unsigned in:16;
  464. /** OUT Endpoint bits */
  465. unsigned out:16;
  466. } ep;
  467. struct {
  468. /** IN Endpoint bits */
  469. unsigned inep0:1;
  470. unsigned inep1:1;
  471. unsigned inep2:1;
  472. unsigned inep3:1;
  473. unsigned inep4:1;
  474. unsigned inep5:1;
  475. unsigned inep6:1;
  476. unsigned inep7:1;
  477. unsigned inep8:1;
  478. unsigned inep9:1;
  479. unsigned inep10:1;
  480. unsigned inep11:1;
  481. unsigned inep12:1;
  482. unsigned inep13:1;
  483. unsigned inep14:1;
  484. unsigned inep15:1;
  485. /** OUT Endpoint bits */
  486. unsigned outep0:1;
  487. unsigned outep1:1;
  488. unsigned outep2:1;
  489. unsigned outep3:1;
  490. unsigned outep4:1;
  491. unsigned outep5:1;
  492. unsigned outep6:1;
  493. unsigned outep7:1;
  494. unsigned outep8:1;
  495. unsigned outep9:1;
  496. unsigned outep10:1;
  497. unsigned outep11:1;
  498. unsigned outep12:1;
  499. unsigned outep13:1;
  500. unsigned outep14:1;
  501. unsigned outep15:1;
  502. } b;
  503. } daint_data_t;
  504. /*
  505. * Functions
  506. */
  507. /* USB Endpoint Callback Events */
  508. #define USB_EVT_SETUP 1 /* Setup Packet */
  509. #define USB_EVT_OUT 2 /* OUT Packet */
  510. #define USB_EVT_IN 3 /* IN Packet */
  511. #define USB_EVT_OUT_NAK 4 /* OUT Packet - Not Acknowledged */
  512. #define USB_EVT_IN_NAK 5 /* IN Packet - Not Acknowledged */
  513. #define USB_EVT_OUT_STALL 6 /* OUT Packet - Stalled */
  514. #define USB_EVT_IN_STALL 7 /* IN Packet - Stalled */
  515. #define USB_EVT_OUT_DMA_EOT 8 /* DMA OUT EP - End of Transfer */
  516. #define USB_EVT_IN_DMA_EOT 9 /* DMA IN EP - End of Transfer */
  517. #define USB_EVT_OUT_DMA_NDR 10 /* DMA OUT EP - New Descriptor Request */
  518. #define USB_EVT_IN_DMA_NDR 11 /* DMA IN EP - New Descriptor Request */
  519. #define USB_EVT_OUT_DMA_ERR 12 /* DMA OUT EP - Error */
  520. #define USB_EVT_IN_DMA_ERR 13 /* DMA IN EP - Error */
  521. #define USB_EVT_SOF 14
  522. void x1000_usbd_init(dwc_handle *dwc);
  523. void dwc_set_address(dwc_handle *dwc,uint8_t address);
  524. int dwc_ep_disable(dwc_handle *dwc,uint8_t epnum);
  525. int dwc_ep_enable(dwc_handle *dwc,uint8_t epnum);
  526. int dwc_set_ep_stall(dwc_handle *dwc,uint8_t epnum);
  527. int dwc_clr_ep_stall(dwc_handle *dwc,uint8_t epnum);
  528. int dwc_enable_in_ep(dwc_handle *dwc,uint8_t epnum);
  529. int dwc_enable_out_ep(dwc_handle *dwc,uint8_t epnum);
  530. void dwc_ep0_status(dwc_handle *dwc);
  531. void dwc_otg_ep0_out_start(dwc_handle *dwc);
  532. void dwc_handle_ep_data_in_phase(dwc_handle *dwc, uint8_t epnum);
  533. void dwc_handle_ep_status_in_phase(dwc_handle *dwc, uint8_t epnum);
  534. void dwc_handle_ep_data_out_phase(dwc_handle *dwc,uint8_t epnum);
  535. void dwc_ep_out_start(dwc_handle *dwc,uint8_t epnum);
  536. int HW_GetPKT(dwc_handle *dwc, uint8_t epnum, uint8_t *buf,int size);
  537. int HW_SendPKT(dwc_handle *dwc, uint8_t epnum, const uint8_t *buf, int size);
  538. extern void x1000_usbd_event_cb(uint8_t epnum,uint32_t event,void *arg);
  539. #endif /* _X1000_DWC_H_ */