ulpi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <mach/ulpi.h>
  25. /* ULPIVIEW register bits */
  26. #define ULPIVW_WU (1 << 31) /* Wakeup */
  27. #define ULPIVW_RUN (1 << 30) /* read/write run */
  28. #define ULPIVW_WRITE (1 << 29) /* 0 = read 1 = write */
  29. #define ULPIVW_SS (1 << 27) /* SyncState */
  30. #define ULPIVW_PORT_MASK 0x07 /* Port field */
  31. #define ULPIVW_PORT_SHIFT 24
  32. #define ULPIVW_ADDR_MASK 0xff /* data address field */
  33. #define ULPIVW_ADDR_SHIFT 16
  34. #define ULPIVW_RDATA_MASK 0xff /* read data field */
  35. #define ULPIVW_RDATA_SHIFT 8
  36. #define ULPIVW_WDATA_MASK 0xff /* write data field */
  37. #define ULPIVW_WDATA_SHIFT 0
  38. static int ulpi_poll(void __iomem *view, u32 bit)
  39. {
  40. int timeout = 10000;
  41. while (timeout--) {
  42. u32 data = __raw_readl(view);
  43. if (!(data & bit))
  44. return 0;
  45. cpu_relax();
  46. };
  47. printk(KERN_WARNING "timeout polling for ULPI device\n");
  48. return -ETIMEDOUT;
  49. }
  50. static int ulpi_read(struct otg_transceiver *otg, u32 reg)
  51. {
  52. int ret;
  53. void __iomem *view = otg->io_priv;
  54. /* make sure interface is running */
  55. if (!(__raw_readl(view) & ULPIVW_SS)) {
  56. __raw_writel(ULPIVW_WU, view);
  57. /* wait for wakeup */
  58. ret = ulpi_poll(view, ULPIVW_WU);
  59. if (ret)
  60. return ret;
  61. }
  62. /* read the register */
  63. __raw_writel((ULPIVW_RUN | (reg << ULPIVW_ADDR_SHIFT)), view);
  64. /* wait for completion */
  65. ret = ulpi_poll(view, ULPIVW_RUN);
  66. if (ret)
  67. return ret;
  68. return (__raw_readl(view) >> ULPIVW_RDATA_SHIFT) & ULPIVW_RDATA_MASK;
  69. }
  70. static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
  71. {
  72. int ret;
  73. void __iomem *view = otg->io_priv;
  74. /* make sure the interface is running */
  75. if (!(__raw_readl(view) & ULPIVW_SS)) {
  76. __raw_writel(ULPIVW_WU, view);
  77. /* wait for wakeup */
  78. ret = ulpi_poll(view, ULPIVW_WU);
  79. if (ret)
  80. return ret;
  81. }
  82. __raw_writel((ULPIVW_RUN | ULPIVW_WRITE |
  83. (reg << ULPIVW_ADDR_SHIFT) |
  84. ((val & ULPIVW_WDATA_MASK) << ULPIVW_WDATA_SHIFT)), view);
  85. /* wait for completion */
  86. return ulpi_poll(view, ULPIVW_RUN);
  87. }
  88. struct otg_io_access_ops mxc_ulpi_access_ops = {
  89. .read = ulpi_read,
  90. .write = ulpi_write,
  91. };
  92. EXPORT_SYMBOL_GPL(mxc_ulpi_access_ops);