fotg210-udc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Faraday FOTG210 USB OTG controller
  4. *
  5. * Copyright (C) 2013 Faraday Technology Corporation
  6. * Author: Yuan-Hsin Chen <yhchen@faraday-tech.com>
  7. */
  8. #include <linux/kernel.h>
  9. #define FOTG210_MAX_NUM_EP 5 /* ep0...ep4 */
  10. #define FOTG210_MAX_FIFO_NUM 4 /* fifo0...fifo4 */
  11. /* Global Mask of HC/OTG/DEV interrupt Register(0xC4) */
  12. #define FOTG210_GMIR 0xC4
  13. #define GMIR_INT_POLARITY 0x8 /*Active High*/
  14. #define GMIR_MHC_INT 0x4
  15. #define GMIR_MOTG_INT 0x2
  16. #define GMIR_MDEV_INT 0x1
  17. /* Device Main Control Register(0x100) */
  18. #define FOTG210_DMCR 0x100
  19. #define DMCR_HS_EN (1 << 6)
  20. #define DMCR_CHIP_EN (1 << 5)
  21. #define DMCR_SFRST (1 << 4)
  22. #define DMCR_GOSUSP (1 << 3)
  23. #define DMCR_GLINT_EN (1 << 2)
  24. #define DMCR_HALF_SPEED (1 << 1)
  25. #define DMCR_CAP_RMWAKUP (1 << 0)
  26. /* Device Address Register(0x104) */
  27. #define FOTG210_DAR 0x104
  28. #define DAR_AFT_CONF (1 << 7)
  29. /* Device Test Register(0x108) */
  30. #define FOTG210_DTR 0x108
  31. #define DTR_TST_CLRFF (1 << 0)
  32. /* PHY Test Mode Selector register(0x114) */
  33. #define FOTG210_PHYTMSR 0x114
  34. #define PHYTMSR_TST_PKT (1 << 4)
  35. #define PHYTMSR_TST_SE0NAK (1 << 3)
  36. #define PHYTMSR_TST_KSTA (1 << 2)
  37. #define PHYTMSR_TST_JSTA (1 << 1)
  38. #define PHYTMSR_UNPLUG (1 << 0)
  39. /* Cx configuration and FIFO Empty Status register(0x120) */
  40. #define FOTG210_DCFESR 0x120
  41. #define DCFESR_FIFO_EMPTY(fifo) (1 << 8 << (fifo))
  42. #define DCFESR_CX_EMP (1 << 5)
  43. #define DCFESR_CX_CLR (1 << 3)
  44. #define DCFESR_CX_STL (1 << 2)
  45. #define DCFESR_TST_PKDONE (1 << 1)
  46. #define DCFESR_CX_DONE (1 << 0)
  47. /* Device IDLE Counter Register(0x124) */
  48. #define FOTG210_DICR 0x124
  49. /* Device Mask of Interrupt Group Register (0x130) */
  50. #define FOTG210_DMIGR 0x130
  51. #define DMIGR_MINT_G2 (1 << 2)
  52. #define DMIGR_MINT_G1 (1 << 1)
  53. #define DMIGR_MINT_G0 (1 << 0)
  54. /* Device Mask of Interrupt Source Group 0(0x134) */
  55. #define FOTG210_DMISGR0 0x134
  56. #define DMISGR0_MCX_COMEND (1 << 3)
  57. #define DMISGR0_MCX_OUT_INT (1 << 2)
  58. #define DMISGR0_MCX_IN_INT (1 << 1)
  59. #define DMISGR0_MCX_SETUP_INT (1 << 0)
  60. /* Device Mask of Interrupt Source Group 1 Register(0x138)*/
  61. #define FOTG210_DMISGR1 0x138
  62. #define DMISGR1_MF3_IN_INT (1 << 19)
  63. #define DMISGR1_MF2_IN_INT (1 << 18)
  64. #define DMISGR1_MF1_IN_INT (1 << 17)
  65. #define DMISGR1_MF0_IN_INT (1 << 16)
  66. #define DMISGR1_MF_IN_INT(fifo) (1 << (16 + (fifo)))
  67. #define DMISGR1_MF3_SPK_INT (1 << 7)
  68. #define DMISGR1_MF3_OUT_INT (1 << 6)
  69. #define DMISGR1_MF2_SPK_INT (1 << 5)
  70. #define DMISGR1_MF2_OUT_INT (1 << 4)
  71. #define DMISGR1_MF1_SPK_INT (1 << 3)
  72. #define DMISGR1_MF1_OUT_INT (1 << 2)
  73. #define DMISGR1_MF0_SPK_INT (1 << 1)
  74. #define DMISGR1_MF0_OUT_INT (1 << 0)
  75. #define DMISGR1_MF_OUTSPK_INT(fifo) (0x3 << (fifo) * 2)
  76. /* Device Mask of Interrupt Source Group 2 Register (0x13C) */
  77. #define FOTG210_DMISGR2 0x13C
  78. #define DMISGR2_MDMA_ERROR (1 << 8)
  79. #define DMISGR2_MDMA_CMPLT (1 << 7)
  80. /* Device Interrupt group Register (0x140) */
  81. #define FOTG210_DIGR 0x140
  82. #define DIGR_INT_G2 (1 << 2)
  83. #define DIGR_INT_G1 (1 << 1)
  84. #define DIGR_INT_G0 (1 << 0)
  85. /* Device Interrupt Source Group 0 Register (0x144) */
  86. #define FOTG210_DISGR0 0x144
  87. #define DISGR0_CX_COMABT_INT (1 << 5)
  88. #define DISGR0_CX_COMFAIL_INT (1 << 4)
  89. #define DISGR0_CX_COMEND_INT (1 << 3)
  90. #define DISGR0_CX_OUT_INT (1 << 2)
  91. #define DISGR0_CX_IN_INT (1 << 1)
  92. #define DISGR0_CX_SETUP_INT (1 << 0)
  93. /* Device Interrupt Source Group 1 Register (0x148) */
  94. #define FOTG210_DISGR1 0x148
  95. #define DISGR1_OUT_INT(fifo) (1 << ((fifo) * 2))
  96. #define DISGR1_SPK_INT(fifo) (1 << 1 << ((fifo) * 2))
  97. #define DISGR1_IN_INT(fifo) (1 << 16 << (fifo))
  98. /* Device Interrupt Source Group 2 Register (0x14C) */
  99. #define FOTG210_DISGR2 0x14C
  100. #define DISGR2_DMA_ERROR (1 << 8)
  101. #define DISGR2_DMA_CMPLT (1 << 7)
  102. #define DISGR2_RX0BYTE_INT (1 << 6)
  103. #define DISGR2_TX0BYTE_INT (1 << 5)
  104. #define DISGR2_ISO_SEQ_ABORT_INT (1 << 4)
  105. #define DISGR2_ISO_SEQ_ERR_INT (1 << 3)
  106. #define DISGR2_RESM_INT (1 << 2)
  107. #define DISGR2_SUSP_INT (1 << 1)
  108. #define DISGR2_USBRST_INT (1 << 0)
  109. /* Device Receive Zero-Length Data Packet Register (0x150)*/
  110. #define FOTG210_RX0BYTE 0x150
  111. #define RX0BYTE_EP8 (1 << 7)
  112. #define RX0BYTE_EP7 (1 << 6)
  113. #define RX0BYTE_EP6 (1 << 5)
  114. #define RX0BYTE_EP5 (1 << 4)
  115. #define RX0BYTE_EP4 (1 << 3)
  116. #define RX0BYTE_EP3 (1 << 2)
  117. #define RX0BYTE_EP2 (1 << 1)
  118. #define RX0BYTE_EP1 (1 << 0)
  119. /* Device Transfer Zero-Length Data Packet Register (0x154)*/
  120. #define FOTG210_TX0BYTE 0x154
  121. #define TX0BYTE_EP8 (1 << 7)
  122. #define TX0BYTE_EP7 (1 << 6)
  123. #define TX0BYTE_EP6 (1 << 5)
  124. #define TX0BYTE_EP5 (1 << 4)
  125. #define TX0BYTE_EP4 (1 << 3)
  126. #define TX0BYTE_EP3 (1 << 2)
  127. #define TX0BYTE_EP2 (1 << 1)
  128. #define TX0BYTE_EP1 (1 << 0)
  129. /* Device IN Endpoint x MaxPacketSize Register(0x160+4*(x-1)) */
  130. #define FOTG210_INEPMPSR(ep) (0x160 + 4 * ((ep) - 1))
  131. #define INOUTEPMPSR_MPS(mps) ((mps) & 0x2FF)
  132. #define INOUTEPMPSR_STL_EP (1 << 11)
  133. #define INOUTEPMPSR_RESET_TSEQ (1 << 12)
  134. /* Device OUT Endpoint x MaxPacketSize Register(0x180+4*(x-1)) */
  135. #define FOTG210_OUTEPMPSR(ep) (0x180 + 4 * ((ep) - 1))
  136. /* Device Endpoint 1~4 Map Register (0x1A0) */
  137. #define FOTG210_EPMAP 0x1A0
  138. #define EPMAP_FIFONO(ep, dir) \
  139. ((((ep) - 1) << ((ep) - 1) * 8) << ((dir) ? 0 : 4))
  140. #define EPMAP_FIFONOMSK(ep, dir) \
  141. ((3 << ((ep) - 1) * 8) << ((dir) ? 0 : 4))
  142. /* Device FIFO Map Register (0x1A8) */
  143. #define FOTG210_FIFOMAP 0x1A8
  144. #define FIFOMAP_DIROUT(fifo) (0x0 << 4 << (fifo) * 8)
  145. #define FIFOMAP_DIRIN(fifo) (0x1 << 4 << (fifo) * 8)
  146. #define FIFOMAP_BIDIR(fifo) (0x2 << 4 << (fifo) * 8)
  147. #define FIFOMAP_NA(fifo) (0x3 << 4 << (fifo) * 8)
  148. #define FIFOMAP_EPNO(ep) ((ep) << ((ep) - 1) * 8)
  149. #define FIFOMAP_EPNOMSK(ep) (0xF << ((ep) - 1) * 8)
  150. /* Device FIFO Confuguration Register (0x1AC) */
  151. #define FOTG210_FIFOCF 0x1AC
  152. #define FIFOCF_TYPE(type, fifo) ((type) << (fifo) * 8)
  153. #define FIFOCF_BLK_SIN(fifo) (0x0 << (fifo) * 8 << 2)
  154. #define FIFOCF_BLK_DUB(fifo) (0x1 << (fifo) * 8 << 2)
  155. #define FIFOCF_BLK_TRI(fifo) (0x2 << (fifo) * 8 << 2)
  156. #define FIFOCF_BLKSZ_512(fifo) (0x0 << (fifo) * 8 << 4)
  157. #define FIFOCF_BLKSZ_1024(fifo) (0x1 << (fifo) * 8 << 4)
  158. #define FIFOCF_FIFO_EN(fifo) (0x1 << (fifo) * 8 << 5)
  159. /* Device FIFO n Instruction and Byte Count Register (0x1B0+4*n) */
  160. #define FOTG210_FIBCR(fifo) (0x1B0 + (fifo) * 4)
  161. #define FIBCR_BCFX 0x7FF
  162. #define FIBCR_FFRST (1 << 12)
  163. /* Device DMA Target FIFO Number Register (0x1C0) */
  164. #define FOTG210_DMATFNR 0x1C0
  165. #define DMATFNR_ACC_CXF (1 << 4)
  166. #define DMATFNR_ACC_F3 (1 << 3)
  167. #define DMATFNR_ACC_F2 (1 << 2)
  168. #define DMATFNR_ACC_F1 (1 << 1)
  169. #define DMATFNR_ACC_F0 (1 << 0)
  170. #define DMATFNR_ACC_FN(fifo) (1 << (fifo))
  171. #define DMATFNR_DISDMA 0
  172. /* Device DMA Controller Parameter setting 1 Register (0x1C8) */
  173. #define FOTG210_DMACPSR1 0x1C8
  174. #define DMACPSR1_DMA_LEN(len) (((len) & 0xFFFF) << 8)
  175. #define DMACPSR1_DMA_ABORT (1 << 3)
  176. #define DMACPSR1_DMA_TYPE(dir_in) (((dir_in) ? 1 : 0) << 1)
  177. #define DMACPSR1_DMA_START (1 << 0)
  178. /* Device DMA Controller Parameter setting 2 Register (0x1CC) */
  179. #define FOTG210_DMACPSR2 0x1CC
  180. /* Device DMA Controller Parameter setting 3 Register (0x1CC) */
  181. #define FOTG210_CXPORT 0x1D0
  182. struct fotg210_request {
  183. struct usb_request req;
  184. struct list_head queue;
  185. };
  186. struct fotg210_ep {
  187. struct usb_ep ep;
  188. struct fotg210_udc *fotg210;
  189. struct list_head queue;
  190. unsigned stall:1;
  191. unsigned wedged:1;
  192. unsigned use_dma:1;
  193. unsigned char epnum;
  194. unsigned char type;
  195. unsigned char dir_in;
  196. unsigned int maxp;
  197. const struct usb_endpoint_descriptor *desc;
  198. };
  199. struct fotg210_udc {
  200. spinlock_t lock; /* protect the struct */
  201. void __iomem *reg;
  202. unsigned long irq_trigger;
  203. struct device *dev;
  204. struct fotg210 *fotg;
  205. struct usb_phy *phy;
  206. struct usb_gadget gadget;
  207. struct usb_gadget_driver *driver;
  208. struct fotg210_ep *ep[FOTG210_MAX_NUM_EP];
  209. struct usb_request *ep0_req; /* for internal request */
  210. __le16 ep0_data;
  211. u8 ep0_dir; /* 0/0x80 out/in */
  212. u8 reenum; /* if re-enumeration */
  213. };
  214. #define gadget_to_fotg210(g) container_of((g), struct fotg210_udc, gadget)