phy-ulpi-viewport.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C) 2011 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/usb.h>
  17. #include <linux/io.h>
  18. #include <linux/usb/otg.h>
  19. #include <linux/usb/ulpi.h>
  20. #define ULPI_VIEW_WAKEUP (1 << 31)
  21. #define ULPI_VIEW_RUN (1 << 30)
  22. #define ULPI_VIEW_WRITE (1 << 29)
  23. #define ULPI_VIEW_READ (0 << 29)
  24. #define ULPI_VIEW_ADDR(x) (((x) & 0xff) << 16)
  25. #define ULPI_VIEW_DATA_READ(x) (((x) >> 8) & 0xff)
  26. #define ULPI_VIEW_DATA_WRITE(x) ((x) & 0xff)
  27. static int ulpi_viewport_wait(void __iomem *view, u32 mask)
  28. {
  29. unsigned long usec = 2000;
  30. while (usec--) {
  31. if (!(readl(view) & mask))
  32. return 0;
  33. udelay(1);
  34. }
  35. return -ETIMEDOUT;
  36. }
  37. static int ulpi_viewport_read(struct usb_phy *otg, u32 reg)
  38. {
  39. int ret;
  40. void __iomem *view = otg->io_priv;
  41. writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view);
  42. ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP);
  43. if (ret)
  44. return ret;
  45. writel(ULPI_VIEW_RUN | ULPI_VIEW_READ | ULPI_VIEW_ADDR(reg), view);
  46. ret = ulpi_viewport_wait(view, ULPI_VIEW_RUN);
  47. if (ret)
  48. return ret;
  49. return ULPI_VIEW_DATA_READ(readl(view));
  50. }
  51. static int ulpi_viewport_write(struct usb_phy *otg, u32 val, u32 reg)
  52. {
  53. int ret;
  54. void __iomem *view = otg->io_priv;
  55. writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view);
  56. ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP);
  57. if (ret)
  58. return ret;
  59. writel(ULPI_VIEW_RUN | ULPI_VIEW_WRITE | ULPI_VIEW_DATA_WRITE(val) |
  60. ULPI_VIEW_ADDR(reg), view);
  61. return ulpi_viewport_wait(view, ULPI_VIEW_RUN);
  62. }
  63. struct usb_phy_io_ops ulpi_viewport_access_ops = {
  64. .read = ulpi_viewport_read,
  65. .write = ulpi_viewport_write,
  66. };
  67. EXPORT_SYMBOL_GPL(ulpi_viewport_access_ops);