atmel_usba_udc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Driver for the Atmel USBA high speed USB device controller
  3. *
  4. * Copyright (C) 2005-2007 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #ifndef __LINUX_USB_GADGET_USBA_UDC_H__
  11. #define __LINUX_USB_GADGET_USBA_UDC_H__
  12. /* USB register offsets */
  13. #define USBA_CTRL 0x0000
  14. #define USBA_FNUM 0x0004
  15. #define USBA_INT_ENB 0x0010
  16. #define USBA_INT_STA 0x0014
  17. #define USBA_INT_CLR 0x0018
  18. #define USBA_EPT_RST 0x001c
  19. #define USBA_TST 0x00e0
  20. /* USB endpoint register offsets */
  21. #define USBA_EPT_CFG 0x0000
  22. #define USBA_EPT_CTL_ENB 0x0004
  23. #define USBA_EPT_CTL_DIS 0x0008
  24. #define USBA_EPT_CTL 0x000c
  25. #define USBA_EPT_SET_STA 0x0014
  26. #define USBA_EPT_CLR_STA 0x0018
  27. #define USBA_EPT_STA 0x001c
  28. /* USB DMA register offsets */
  29. #define USBA_DMA_NXT_DSC 0x0000
  30. #define USBA_DMA_ADDRESS 0x0004
  31. #define USBA_DMA_CONTROL 0x0008
  32. #define USBA_DMA_STATUS 0x000c
  33. /* Bitfields in CTRL */
  34. #define USBA_DEV_ADDR_OFFSET 0
  35. #define USBA_DEV_ADDR_SIZE 7
  36. #define USBA_FADDR_EN (1 << 7)
  37. #define USBA_EN_USBA (1 << 8)
  38. #define USBA_DETACH (1 << 9)
  39. #define USBA_REMOTE_WAKE_UP (1 << 10)
  40. #define USBA_PULLD_DIS (1 << 11)
  41. #if defined(CONFIG_AVR32)
  42. #define USBA_ENABLE_MASK USBA_EN_USBA
  43. #define USBA_DISABLE_MASK 0
  44. #elif defined(CONFIG_ARCH_AT91)
  45. #define USBA_ENABLE_MASK (USBA_EN_USBA | USBA_PULLD_DIS)
  46. #define USBA_DISABLE_MASK USBA_DETACH
  47. #endif /* CONFIG_ARCH_AT91 */
  48. /* Bitfields in FNUM */
  49. #define USBA_MICRO_FRAME_NUM_OFFSET 0
  50. #define USBA_MICRO_FRAME_NUM_SIZE 3
  51. #define USBA_FRAME_NUMBER_OFFSET 3
  52. #define USBA_FRAME_NUMBER_SIZE 11
  53. #define USBA_FRAME_NUM_ERROR (1 << 31)
  54. /* Bitfields in INT_ENB/INT_STA/INT_CLR */
  55. #define USBA_HIGH_SPEED (1 << 0)
  56. #define USBA_DET_SUSPEND (1 << 1)
  57. #define USBA_MICRO_SOF (1 << 2)
  58. #define USBA_SOF (1 << 3)
  59. #define USBA_END_OF_RESET (1 << 4)
  60. #define USBA_WAKE_UP (1 << 5)
  61. #define USBA_END_OF_RESUME (1 << 6)
  62. #define USBA_UPSTREAM_RESUME (1 << 7)
  63. #define USBA_EPT_INT_OFFSET 8
  64. #define USBA_EPT_INT_SIZE 16
  65. #define USBA_DMA_INT_OFFSET 24
  66. #define USBA_DMA_INT_SIZE 8
  67. /* Bitfields in EPT_RST */
  68. #define USBA_RST_OFFSET 0
  69. #define USBA_RST_SIZE 16
  70. /* Bitfields in USBA_TST */
  71. #define USBA_SPEED_CFG_OFFSET 0
  72. #define USBA_SPEED_CFG_SIZE 2
  73. #define USBA_TST_J_MODE (1 << 2)
  74. #define USBA_TST_K_MODE (1 << 3)
  75. #define USBA_TST_PKT_MODE (1 << 4)
  76. #define USBA_OPMODE2 (1 << 5)
  77. /* Bitfields in EPT_CFG */
  78. #define USBA_EPT_SIZE_OFFSET 0
  79. #define USBA_EPT_SIZE_SIZE 3
  80. #define USBA_EPT_DIR_IN (1 << 3)
  81. #define USBA_EPT_TYPE_OFFSET 4
  82. #define USBA_EPT_TYPE_SIZE 2
  83. #define USBA_BK_NUMBER_OFFSET 6
  84. #define USBA_BK_NUMBER_SIZE 2
  85. #define USBA_NB_TRANS_OFFSET 8
  86. #define USBA_NB_TRANS_SIZE 2
  87. #define USBA_EPT_MAPPED (1 << 31)
  88. /* Bitfields in EPT_CTL/EPT_CTL_ENB/EPT_CTL_DIS */
  89. #define USBA_EPT_ENABLE (1 << 0)
  90. #define USBA_AUTO_VALID (1 << 1)
  91. #define USBA_INTDIS_DMA (1 << 3)
  92. #define USBA_NYET_DIS (1 << 4)
  93. #define USBA_DATAX_RX (1 << 6)
  94. #define USBA_MDATA_RX (1 << 7)
  95. /* Bits 8-15 and 31 enable interrupts for respective bits in EPT_STA */
  96. #define USBA_BUSY_BANK_IE (1 << 18)
  97. /* Bitfields in EPT_SET_STA/EPT_CLR_STA/EPT_STA */
  98. #define USBA_FORCE_STALL (1 << 5)
  99. #define USBA_TOGGLE_CLR (1 << 6)
  100. #define USBA_TOGGLE_SEQ_OFFSET 6
  101. #define USBA_TOGGLE_SEQ_SIZE 2
  102. #define USBA_ERR_OVFLW (1 << 8)
  103. #define USBA_RX_BK_RDY (1 << 9)
  104. #define USBA_KILL_BANK (1 << 9)
  105. #define USBA_TX_COMPLETE (1 << 10)
  106. #define USBA_TX_PK_RDY (1 << 11)
  107. #define USBA_ISO_ERR_TRANS (1 << 11)
  108. #define USBA_RX_SETUP (1 << 12)
  109. #define USBA_ISO_ERR_FLOW (1 << 12)
  110. #define USBA_STALL_SENT (1 << 13)
  111. #define USBA_ISO_ERR_CRC (1 << 13)
  112. #define USBA_ISO_ERR_NBTRANS (1 << 13)
  113. #define USBA_NAK_IN (1 << 14)
  114. #define USBA_ISO_ERR_FLUSH (1 << 14)
  115. #define USBA_NAK_OUT (1 << 15)
  116. #define USBA_CURRENT_BANK_OFFSET 16
  117. #define USBA_CURRENT_BANK_SIZE 2
  118. #define USBA_BUSY_BANKS_OFFSET 18
  119. #define USBA_BUSY_BANKS_SIZE 2
  120. #define USBA_BYTE_COUNT_OFFSET 20
  121. #define USBA_BYTE_COUNT_SIZE 11
  122. #define USBA_SHORT_PACKET (1 << 31)
  123. /* Bitfields in DMA_CONTROL */
  124. #define USBA_DMA_CH_EN (1 << 0)
  125. #define USBA_DMA_LINK (1 << 1)
  126. #define USBA_DMA_END_TR_EN (1 << 2)
  127. #define USBA_DMA_END_BUF_EN (1 << 3)
  128. #define USBA_DMA_END_TR_IE (1 << 4)
  129. #define USBA_DMA_END_BUF_IE (1 << 5)
  130. #define USBA_DMA_DESC_LOAD_IE (1 << 6)
  131. #define USBA_DMA_BURST_LOCK (1 << 7)
  132. #define USBA_DMA_BUF_LEN_OFFSET 16
  133. #define USBA_DMA_BUF_LEN_SIZE 16
  134. /* Bitfields in DMA_STATUS */
  135. #define USBA_DMA_CH_ACTIVE (1 << 1)
  136. #define USBA_DMA_END_TR_ST (1 << 4)
  137. #define USBA_DMA_END_BUF_ST (1 << 5)
  138. #define USBA_DMA_DESC_LOAD_ST (1 << 6)
  139. /* Constants for SPEED_CFG */
  140. #define USBA_SPEED_CFG_NORMAL 0
  141. #define USBA_SPEED_CFG_FORCE_HIGH 2
  142. #define USBA_SPEED_CFG_FORCE_FULL 3
  143. /* Constants for EPT_SIZE */
  144. #define USBA_EPT_SIZE_8 0
  145. #define USBA_EPT_SIZE_16 1
  146. #define USBA_EPT_SIZE_32 2
  147. #define USBA_EPT_SIZE_64 3
  148. #define USBA_EPT_SIZE_128 4
  149. #define USBA_EPT_SIZE_256 5
  150. #define USBA_EPT_SIZE_512 6
  151. #define USBA_EPT_SIZE_1024 7
  152. /* Constants for EPT_TYPE */
  153. #define USBA_EPT_TYPE_CONTROL 0
  154. #define USBA_EPT_TYPE_ISO 1
  155. #define USBA_EPT_TYPE_BULK 2
  156. #define USBA_EPT_TYPE_INT 3
  157. /* Constants for BK_NUMBER */
  158. #define USBA_BK_NUMBER_ZERO 0
  159. #define USBA_BK_NUMBER_ONE 1
  160. #define USBA_BK_NUMBER_DOUBLE 2
  161. #define USBA_BK_NUMBER_TRIPLE 3
  162. /* Bit manipulation macros */
  163. #define USBA_BF(name, value) \
  164. (((value) & ((1 << USBA_##name##_SIZE) - 1)) \
  165. << USBA_##name##_OFFSET)
  166. #define USBA_BFEXT(name, value) \
  167. (((value) >> USBA_##name##_OFFSET) \
  168. & ((1 << USBA_##name##_SIZE) - 1))
  169. #define USBA_BFINS(name, value, old) \
  170. (((old) & ~(((1 << USBA_##name##_SIZE) - 1) \
  171. << USBA_##name##_OFFSET)) \
  172. | USBA_BF(name, value))
  173. /* Register access macros */
  174. #ifdef CONFIG_AVR32
  175. #define usba_io_readl __raw_readl
  176. #define usba_io_writel __raw_writel
  177. #define usba_io_writew __raw_writew
  178. #else
  179. #define usba_io_readl readl_relaxed
  180. #define usba_io_writel writel_relaxed
  181. #define usba_io_writew writew_relaxed
  182. #endif
  183. #define usba_readl(udc, reg) \
  184. usba_io_readl((udc)->regs + USBA_##reg)
  185. #define usba_writel(udc, reg, value) \
  186. usba_io_writel((value), (udc)->regs + USBA_##reg)
  187. #define usba_ep_readl(ep, reg) \
  188. usba_io_readl((ep)->ep_regs + USBA_EPT_##reg)
  189. #define usba_ep_writel(ep, reg, value) \
  190. usba_io_writel((value), (ep)->ep_regs + USBA_EPT_##reg)
  191. #define usba_dma_readl(ep, reg) \
  192. usba_io_readl((ep)->dma_regs + USBA_DMA_##reg)
  193. #define usba_dma_writel(ep, reg, value) \
  194. usba_io_writel((value), (ep)->dma_regs + USBA_DMA_##reg)
  195. /* Calculate base address for a given endpoint or DMA controller */
  196. #define USBA_EPT_BASE(x) (0x100 + (x) * 0x20)
  197. #define USBA_DMA_BASE(x) (0x300 + (x) * 0x10)
  198. #define USBA_FIFO_BASE(x) ((x) << 16)
  199. /* Synth parameters */
  200. #define USBA_NR_DMAS 7
  201. #define EP0_FIFO_SIZE 64
  202. #define EP0_EPT_SIZE USBA_EPT_SIZE_64
  203. #define EP0_NR_BANKS 1
  204. #define FIFO_IOMEM_ID 0
  205. #define CTRL_IOMEM_ID 1
  206. #define DBG_ERR 0x0001 /* report all error returns */
  207. #define DBG_HW 0x0002 /* debug hardware initialization */
  208. #define DBG_GADGET 0x0004 /* calls to/from gadget driver */
  209. #define DBG_INT 0x0008 /* interrupts */
  210. #define DBG_BUS 0x0010 /* report changes in bus state */
  211. #define DBG_QUEUE 0x0020 /* debug request queue processing */
  212. #define DBG_FIFO 0x0040 /* debug FIFO contents */
  213. #define DBG_DMA 0x0080 /* debug DMA handling */
  214. #define DBG_REQ 0x0100 /* print out queued request length */
  215. #define DBG_ALL 0xffff
  216. #define DBG_NONE 0x0000
  217. #define DEBUG_LEVEL (DBG_ERR)
  218. #define DBG(level, fmt, ...) \
  219. do { \
  220. if ((level) & DEBUG_LEVEL) \
  221. pr_debug("udc: " fmt, ## __VA_ARGS__); \
  222. } while (0)
  223. enum usba_ctrl_state {
  224. WAIT_FOR_SETUP,
  225. DATA_STAGE_IN,
  226. DATA_STAGE_OUT,
  227. STATUS_STAGE_IN,
  228. STATUS_STAGE_OUT,
  229. STATUS_STAGE_ADDR,
  230. STATUS_STAGE_TEST,
  231. };
  232. /*
  233. EP_STATE_IDLE,
  234. EP_STATE_SETUP,
  235. EP_STATE_IN_DATA,
  236. EP_STATE_OUT_DATA,
  237. EP_STATE_SET_ADDR_STATUS,
  238. EP_STATE_RX_STATUS,
  239. EP_STATE_TX_STATUS,
  240. EP_STATE_HALT,
  241. */
  242. struct usba_dma_desc {
  243. dma_addr_t next;
  244. dma_addr_t addr;
  245. u32 ctrl;
  246. };
  247. struct usba_ep {
  248. int state;
  249. void __iomem *ep_regs;
  250. void __iomem *dma_regs;
  251. void __iomem *fifo;
  252. struct usb_ep ep;
  253. struct usba_udc *udc;
  254. struct list_head queue;
  255. u16 fifo_size;
  256. u8 nr_banks;
  257. u8 index;
  258. unsigned int can_dma:1;
  259. unsigned int can_isoc:1;
  260. unsigned int is_isoc:1;
  261. unsigned int is_in:1;
  262. #ifdef CONFIG_USB_GADGET_DEBUG_FS
  263. u32 last_dma_status;
  264. struct dentry *debugfs_dir;
  265. struct dentry *debugfs_queue;
  266. struct dentry *debugfs_dma_status;
  267. struct dentry *debugfs_state;
  268. #endif
  269. };
  270. struct usba_request {
  271. struct usb_request req;
  272. struct list_head queue;
  273. u32 ctrl;
  274. unsigned int submitted:1;
  275. unsigned int last_transaction:1;
  276. unsigned int using_dma:1;
  277. unsigned int mapped:1;
  278. };
  279. struct usba_udc_errata {
  280. void (*toggle_bias)(struct usba_udc *udc, int is_on);
  281. void (*pulse_bias)(struct usba_udc *udc);
  282. };
  283. struct usba_udc {
  284. /* Protect hw registers from concurrent modifications */
  285. spinlock_t lock;
  286. /* Mutex to prevent concurrent start or stop */
  287. struct mutex vbus_mutex;
  288. void __iomem *regs;
  289. void __iomem *fifo;
  290. struct usb_gadget gadget;
  291. struct usb_gadget_driver *driver;
  292. struct platform_device *pdev;
  293. const struct usba_udc_errata *errata;
  294. int irq;
  295. int vbus_pin;
  296. int vbus_pin_inverted;
  297. int num_ep;
  298. struct clk *pclk;
  299. struct clk *hclk;
  300. struct usba_ep *usba_ep;
  301. bool bias_pulse_needed;
  302. bool clocked;
  303. u16 devstatus;
  304. u16 test_mode;
  305. int vbus_prev;
  306. u32 int_enb_cache;
  307. #ifdef CONFIG_USB_GADGET_DEBUG_FS
  308. struct dentry *debugfs_root;
  309. struct dentry *debugfs_regs;
  310. #endif
  311. };
  312. static inline struct usba_ep *to_usba_ep(struct usb_ep *ep)
  313. {
  314. return container_of(ep, struct usba_ep, ep);
  315. }
  316. static inline struct usba_request *to_usba_req(struct usb_request *req)
  317. {
  318. return container_of(req, struct usba_request, req);
  319. }
  320. static inline struct usba_udc *to_usba_udc(struct usb_gadget *gadget)
  321. {
  322. return container_of(gadget, struct usba_udc, gadget);
  323. }
  324. #define ep_is_control(ep) ((ep)->index == 0)
  325. #define ep_is_idle(ep) ((ep)->state == EP_STATE_IDLE)
  326. #endif /* __LINUX_USB_GADGET_USBA_UDC_H */