musb_core.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. u8 devctl;
  33. /* disable all interrupts */
  34. writew(0, &musbr->intrtxe);
  35. writew(0, &musbr->intrrxe);
  36. writeb(0, &musbr->intrusbe);
  37. writeb(0, &musbr->testmode);
  38. /* put into basic highspeed mode and start session */
  39. writeb(MUSB_POWER_HSENAB, &musbr->power);
  40. #if defined(CONFIG_MUSB_HCD)
  41. devctl = readb(&musbr->devctl);
  42. writeb(devctl | MUSB_DEVCTL_SESSION, &musbr->devctl);
  43. #endif
  44. }
  45. /*
  46. * This function configures the endpoint configuration. The musb hcd or musb
  47. * device implementation can use this function to configure the endpoints
  48. * and set the FIFO sizes. Note: The summation of FIFO sizes of all endpoints
  49. * should not be more than the available FIFO size.
  50. *
  51. * epinfo - Pointer to EP configuration table
  52. * cnt - Number of entries in the EP conf table.
  53. */
  54. void musb_configure_ep(struct musb_epinfo *epinfo, u8 cnt)
  55. {
  56. u16 csr;
  57. u16 fifoaddr = 64; /* First 64 bytes of FIFO reserved for EP0 */
  58. u32 fifosize;
  59. u8 idx;
  60. while (cnt--) {
  61. /* prepare fifosize to write to register */
  62. fifosize = epinfo->epsize >> 3;
  63. idx = ffs(fifosize) - 1;
  64. writeb(epinfo->epnum, &musbr->index);
  65. if (epinfo->epdir) {
  66. /* Configure fifo size and fifo base address */
  67. writeb(idx, &musbr->txfifosz);
  68. writew(fifoaddr >> 3, &musbr->txfifoadd);
  69. #if defined(CONFIG_MUSB_HCD)
  70. /* clear the data toggle bit */
  71. csr = readw(&musbr->txcsr);
  72. writew(csr | MUSB_TXCSR_CLRDATATOG, &musbr->txcsr);
  73. #endif
  74. /* Flush fifo if required */
  75. if (csr & MUSB_TXCSR_TXPKTRDY)
  76. writew(csr | MUSB_TXCSR_FLUSHFIFO,
  77. &musbr->txcsr);
  78. } else {
  79. /* Configure fifo size and fifo base address */
  80. writeb(idx, &musbr->rxfifosz);
  81. writew(fifoaddr >> 3, &musbr->rxfifoadd);
  82. #if defined(CONFIG_MUSB_HCD)
  83. /* clear the data toggle bit */
  84. csr = readw(&musbr->rxcsr);
  85. writew(csr | MUSB_RXCSR_CLRDATATOG, &musbr->rxcsr);
  86. #endif
  87. /* Flush fifo if required */
  88. if (csr & MUSB_RXCSR_RXPKTRDY)
  89. writew(csr | MUSB_RXCSR_FLUSHFIFO,
  90. &musbr->rxcsr);
  91. }
  92. fifoaddr += epinfo->epsize;
  93. epinfo++;
  94. }
  95. }
  96. /*
  97. * This function writes data to endpoint fifo
  98. *
  99. * ep - endpoint number
  100. * length - number of bytes to write to FIFO
  101. * fifo_data - Pointer to data buffer that contains the data to write
  102. */
  103. void write_fifo(u8 ep, u32 length, void *fifo_data)
  104. {
  105. u8 *data = (u8 *)fifo_data;
  106. /* select the endpoint index */
  107. writeb(ep, &musbr->index);
  108. /* write the data to the fifo */
  109. while (length--)
  110. writeb(*data++, &musbr->fifox[ep]);
  111. }
  112. /*
  113. * This function reads data from endpoint fifo
  114. *
  115. * ep - endpoint number
  116. * length - number of bytes to read from FIFO
  117. * fifo_data - pointer to data buffer into which data is read
  118. */
  119. void read_fifo(u8 ep, u32 length, void *fifo_data)
  120. {
  121. u8 *data = (u8 *)fifo_data;
  122. /* select the endpoint index */
  123. writeb(ep, &musbr->index);
  124. /* read the data to the fifo */
  125. while (length--)
  126. *data++ = readb(&musbr->fifox[ep]);
  127. }