r8a66597-udc.h 6.5 KB

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