imx_udc.h 12 KB

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