musb_core.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Mentor USB OTG Core functionality common for both Host and Device
  3. * functionality.
  4. *
  5. * Copyright (c) 2008 Texas Instruments
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  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., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. *
  22. * Author: Thomas Abraham t-abraham@ti.com, Texas Instruments
  23. */
  24. #include <common.h>
  25. #include "musb_core.h"
  26. struct musb_regs *musbr;
  27. /*
  28. * program the mentor core to start (enable interrupts, dma, etc.)
  29. */
  30. void musb_start(void)
  31. {
  32. #if defined(CONFIG_MUSB_HCD)
  33. u8 devctl;
  34. #endif
  35. /* disable all interrupts */
  36. writew(0, &musbr->intrtxe);
  37. writew(0, &musbr->intrrxe);
  38. writeb(0, &musbr->intrusbe);
  39. writeb(0, &musbr->testmode);
  40. /* put into basic highspeed mode and start session */
  41. writeb(MUSB_POWER_HSENAB, &musbr->power);
  42. #if defined(CONFIG_MUSB_HCD)
  43. devctl = readb(&musbr->devctl);
  44. writeb(devctl | MUSB_DEVCTL_SESSION, &musbr->devctl);
  45. #endif
  46. }
  47. /*
  48. * This function configures the endpoint configuration. The musb hcd or musb
  49. * device implementation can use this function to configure the endpoints
  50. * and set the FIFO sizes. Note: The summation of FIFO sizes of all endpoints
  51. * should not be more than the available FIFO size.
  52. *
  53. * epinfo - Pointer to EP configuration table
  54. * cnt - Number of entries in the EP conf table.
  55. */
  56. void musb_configure_ep(struct musb_epinfo *epinfo, u8 cnt)
  57. {
  58. u16 csr;
  59. u16 fifoaddr = 64; /* First 64 bytes of FIFO reserved for EP0 */
  60. u32 fifosize;
  61. u8 idx;
  62. while (cnt--) {
  63. /* prepare fifosize to write to register */
  64. fifosize = epinfo->epsize >> 3;
  65. idx = ffs(fifosize) - 1;
  66. writeb(epinfo->epnum, &musbr->index);
  67. if (epinfo->epdir) {
  68. /* Configure fifo size and fifo base address */
  69. writeb(idx, &musbr->txfifosz);
  70. writew(fifoaddr >> 3, &musbr->txfifoadd);
  71. csr = readw(&musbr->txcsr);
  72. #if defined(CONFIG_MUSB_HCD)
  73. /* clear the data toggle bit */
  74. writew(csr | MUSB_TXCSR_CLRDATATOG, &musbr->txcsr);
  75. #endif
  76. /* Flush fifo if required */
  77. if (csr & MUSB_TXCSR_TXPKTRDY)
  78. writew(csr | MUSB_TXCSR_FLUSHFIFO,
  79. &musbr->txcsr);
  80. } else {
  81. /* Configure fifo size and fifo base address */
  82. writeb(idx, &musbr->rxfifosz);
  83. writew(fifoaddr >> 3, &musbr->rxfifoadd);
  84. csr = readw(&musbr->rxcsr);
  85. #if defined(CONFIG_MUSB_HCD)
  86. /* clear the data toggle bit */
  87. writew(csr | MUSB_RXCSR_CLRDATATOG, &musbr->rxcsr);
  88. #endif
  89. /* Flush fifo if required */
  90. if (csr & MUSB_RXCSR_RXPKTRDY)
  91. writew(csr | MUSB_RXCSR_FLUSHFIFO,
  92. &musbr->rxcsr);
  93. }
  94. fifoaddr += epinfo->epsize;
  95. epinfo++;
  96. }
  97. }
  98. /*
  99. * This function writes data to endpoint fifo
  100. *
  101. * ep - endpoint number
  102. * length - number of bytes to write to FIFO
  103. * fifo_data - Pointer to data buffer that contains the data to write
  104. */
  105. void write_fifo(u8 ep, u32 length, void *fifo_data)
  106. {
  107. u8 *data = (u8 *)fifo_data;
  108. /* select the endpoint index */
  109. writeb(ep, &musbr->index);
  110. /* write the data to the fifo */
  111. while (length--)
  112. writeb(*data++, &musbr->fifox[ep]);
  113. }
  114. /*
  115. * This function reads data from endpoint fifo
  116. *
  117. * ep - endpoint number
  118. * length - number of bytes to read from FIFO
  119. * fifo_data - pointer to data buffer into which data is read
  120. */
  121. void read_fifo(u8 ep, u32 length, void *fifo_data)
  122. {
  123. u8 *data = (u8 *)fifo_data;
  124. /* select the endpoint index */
  125. writeb(ep, &musbr->index);
  126. /* read the data to the fifo */
  127. while (length--)
  128. *data++ = readb(&musbr->fifox[ep]);
  129. }