ulpi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  3. * Copyright 2009 Daniel Mack <daniel@caiaq.de>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/io.h>
  22. #include <linux/delay.h>
  23. #include <linux/usb/otg.h>
  24. #include <linux/usb/ulpi.h>
  25. #include <mach/ulpi.h>
  26. /* ULPIVIEW register bits */
  27. #define ULPIVW_WU (1 << 31) /* Wakeup */
  28. #define ULPIVW_RUN (1 << 30) /* read/write run */
  29. #define ULPIVW_WRITE (1 << 29) /* 0 = read 1 = write */
  30. #define ULPIVW_SS (1 << 27) /* SyncState */
  31. #define ULPIVW_PORT_MASK 0x07 /* Port field */
  32. #define ULPIVW_PORT_SHIFT 24
  33. #define ULPIVW_ADDR_MASK 0xff /* data address field */
  34. #define ULPIVW_ADDR_SHIFT 16
  35. #define ULPIVW_RDATA_MASK 0xff /* read data field */
  36. #define ULPIVW_RDATA_SHIFT 8
  37. #define ULPIVW_WDATA_MASK 0xff /* write data field */
  38. #define ULPIVW_WDATA_SHIFT 0
  39. static int ulpi_poll(void __iomem *view, u32 bit)
  40. {
  41. int timeout = 10000;
  42. while (timeout--) {
  43. u32 data = __raw_readl(view);
  44. if (!(data & bit))
  45. return 0;
  46. cpu_relax();
  47. };
  48. printk(KERN_WARNING "timeout polling for ULPI device\n");
  49. return -ETIMEDOUT;
  50. }
  51. static int ulpi_read(struct otg_transceiver *otg, u32 reg)
  52. {
  53. int ret;
  54. void __iomem *view = otg->io_priv;
  55. /* make sure interface is running */
  56. if (!(__raw_readl(view) & ULPIVW_SS)) {
  57. __raw_writel(ULPIVW_WU, view);
  58. /* wait for wakeup */
  59. ret = ulpi_poll(view, ULPIVW_WU);
  60. if (ret)
  61. return ret;
  62. }
  63. /* read the register */
  64. __raw_writel((ULPIVW_RUN | (reg << ULPIVW_ADDR_SHIFT)), view);
  65. /* wait for completion */
  66. ret = ulpi_poll(view, ULPIVW_RUN);
  67. if (ret)
  68. return ret;
  69. return (__raw_readl(view) >> ULPIVW_RDATA_SHIFT) & ULPIVW_RDATA_MASK;
  70. }
  71. static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
  72. {
  73. int ret;
  74. void __iomem *view = otg->io_priv;
  75. /* make sure the interface is running */
  76. if (!(__raw_readl(view) & ULPIVW_SS)) {
  77. __raw_writel(ULPIVW_WU, view);
  78. /* wait for wakeup */
  79. ret = ulpi_poll(view, ULPIVW_WU);
  80. if (ret)
  81. return ret;
  82. }
  83. __raw_writel((ULPIVW_RUN | ULPIVW_WRITE |
  84. (reg << ULPIVW_ADDR_SHIFT) |
  85. ((val & ULPIVW_WDATA_MASK) << ULPIVW_WDATA_SHIFT)), view);
  86. /* wait for completion */
  87. return ulpi_poll(view, ULPIVW_RUN);
  88. }
  89. struct otg_io_access_ops mxc_ulpi_access_ops = {
  90. .read = ulpi_read,
  91. .write = ulpi_write,
  92. };
  93. EXPORT_SYMBOL_GPL(mxc_ulpi_access_ops);
  94. struct otg_transceiver *imx_otg_ulpi_create(unsigned int flags)
  95. {
  96. return otg_ulpi_create(&mxc_ulpi_access_ops, flags);
  97. }