r8a66597-udc.h 6.8 KB

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