ulpi_viewport.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/kernel.h>
  15. #include <linux/usb.h>
  16. #include <linux/io.h>
  17. #include <linux/usb/otg.h>
  18. #include <linux/usb/ulpi.h>
  19. #define ULPI_VIEW_WAKEUP (1 << 31)
  20. #define ULPI_VIEW_RUN (1 << 30)
  21. #define ULPI_VIEW_WRITE (1 << 29)
  22. #define ULPI_VIEW_READ (0 << 29)
  23. #define ULPI_VIEW_ADDR(x) (((x) & 0xff) << 16)
  24. #define ULPI_VIEW_DATA_READ(x) (((x) >> 8) & 0xff)
  25. #define ULPI_VIEW_DATA_WRITE(x) ((x) & 0xff)
  26. static int ulpi_viewport_wait(void __iomem *view, u32 mask)
  27. {
  28. unsigned long usec = 2000;
  29. while (usec--) {
  30. if (!(readl(view) & mask))
  31. return 0;
  32. udelay(1);
  33. };
  34. return -ETIMEDOUT;
  35. }
  36. static int ulpi_viewport_read(struct otg_transceiver *otg, u32 reg)
  37. {
  38. int ret;
  39. void __iomem *view = otg->io_priv;
  40. writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view);
  41. ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP);
  42. if (ret)
  43. return ret;
  44. writel(ULPI_VIEW_RUN | ULPI_VIEW_READ | ULPI_VIEW_ADDR(reg), view);
  45. ret = ulpi_viewport_wait(view, ULPI_VIEW_RUN);
  46. if (ret)
  47. return ret;
  48. return ULPI_VIEW_DATA_READ(readl(view));
  49. }
  50. static int ulpi_viewport_write(struct otg_transceiver *otg, u32 val, u32 reg)
  51. {
  52. int ret;
  53. void __iomem *view = otg->io_priv;
  54. writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view);
  55. ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP);
  56. if (ret)
  57. return ret;
  58. writel(ULPI_VIEW_RUN | ULPI_VIEW_WRITE | ULPI_VIEW_DATA_WRITE(val) |
  59. ULPI_VIEW_ADDR(reg), view);
  60. return ulpi_viewport_wait(view, ULPI_VIEW_RUN);
  61. }
  62. struct otg_io_access_ops ulpi_viewport_access_ops = {
  63. .read = ulpi_viewport_read,
  64. .write = ulpi_viewport_write,
  65. };