r8a66597-udc.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * R8A66597 UDC
  3. *
  4. * Copyright (C) 2007-2009 Renesas Solutions Corp.
  5. *
  6. * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #ifndef __R8A66597_H__
  13. #define __R8A66597_H__
  14. #ifdef CONFIG_HAVE_CLK
  15. #include <linux/clk.h>
  16. #endif
  17. #include <linux/usb/r8a66597.h>
  18. #define R8A66597_MAX_SAMPLING 10
  19. #define R8A66597_MAX_NUM_PIPE 8
  20. #define R8A66597_MAX_NUM_BULK 3
  21. #define R8A66597_MAX_NUM_ISOC 2
  22. #define R8A66597_MAX_NUM_INT 2
  23. #define R8A66597_BASE_PIPENUM_BULK 3
  24. #define R8A66597_BASE_PIPENUM_ISOC 1
  25. #define R8A66597_BASE_PIPENUM_INT 6
  26. #define R8A66597_BASE_BUFNUM 6
  27. #define R8A66597_MAX_BUFNUM 0x4F
  28. #define is_bulk_pipe(pipenum) \
  29. ((pipenum >= R8A66597_BASE_PIPENUM_BULK) && \
  30. (pipenum < (R8A66597_BASE_PIPENUM_BULK + R8A66597_MAX_NUM_BULK)))
  31. #define is_interrupt_pipe(pipenum) \
  32. ((pipenum >= R8A66597_BASE_PIPENUM_INT) && \
  33. (pipenum < (R8A66597_BASE_PIPENUM_INT + R8A66597_MAX_NUM_INT)))
  34. #define is_isoc_pipe(pipenum) \
  35. ((pipenum >= R8A66597_BASE_PIPENUM_ISOC) && \
  36. (pipenum < (R8A66597_BASE_PIPENUM_ISOC + R8A66597_MAX_NUM_ISOC)))
  37. #define r8a66597_is_sudmac(r8a66597) (r8a66597->pdata->sudmac)
  38. struct r8a66597_pipe_info {
  39. u16 pipe;
  40. u16 epnum;
  41. u16 maxpacket;
  42. u16 type;
  43. u16 interval;
  44. u16 dir_in;
  45. };
  46. struct r8a66597_request {
  47. struct usb_request req;
  48. struct list_head queue;
  49. };
  50. struct r8a66597_ep {
  51. struct usb_ep ep;
  52. struct r8a66597 *r8a66597;
  53. struct r8a66597_dma *dma;
  54. struct list_head queue;
  55. unsigned busy:1;
  56. unsigned wedge:1;
  57. unsigned internal_ccpl:1; /* use only control */
  58. /* this member can able to after r8a66597_enable */
  59. unsigned use_dma:1;
  60. u16 pipenum;
  61. u16 type;
  62. /* register address */
  63. unsigned char fifoaddr;
  64. unsigned char fifosel;
  65. unsigned char fifoctr;
  66. unsigned char pipectr;
  67. unsigned char pipetre;
  68. unsigned char pipetrn;
  69. };
  70. struct r8a66597_dma {
  71. unsigned used:1;
  72. unsigned dir:1; /* 1 = IN(write), 0 = OUT(read) */
  73. };
  74. struct r8a66597 {
  75. spinlock_t lock;
  76. void __iomem *reg;
  77. void __iomem *sudmac_reg;
  78. #ifdef CONFIG_HAVE_CLK
  79. struct clk *clk;
  80. #endif
  81. struct r8a66597_platdata *pdata;
  82. struct usb_gadget gadget;
  83. struct usb_gadget_driver *driver;
  84. struct r8a66597_ep ep[R8A66597_MAX_NUM_PIPE];
  85. struct r8a66597_ep *pipenum2ep[R8A66597_MAX_NUM_PIPE];
  86. struct r8a66597_ep *epaddr2ep[16];
  87. struct r8a66597_dma dma;
  88. struct timer_list timer;
  89. struct usb_request *ep0_req; /* for internal request */
  90. u16 ep0_data; /* for internal request */
  91. u16 old_vbus;
  92. u16 scount;
  93. u16 old_dvsq;
  94. /* pipe config */
  95. unsigned char bulk;
  96. unsigned char interrupt;
  97. unsigned char isochronous;
  98. unsigned char num_dma;
  99. unsigned irq_sense_low:1;
  100. };
  101. #define gadget_to_r8a66597(_gadget) \
  102. container_of(_gadget, struct r8a66597, gadget)
  103. #define r8a66597_to_gadget(r8a66597) (&r8a66597->gadget)
  104. #define r8a66597_to_dev(r8a66597) (r8a66597->gadget.dev.parent)
  105. static inline u16 r8a66597_read(struct r8a66597 *r8a66597, unsigned long offset)
  106. {
  107. return ioread16(r8a66597->reg + offset);
  108. }
  109. static inline void r8a66597_read_fifo(struct r8a66597 *r8a66597,
  110. unsigned long offset,
  111. unsigned char *buf,
  112. int len)
  113. {
  114. void __iomem *fifoaddr = r8a66597->reg + offset;
  115. unsigned int data = 0;
  116. int i;
  117. if (r8a66597->pdata->on_chip) {
  118. /* 32-bit accesses for on_chip controllers */
  119. /* aligned buf case */
  120. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  121. ioread32_rep(fifoaddr, buf, len / 4);
  122. buf += len & ~0x03;
  123. len &= 0x03;
  124. }
  125. /* unaligned buf case */
  126. for (i = 0; i < len; i++) {
  127. if (!(i & 0x03))
  128. data = ioread32(fifoaddr);
  129. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  130. }
  131. } else {
  132. /* 16-bit accesses for external controllers */
  133. /* aligned buf case */
  134. if (len >= 2 && !((unsigned long)buf & 0x01)) {
  135. ioread16_rep(fifoaddr, buf, len / 2);
  136. buf += len & ~0x01;
  137. len &= 0x01;
  138. }
  139. /* unaligned buf case */
  140. for (i = 0; i < len; i++) {
  141. if (!(i & 0x01))
  142. data = ioread16(fifoaddr);
  143. buf[i] = (data >> ((i & 0x01) * 8)) & 0xff;
  144. }
  145. }
  146. }
  147. static inline void r8a66597_write(struct r8a66597 *r8a66597, u16 val,
  148. unsigned long offset)
  149. {
  150. iowrite16(val, r8a66597->reg + offset);
  151. }
  152. static inline void r8a66597_mdfy(struct r8a66597 *r8a66597,
  153. u16 val, u16 pat, unsigned long offset)
  154. {
  155. u16 tmp;
  156. tmp = r8a66597_read(r8a66597, offset);
  157. tmp = tmp & (~pat);
  158. tmp = tmp | val;
  159. r8a66597_write(r8a66597, tmp, offset);
  160. }
  161. #define r8a66597_bclr(r8a66597, val, offset) \
  162. r8a66597_mdfy(r8a66597, 0, val, offset)
  163. #define r8a66597_bset(r8a66597, val, offset) \
  164. r8a66597_mdfy(r8a66597, val, 0, offset)
  165. static inline void r8a66597_write_fifo(struct r8a66597 *r8a66597,
  166. struct r8a66597_ep *ep,
  167. unsigned char *buf,
  168. int len)
  169. {
  170. void __iomem *fifoaddr = r8a66597->reg + ep->fifoaddr;
  171. int adj = 0;
  172. int i;
  173. if (r8a66597->pdata->on_chip) {
  174. /* 32-bit access only if buf is 32-bit aligned */
  175. if (len >= 4 && !((unsigned long)buf & 0x03)) {
  176. iowrite32_rep(fifoaddr, buf, len / 4);
  177. buf += len & ~0x03;
  178. len &= 0x03;
  179. }
  180. } else {
  181. /* 16-bit access only if buf is 16-bit aligned */
  182. if (len >= 2 && !((unsigned long)buf & 0x01)) {
  183. iowrite16_rep(fifoaddr, buf, len / 2);
  184. buf += len & ~0x01;
  185. len &= 0x01;
  186. }
  187. }
  188. /* adjust fifo address in the little endian case */
  189. if (!(r8a66597_read(r8a66597, CFIFOSEL) & BIGEND)) {
  190. if (r8a66597->pdata->on_chip)
  191. adj = 0x03; /* 32-bit wide */
  192. else
  193. adj = 0x01; /* 16-bit wide */
  194. }
  195. if (r8a66597->pdata->wr0_shorted_to_wr1)
  196. r8a66597_bclr(r8a66597, MBW_16, ep->fifosel);
  197. for (i = 0; i < len; i++)
  198. iowrite8(buf[i], fifoaddr + adj - (i & adj));
  199. if (r8a66597->pdata->wr0_shorted_to_wr1)
  200. r8a66597_bclr(r8a66597, MBW_16, ep->fifosel);
  201. }
  202. static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata)
  203. {
  204. u16 clock = 0;
  205. switch (pdata->xtal) {
  206. case R8A66597_PLATDATA_XTAL_12MHZ:
  207. clock = XTAL12;
  208. break;
  209. case R8A66597_PLATDATA_XTAL_24MHZ:
  210. clock = XTAL24;
  211. break;
  212. case R8A66597_PLATDATA_XTAL_48MHZ:
  213. clock = XTAL48;
  214. break;
  215. default:
  216. printk(KERN_ERR "r8a66597: platdata clock is wrong.\n");
  217. break;
  218. }
  219. return clock;
  220. }
  221. static inline u32 r8a66597_sudmac_read(struct r8a66597 *r8a66597,
  222. unsigned long offset)
  223. {
  224. return ioread32(r8a66597->sudmac_reg + offset);
  225. }
  226. static inline void r8a66597_sudmac_write(struct r8a66597 *r8a66597, u32 val,
  227. unsigned long offset)
  228. {
  229. iowrite32(val, r8a66597->sudmac_reg + offset);
  230. }
  231. #define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
  232. #define get_pipetre_addr(pipenum) (PIPE1TRE + (pipenum - 1) * 4)
  233. #define get_pipetrn_addr(pipenum) (PIPE1TRN + (pipenum - 1) * 4)
  234. #define enable_irq_ready(r8a66597, pipenum) \
  235. enable_pipe_irq(r8a66597, pipenum, BRDYENB)
  236. #define disable_irq_ready(r8a66597, pipenum) \
  237. disable_pipe_irq(r8a66597, pipenum, BRDYENB)
  238. #define enable_irq_empty(r8a66597, pipenum) \
  239. enable_pipe_irq(r8a66597, pipenum, BEMPENB)
  240. #define disable_irq_empty(r8a66597, pipenum) \
  241. disable_pipe_irq(r8a66597, pipenum, BEMPENB)
  242. #define enable_irq_nrdy(r8a66597, pipenum) \
  243. enable_pipe_irq(r8a66597, pipenum, NRDYENB)
  244. #define disable_irq_nrdy(r8a66597, pipenum) \
  245. disable_pipe_irq(r8a66597, pipenum, NRDYENB)
  246. #endif /* __R8A66597_H__ */