r8a66597-udc.h 7.2 KB

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