fifo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Renesas USB driver
  3. *
  4. * Copyright (C) 2011 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. *
  16. */
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include "./common.h"
  20. #include "./pipe.h"
  21. /*
  22. * packet info function
  23. */
  24. void usbhs_pkt_update(struct usbhs_pkt *pkt,
  25. struct usbhs_pipe *pipe,
  26. void *buf, int len)
  27. {
  28. pkt->pipe = pipe;
  29. pkt->buf = buf;
  30. pkt->length = len;
  31. pkt->actual = 0;
  32. pkt->maxp = 0;
  33. }
  34. /*
  35. * FIFO ctrl
  36. */
  37. static void usbhsf_send_terminator(struct usbhs_pipe *pipe)
  38. {
  39. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  40. usbhs_bset(priv, CFIFOCTR, BVAL, BVAL);
  41. }
  42. static int usbhsf_fifo_barrier(struct usbhs_priv *priv)
  43. {
  44. int timeout = 1024;
  45. do {
  46. /* The FIFO port is accessible */
  47. if (usbhs_read(priv, CFIFOCTR) & FRDY)
  48. return 0;
  49. udelay(10);
  50. } while (timeout--);
  51. return -EBUSY;
  52. }
  53. static void usbhsf_fifo_clear(struct usbhs_pipe *pipe)
  54. {
  55. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  56. if (!usbhs_pipe_is_dcp(pipe))
  57. usbhsf_fifo_barrier(priv);
  58. usbhs_write(priv, CFIFOCTR, BCLR);
  59. }
  60. static int usbhsf_fifo_rcv_len(struct usbhs_priv *priv)
  61. {
  62. return usbhs_read(priv, CFIFOCTR) & DTLN_MASK;
  63. }
  64. static int usbhsf_fifo_select(struct usbhs_pipe *pipe, int write)
  65. {
  66. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  67. struct device *dev = usbhs_priv_to_dev(priv);
  68. int timeout = 1024;
  69. u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */
  70. u16 base = usbhs_pipe_number(pipe); /* CURPIPE */
  71. if (usbhs_pipe_is_dcp(pipe))
  72. base |= (1 == write) << 5; /* ISEL */
  73. /* "base" will be used below */
  74. usbhs_write(priv, CFIFOSEL, base | MBW_32);
  75. /* check ISEL and CURPIPE value */
  76. while (timeout--) {
  77. if (base == (mask & usbhs_read(priv, CFIFOSEL)))
  78. return 0;
  79. udelay(10);
  80. }
  81. dev_err(dev, "fifo select error\n");
  82. return -EIO;
  83. }
  84. /*
  85. * PIO fifo functions
  86. */
  87. int usbhs_fifo_prepare_write(struct usbhs_pipe *pipe)
  88. {
  89. return usbhsf_fifo_select(pipe, 1);
  90. }
  91. int usbhs_fifo_write(struct usbhs_pkt *pkt)
  92. {
  93. struct usbhs_pipe *pipe = pkt->pipe;
  94. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  95. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  96. void __iomem *addr = priv->base + CFIFO;
  97. int maxp = usbhs_pipe_get_maxpacket(pipe);
  98. int total_len;
  99. u8 *buf = pkt->buf;
  100. int i, ret, len;
  101. ret = usbhs_pipe_is_accessible(pipe);
  102. if (ret < 0)
  103. return ret;
  104. ret = usbhsf_fifo_select(pipe, 1);
  105. if (ret < 0)
  106. return ret;
  107. ret = usbhsf_fifo_barrier(priv);
  108. if (ret < 0)
  109. return ret;
  110. len = min(pkt->length, maxp);
  111. total_len = len;
  112. /*
  113. * FIXME
  114. *
  115. * 32-bit access only
  116. */
  117. if (len >= 4 &&
  118. !((unsigned long)buf & 0x03)) {
  119. iowrite32_rep(addr, buf, len / 4);
  120. len %= 4;
  121. buf += total_len - len;
  122. }
  123. /* the rest operation */
  124. for (i = 0; i < len; i++)
  125. iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
  126. if (total_len < maxp)
  127. usbhsf_send_terminator(pipe);
  128. usbhs_pipe_enable(pipe);
  129. /* update pkt */
  130. if (info->tx_done) {
  131. pkt->actual = total_len;
  132. pkt->maxp = maxp;
  133. info->tx_done(pkt);
  134. }
  135. return 0;
  136. }
  137. int usbhs_fifo_prepare_read(struct usbhs_pipe *pipe)
  138. {
  139. int ret;
  140. /*
  141. * select pipe and enable it to prepare packet receive
  142. */
  143. ret = usbhsf_fifo_select(pipe, 0);
  144. if (ret < 0)
  145. return ret;
  146. usbhs_pipe_enable(pipe);
  147. return ret;
  148. }
  149. int usbhs_fifo_read(struct usbhs_pkt *pkt)
  150. {
  151. struct usbhs_pipe *pipe = pkt->pipe;
  152. struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
  153. struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
  154. void __iomem *addr = priv->base + CFIFO;
  155. u8 *buf = pkt->buf;
  156. int rcv_len, len;
  157. int i, ret;
  158. int total_len = 0;
  159. u32 data = 0;
  160. ret = usbhsf_fifo_select(pipe, 0);
  161. if (ret < 0)
  162. return ret;
  163. ret = usbhsf_fifo_barrier(priv);
  164. if (ret < 0)
  165. return ret;
  166. rcv_len = usbhsf_fifo_rcv_len(priv);
  167. /*
  168. * Buffer clear if Zero-Length packet
  169. *
  170. * see
  171. * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
  172. */
  173. if (0 == rcv_len) {
  174. usbhsf_fifo_clear(pipe);
  175. goto usbhs_fifo_read_end;
  176. }
  177. len = min(rcv_len, pkt->length);
  178. total_len = len;
  179. /*
  180. * FIXME
  181. *
  182. * 32-bit access only
  183. */
  184. if (len >= 4 &&
  185. !((unsigned long)buf & 0x03)) {
  186. ioread32_rep(addr, buf, len / 4);
  187. len %= 4;
  188. buf += rcv_len - len;
  189. }
  190. /* the rest operation */
  191. for (i = 0; i < len; i++) {
  192. if (!(i & 0x03))
  193. data = ioread32(addr);
  194. buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
  195. }
  196. usbhs_fifo_read_end:
  197. if (info->rx_done) {
  198. /* update pkt */
  199. pkt->actual = total_len;
  200. pkt->maxp = usbhs_pipe_get_maxpacket(pipe);
  201. info->rx_done(pkt);
  202. }
  203. return 0;
  204. }