imx_udc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (C) 2005 Mike Lee(eemike@gmail.com)
  3. *
  4. * This udc driver is now under testing and code is based on pxa2xx_udc.h
  5. * Please use it with your own risk!
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #ifndef __LINUX_USB_GADGET_IMX_H
  18. #define __LINUX_USB_GADGET_IMX_H
  19. #include <linux/types.h>
  20. /* Helper macros */
  21. #define EP_NO(ep) ((ep->bEndpointAddress) & ~USB_DIR_IN) /* IN:1, OUT:0 */
  22. #define EP_DIR(ep) ((ep->bEndpointAddress) & USB_DIR_IN ? 1 : 0)
  23. #define irq_to_ep(irq) (((irq) >= USBD_INT0) || ((irq) <= USBD_INT6) ? ((irq) - USBD_INT0) : (USBD_INT6)) /*should not happen*/
  24. #define ep_to_irq(ep) (EP_NO((ep)) + USBD_INT0)
  25. #define IMX_USB_NB_EP 6
  26. /* Driver structures */
  27. struct imx_request {
  28. struct usb_request req;
  29. struct list_head queue;
  30. unsigned int in_use;
  31. };
  32. enum ep0_state {
  33. EP0_IDLE,
  34. EP0_IN_DATA_PHASE,
  35. EP0_OUT_DATA_PHASE,
  36. EP0_CONFIG,
  37. EP0_STALL,
  38. };
  39. struct imx_ep_struct {
  40. struct usb_ep ep;
  41. struct imx_udc_struct *imx_usb;
  42. struct list_head queue;
  43. unsigned char stopped;
  44. unsigned char fifosize;
  45. unsigned char bEndpointAddress;
  46. unsigned char bmAttributes;
  47. };
  48. struct imx_udc_struct {
  49. struct usb_gadget gadget;
  50. struct usb_gadget_driver *driver;
  51. struct device *dev;
  52. struct imx_ep_struct imx_ep[IMX_USB_NB_EP];
  53. struct clk *clk;
  54. enum ep0_state ep0state;
  55. struct resource *res;
  56. void __iomem *base;
  57. unsigned char set_config;
  58. int cfg,
  59. intf,
  60. alt,
  61. usbd_int[7];
  62. };
  63. /* USB registers */
  64. #define USB_FRAME (0x00) /* USB frame */
  65. #define USB_SPEC (0x04) /* USB Spec */
  66. #define USB_STAT (0x08) /* USB Status */
  67. #define USB_CTRL (0x0C) /* USB Control */
  68. #define USB_DADR (0x10) /* USB Desc RAM addr */
  69. #define USB_DDAT (0x14) /* USB Desc RAM/EP buffer data */
  70. #define USB_INTR (0x18) /* USB interrupt */
  71. #define USB_MASK (0x1C) /* USB Mask */
  72. #define USB_ENAB (0x24) /* USB Enable */
  73. #define USB_EP_STAT(x) (0x30 + (x*0x30)) /* USB status/control */
  74. #define USB_EP_INTR(x) (0x34 + (x*0x30)) /* USB interrupt */
  75. #define USB_EP_MASK(x) (0x38 + (x*0x30)) /* USB mask */
  76. #define USB_EP_FDAT(x) (0x3C + (x*0x30)) /* USB FIFO data */
  77. #define USB_EP_FDAT0(x) (0x3C + (x*0x30)) /* USB FIFO data */
  78. #define USB_EP_FDAT1(x) (0x3D + (x*0x30)) /* USB FIFO data */
  79. #define USB_EP_FDAT2(x) (0x3E + (x*0x30)) /* USB FIFO data */
  80. #define USB_EP_FDAT3(x) (0x3F + (x*0x30)) /* USB FIFO data */
  81. #define USB_EP_FSTAT(x) (0x40 + (x*0x30)) /* USB FIFO status */
  82. #define USB_EP_FCTRL(x) (0x44 + (x*0x30)) /* USB FIFO control */
  83. #define USB_EP_LRFP(x) (0x48 + (x*0x30)) /* USB last read frame pointer */
  84. #define USB_EP_LWFP(x) (0x4C + (x*0x30)) /* USB last write frame pointer */
  85. #define USB_EP_FALRM(x) (0x50 + (x*0x30)) /* USB FIFO alarm */
  86. #define USB_EP_FRDP(x) (0x54 + (x*0x30)) /* USB FIFO read pointer */
  87. #define USB_EP_FWRP(x) (0x58 + (x*0x30)) /* USB FIFO write pointer */
  88. /* USB Control Register Bit Fields.*/
  89. #define CTRL_CMDOVER (1<<6) /* UDC status */
  90. #define CTRL_CMDERROR (1<<5) /* UDC status */
  91. #define CTRL_FE_ENA (1<<3) /* Enable Font End logic */
  92. #define CTRL_UDC_RST (1<<2) /* UDC reset */
  93. #define CTRL_AFE_ENA (1<<1) /* Analog Font end enable */
  94. #define CTRL_RESUME (1<<0) /* UDC resume */
  95. /* USB Status Register Bit Fields.*/
  96. #define STAT_RST (1<<8)
  97. #define STAT_SUSP (1<<7)
  98. #define STAT_CFG (3<<5)
  99. #define STAT_INTF (3<<3)
  100. #define STAT_ALTSET (7<<0)
  101. /* USB Interrupt Status/Mask Registers Bit fields */
  102. #define INTR_WAKEUP (1<<31) /* Wake up Interrupt */
  103. #define INTR_MSOF (1<<7) /* Missed Start of Frame */
  104. #define INTR_SOF (1<<6) /* Start of Frame */
  105. #define INTR_RESET_STOP (1<<5) /* Reset Signaling stop */
  106. #define INTR_RESET_START (1<<4) /* Reset Signaling start */
  107. #define INTR_RESUME (1<<3) /* Suspend to resume */
  108. #define INTR_SUSPEND (1<<2) /* Active to suspend */
  109. #define INTR_FRAME_MATCH (1<<1) /* Frame matched */
  110. #define INTR_CFG_CHG (1<<0) /* Configuration change occurred */
  111. /* USB Enable Register Bit Fields.*/
  112. #define ENAB_RST (1<<31) /* Reset USB modules */
  113. #define ENAB_ENAB (1<<30) /* Enable USB modules*/
  114. #define ENAB_SUSPEND (1<<29) /* Suspend USB modules */
  115. #define ENAB_ENDIAN (1<<28) /* Endian of USB modules */
  116. #define ENAB_PWRMD (1<<0) /* Power mode of USB modules */
  117. /* USB Descriptor Ram Address Register bit fields */
  118. #define DADR_CFG (1<<31) /* Configuration */
  119. #define DADR_BSY (1<<30) /* Busy status */
  120. #define DADR_DADR (0x1FF) /* Descriptor Ram Address */
  121. /* USB Descriptor RAM/Endpoint Buffer Data Register bit fields */
  122. #define DDAT_DDAT (0xFF) /* Descriptor Endpoint Buffer */
  123. /* USB Endpoint Status Register bit fields */
  124. #define EPSTAT_BCOUNT (0x7F<<16) /* Endpoint FIFO byte count */
  125. #define EPSTAT_SIP (1<<8) /* Endpoint setup in progress */
  126. #define EPSTAT_DIR (1<<7) /* Endpoint transfer direction */
  127. #define EPSTAT_MAX (3<<5) /* Endpoint Max packet size */
  128. #define EPSTAT_TYP (3<<3) /* Endpoint type */
  129. #define EPSTAT_ZLPS (1<<2) /* Send zero length packet */
  130. #define EPSTAT_FLUSH (1<<1) /* Endpoint FIFO Flush */
  131. #define EPSTAT_STALL (1<<0) /* Force stall */
  132. /* USB Endpoint FIFO Status Register bit fields */
  133. #define FSTAT_FRAME_STAT (0xF<<24) /* Frame status bit [0-3] */
  134. #define FSTAT_ERR (1<<22) /* FIFO error */
  135. #define FSTAT_UF (1<<21) /* FIFO underflow */
  136. #define FSTAT_OF (1<<20) /* FIFO overflow */
  137. #define FSTAT_FR (1<<19) /* FIFO frame ready */
  138. #define FSTAT_FULL (1<<18) /* FIFO full */
  139. #define FSTAT_ALRM (1<<17) /* FIFO alarm */
  140. #define FSTAT_EMPTY (1<<16) /* FIFO empty */
  141. /* USB Endpoint FIFO Control Register bit fields */
  142. #define FCTRL_WFR (1<<29) /* Write frame end */
  143. /* USB Endpoint Interrupt Status Regsiter bit fields */
  144. #define EPINTR_FIFO_FULL (1<<8) /* fifo full */
  145. #define EPINTR_FIFO_EMPTY (1<<7) /* fifo empty */
  146. #define EPINTR_FIFO_ERROR (1<<6) /* fifo error */
  147. #define EPINTR_FIFO_HIGH (1<<5) /* fifo high */
  148. #define EPINTR_FIFO_LOW (1<<4) /* fifo low */
  149. #define EPINTR_MDEVREQ (1<<3) /* multi Device request */
  150. #define EPINTR_EOT (1<<2) /* fifo end of transfer */
  151. #define EPINTR_DEVREQ (1<<1) /* Device request */
  152. #define EPINTR_EOF (1<<0) /* fifo end of frame */
  153. /* Debug macros */
  154. #ifdef DEBUG
  155. /* #define DEBUG_REQ */
  156. /* #define DEBUG_TRX */
  157. /* #define DEBUG_INIT */
  158. /* #define DEBUG_EP0 */
  159. /* #define DEBUG_EPX */
  160. /* #define DEBUG_IRQ */
  161. /* #define DEBUG_EPIRQ */
  162. /* #define DEBUG_DUMP */
  163. #define DEBUG_ERR
  164. #ifdef DEBUG_REQ
  165. #define D_REQ(dev, args...) dev_dbg(dev, ## args)
  166. #else
  167. #define D_REQ(dev, args...) do {} while (0)
  168. #endif /* DEBUG_REQ */
  169. #ifdef DEBUG_TRX
  170. #define D_TRX(dev, args...) dev_dbg(dev, ## args)
  171. #else
  172. #define D_TRX(dev, args...) do {} while (0)
  173. #endif /* DEBUG_TRX */
  174. #ifdef DEBUG_INIT
  175. #define D_INI(dev, args...) dev_dbg(dev, ## args)
  176. #else
  177. #define D_INI(dev, args...) do {} while (0)
  178. #endif /* DEBUG_INIT */
  179. #ifdef DEBUG_EP0
  180. static const char *state_name[] = {
  181. "EP0_IDLE",
  182. "EP0_IN_DATA_PHASE",
  183. "EP0_OUT_DATA_PHASE",
  184. "EP0_CONFIG",
  185. "EP0_STALL"
  186. };
  187. #define D_EP0(dev, args...) dev_dbg(dev, ## args)
  188. #else
  189. #define D_EP0(dev, args...) do {} while (0)
  190. #endif /* DEBUG_EP0 */
  191. #ifdef DEBUG_EPX
  192. #define D_EPX(dev, args...) dev_dbg(dev, ## args)
  193. #else
  194. #define D_EPX(dev, args...) do {} while (0)
  195. #endif /* DEBUG_EP0 */
  196. #ifdef DEBUG_IRQ
  197. static void dump_intr(const char *label, int irqreg, struct device *dev)
  198. {
  199. dev_dbg(dev, "<%s> USB_INTR=[%s%s%s%s%s%s%s%s%s]\n", label,
  200. (irqreg & INTR_WAKEUP) ? " wake" : "",
  201. (irqreg & INTR_MSOF) ? " msof" : "",
  202. (irqreg & INTR_SOF) ? " sof" : "",
  203. (irqreg & INTR_RESUME) ? " resume" : "",
  204. (irqreg & INTR_SUSPEND) ? " suspend" : "",
  205. (irqreg & INTR_RESET_STOP) ? " noreset" : "",
  206. (irqreg & INTR_RESET_START) ? " reset" : "",
  207. (irqreg & INTR_FRAME_MATCH) ? " fmatch" : "",
  208. (irqreg & INTR_CFG_CHG) ? " config" : "");
  209. }
  210. #else
  211. #define dump_intr(x, y, z) do {} while (0)
  212. #endif /* DEBUG_IRQ */
  213. #ifdef DEBUG_EPIRQ
  214. static void dump_ep_intr(const char *label, int nr, int irqreg, struct device *dev)
  215. {
  216. dev_dbg(dev, "<%s> EP%d_INTR=[%s%s%s%s%s%s%s%s%s]\n", label, nr,
  217. (irqreg & EPINTR_FIFO_FULL) ? " full" : "",
  218. (irqreg & EPINTR_FIFO_EMPTY) ? " fempty" : "",
  219. (irqreg & EPINTR_FIFO_ERROR) ? " ferr" : "",
  220. (irqreg & EPINTR_FIFO_HIGH) ? " fhigh" : "",
  221. (irqreg & EPINTR_FIFO_LOW) ? " flow" : "",
  222. (irqreg & EPINTR_MDEVREQ) ? " mreq" : "",
  223. (irqreg & EPINTR_EOF) ? " eof" : "",
  224. (irqreg & EPINTR_DEVREQ) ? " devreq" : "",
  225. (irqreg & EPINTR_EOT) ? " eot" : "");
  226. }
  227. #else
  228. #define dump_ep_intr(x, y, z, i) do {} while (0)
  229. #endif /* DEBUG_IRQ */
  230. #ifdef DEBUG_DUMP
  231. static void dump_usb_stat(const char *label, struct imx_udc_struct *imx_usb)
  232. {
  233. int temp = __raw_readl(imx_usb->base + USB_STAT);
  234. dev_dbg(imx_usb->dev,
  235. "<%s> USB_STAT=[%s%s CFG=%d, INTF=%d, ALTR=%d]\n", label,
  236. (temp & STAT_RST) ? " reset" : "",
  237. (temp & STAT_SUSP) ? " suspend" : "",
  238. (temp & STAT_CFG) >> 5,
  239. (temp & STAT_INTF) >> 3,
  240. (temp & STAT_ALTSET));
  241. }
  242. static void dump_ep_stat(const char *label, struct imx_ep_struct *imx_ep)
  243. {
  244. int temp = __raw_readl(imx_ep->imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
  245. dev_dbg(imx_ep->imx_usb->dev,
  246. "<%s> EP%d_INTR=[%s%s%s%s%s%s%s%s%s]\n", label, EP_NO(imx_ep),
  247. (temp & EPINTR_FIFO_FULL) ? " full" : "",
  248. (temp & EPINTR_FIFO_EMPTY) ? " fempty" : "",
  249. (temp & EPINTR_FIFO_ERROR) ? " ferr" : "",
  250. (temp & EPINTR_FIFO_HIGH) ? " fhigh" : "",
  251. (temp & EPINTR_FIFO_LOW) ? " flow" : "",
  252. (temp & EPINTR_MDEVREQ) ? " mreq" : "",
  253. (temp & EPINTR_EOF) ? " eof" : "",
  254. (temp & EPINTR_DEVREQ) ? " devreq" : "",
  255. (temp & EPINTR_EOT) ? " eot" : "");
  256. temp = __raw_readl(imx_ep->imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
  257. dev_dbg(imx_ep->imx_usb->dev,
  258. "<%s> EP%d_STAT=[%s%s bcount=%d]\n", label, EP_NO(imx_ep),
  259. (temp & EPSTAT_SIP) ? " sip" : "",
  260. (temp & EPSTAT_STALL) ? " stall" : "",
  261. (temp & EPSTAT_BCOUNT) >> 16);
  262. temp = __raw_readl(imx_ep->imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)));
  263. dev_dbg(imx_ep->imx_usb->dev,
  264. "<%s> EP%d_FSTAT=[%s%s%s%s%s%s%s]\n", label, EP_NO(imx_ep),
  265. (temp & FSTAT_ERR) ? " ferr" : "",
  266. (temp & FSTAT_UF) ? " funder" : "",
  267. (temp & FSTAT_OF) ? " fover" : "",
  268. (temp & FSTAT_FR) ? " fready" : "",
  269. (temp & FSTAT_FULL) ? " ffull" : "",
  270. (temp & FSTAT_ALRM) ? " falarm" : "",
  271. (temp & FSTAT_EMPTY) ? " fempty" : "");
  272. }
  273. static void dump_req(const char *label, struct imx_ep_struct *imx_ep, struct usb_request *req)
  274. {
  275. int i;
  276. if (!req || !req->buf) {
  277. dev_dbg(imx_ep->imx_usb->dev, "<%s> req or req buf is free\n", label);
  278. return;
  279. }
  280. if ((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state == EP0_IN_DATA_PHASE)
  281. || (EP_NO(imx_ep) && EP_DIR(imx_ep))) {
  282. dev_dbg(imx_ep->imx_usb->dev, "<%s> request dump <", label);
  283. for (i = 0; i < req->length; i++)
  284. printk("%02x-", *((u8 *)req->buf + i));
  285. printk(">\n");
  286. }
  287. }
  288. #else
  289. #define dump_ep_stat(x, y) do {} while (0)
  290. #define dump_usb_stat(x, y) do {} while (0)
  291. #define dump_req(x, y, z) do {} while (0)
  292. #endif /* DEBUG_DUMP */
  293. #ifdef DEBUG_ERR
  294. #define D_ERR(dev, args...) dev_dbg(dev, ## args)
  295. #else
  296. #define D_ERR(dev, args...) do {} while (0)
  297. #endif
  298. #else
  299. #define D_REQ(dev, args...) do {} while (0)
  300. #define D_TRX(dev, args...) do {} while (0)
  301. #define D_INI(dev, args...) do {} while (0)
  302. #define D_EP0(dev, args...) do {} while (0)
  303. #define D_EPX(dev, args...) do {} while (0)
  304. #define dump_ep_intr(x, y, z, i) do {} while (0)
  305. #define dump_intr(x, y, z) do {} while (0)
  306. #define dump_ep_stat(x, y) do {} while (0)
  307. #define dump_usb_stat(x, y) do {} while (0)
  308. #define dump_req(x, y, z) do {} while (0)
  309. #define D_ERR(dev, args...) do {} while (0)
  310. #endif /* DEBUG */
  311. #endif /* __LINUX_USB_GADGET_IMX_H */