musb_core.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Mentor USB OTG Core functionality common for both Host and Device
  4. * functionality.
  5. *
  6. * Copyright (c) 2008 Texas Instruments
  7. *
  8. * Author: Thomas Abraham t-abraham@ti.com, Texas Instruments
  9. */
  10. #include <common.h>
  11. #include "musb_core.h"
  12. struct musb_regs *musbr;
  13. /*
  14. * program the mentor core to start (enable interrupts, dma, etc.)
  15. */
  16. void musb_start(void)
  17. {
  18. #if defined(CONFIG_USB_MUSB_HCD)
  19. u8 devctl;
  20. u8 busctl;
  21. #endif
  22. /* disable all interrupts */
  23. writew(0, &musbr->intrtxe);
  24. writew(0, &musbr->intrrxe);
  25. writeb(0, &musbr->intrusbe);
  26. writeb(0, &musbr->testmode);
  27. /* put into basic highspeed mode and start session */
  28. writeb(MUSB_POWER_HSENAB, &musbr->power);
  29. #if defined(CONFIG_USB_MUSB_HCD)
  30. /* Program PHY to use EXT VBUS if required */
  31. if (musb_cfg.extvbus == 1) {
  32. busctl = musb_read_ulpi_buscontrol(musbr);
  33. musb_write_ulpi_buscontrol(musbr, busctl | ULPI_USE_EXTVBUS);
  34. }
  35. devctl = readb(&musbr->devctl);
  36. writeb(devctl | MUSB_DEVCTL_SESSION, &musbr->devctl);
  37. #endif
  38. }
  39. #ifdef MUSB_NO_DYNAMIC_FIFO
  40. # define config_fifo(dir, idx, addr)
  41. #else
  42. # define config_fifo(dir, idx, addr) \
  43. do { \
  44. writeb(idx, &musbr->dir##fifosz); \
  45. writew(fifoaddr >> 3, &musbr->dir##fifoadd); \
  46. } while (0)
  47. #endif
  48. /*
  49. * This function configures the endpoint configuration. The musb hcd or musb
  50. * device implementation can use this function to configure the endpoints
  51. * and set the FIFO sizes. Note: The summation of FIFO sizes of all endpoints
  52. * should not be more than the available FIFO size.
  53. *
  54. * epinfo - Pointer to EP configuration table
  55. * cnt - Number of entries in the EP conf table.
  56. */
  57. void musb_configure_ep(const struct musb_epinfo *epinfo, u8 cnt)
  58. {
  59. u16 csr;
  60. u16 fifoaddr = 64; /* First 64 bytes of FIFO reserved for EP0 */
  61. u32 fifosize;
  62. u8 idx;
  63. while (cnt--) {
  64. /* prepare fifosize to write to register */
  65. fifosize = epinfo->epsize >> 3;
  66. idx = ffs(fifosize) - 1;
  67. writeb(epinfo->epnum, &musbr->index);
  68. if (epinfo->epdir) {
  69. /* Configure fifo size and fifo base address */
  70. config_fifo(tx, idx, fifoaddr);
  71. csr = readw(&musbr->txcsr);
  72. #if defined(CONFIG_USB_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. config_fifo(rx, idx, fifoaddr);
  83. csr = readw(&musbr->rxcsr);
  84. #if defined(CONFIG_USB_MUSB_HCD)
  85. /* clear the data toggle bit */
  86. writew(csr | MUSB_RXCSR_CLRDATATOG, &musbr->rxcsr);
  87. #endif
  88. /* Flush fifo if required */
  89. if (csr & MUSB_RXCSR_RXPKTRDY)
  90. writew(csr | MUSB_RXCSR_FLUSHFIFO,
  91. &musbr->rxcsr);
  92. }
  93. fifoaddr += epinfo->epsize;
  94. epinfo++;
  95. }
  96. }
  97. /*
  98. * This function writes data to endpoint fifo
  99. *
  100. * ep - endpoint number
  101. * length - number of bytes to write to FIFO
  102. * fifo_data - Pointer to data buffer that contains the data to write
  103. */
  104. __attribute__((weak))
  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. * AM35x supports only 32bit read operations so
  116. * use seperate read_fifo() function for it.
  117. */
  118. #ifndef CONFIG_USB_AM35X
  119. /*
  120. * This function reads data from endpoint fifo
  121. *
  122. * ep - endpoint number
  123. * length - number of bytes to read from FIFO
  124. * fifo_data - pointer to data buffer into which data is read
  125. */
  126. __attribute__((weak))
  127. void read_fifo(u8 ep, u32 length, void *fifo_data)
  128. {
  129. u8 *data = (u8 *)fifo_data;
  130. /* select the endpoint index */
  131. writeb(ep, &musbr->index);
  132. /* read the data to the fifo */
  133. while (length--)
  134. *data++ = readb(&musbr->fifox[ep]);
  135. }
  136. #endif /* CONFIG_USB_AM35X */