ulpi-viewport.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2011 Jana Rapava <fermata7@gmail.com>
  3. * Copyright (C) 2011 CompuLab, Ltd. <www.compulab.co.il>
  4. *
  5. * Authors: Jana Rapava <fermata7@gmail.com>
  6. * Igor Grinberg <grinberg@compulab.co.il>
  7. *
  8. * Based on:
  9. * linux/drivers/usb/otg/ulpi_viewport.c
  10. *
  11. * Original Copyright follow:
  12. * Copyright (C) 2011 Google, Inc.
  13. *
  14. * This software is licensed under the terms of the GNU General Public
  15. * License version 2, as published by the Free Software Foundation, and
  16. * may be copied, distributed, and modified under those terms.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. */
  24. #include <common.h>
  25. #include <asm/io.h>
  26. #include <usb/ulpi.h>
  27. /* ULPI viewport control bits */
  28. #define ULPI_SS (1 << 27)
  29. #define ULPI_RWCTRL (1 << 29)
  30. #define ULPI_RWRUN (1 << 30)
  31. #define ULPI_WU (1 << 31)
  32. /*
  33. * Wait for the ULPI request to complete
  34. *
  35. * @ulpi_viewport - the address of the viewport
  36. * @mask - expected value to wait for
  37. *
  38. * returns 0 on mask match, ULPI_ERROR on time out.
  39. */
  40. static int ulpi_wait(struct ulpi_viewport *ulpi_vp, u32 mask)
  41. {
  42. int timeout = CONFIG_USB_ULPI_TIMEOUT;
  43. /* Wait for the bits in mask to become zero. */
  44. while (--timeout) {
  45. if ((readl(ulpi_vp->viewport_addr) & mask) == 0)
  46. return 0;
  47. udelay(1);
  48. }
  49. return ULPI_ERROR;
  50. }
  51. /*
  52. * Wake the ULPI PHY up for communication
  53. *
  54. * returns 0 on success.
  55. */
  56. static int ulpi_wakeup(struct ulpi_viewport *ulpi_vp)
  57. {
  58. int err;
  59. if (readl(ulpi_vp->viewport_addr) & ULPI_SS)
  60. return 0; /* already awake */
  61. writel(ULPI_WU, ulpi_vp->viewport_addr);
  62. err = ulpi_wait(ulpi_vp, ULPI_WU);
  63. if (err)
  64. printf("ULPI wakeup timed out\n");
  65. return err;
  66. }
  67. /*
  68. * Issue a ULPI read/write request
  69. *
  70. * @value - the ULPI request
  71. */
  72. static int ulpi_request(struct ulpi_viewport *ulpi_vp, u32 value)
  73. {
  74. int err;
  75. err = ulpi_wakeup(ulpi_vp);
  76. if (err)
  77. return err;
  78. writel(value, ulpi_vp->viewport_addr);
  79. err = ulpi_wait(ulpi_vp, ULPI_RWRUN);
  80. if (err)
  81. printf("ULPI request timed out\n");
  82. return err;
  83. }
  84. int ulpi_write(struct ulpi_viewport *ulpi_vp, u8 *reg, u32 value)
  85. {
  86. u32 val = ULPI_RWRUN | ULPI_RWCTRL | ((u32)reg << 16) | (value & 0xff);
  87. val |= (ulpi_vp->port_num & 0x7) << 24;
  88. return ulpi_request(ulpi_vp, val);
  89. }
  90. u32 ulpi_read(struct ulpi_viewport *ulpi_vp, u8 *reg)
  91. {
  92. int err;
  93. u32 val = ULPI_RWRUN | ((u32)reg << 16);
  94. val |= (ulpi_vp->port_num & 0x7) << 24;
  95. err = ulpi_request(ulpi_vp, val);
  96. if (err)
  97. return err;
  98. return (readl(ulpi_vp->viewport_addr) >> 8) & 0xff;
  99. }