ulpi-viewport.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 Jana Rapava <fermata7@gmail.com>
  4. * Copyright (C) 2011 CompuLab, Ltd. <www.compulab.co.il>
  5. *
  6. * Authors: Jana Rapava <fermata7@gmail.com>
  7. * Igor Grinberg <grinberg@compulab.co.il>
  8. *
  9. * Based on:
  10. * linux/drivers/usb/otg/ulpi_viewport.c
  11. *
  12. * Original Copyright follow:
  13. * Copyright (C) 2011 Google, Inc.
  14. */
  15. #include <common.h>
  16. #include <asm/io.h>
  17. #include <usb/ulpi.h>
  18. /* ULPI viewport control bits */
  19. #define ULPI_SS (1 << 27)
  20. #define ULPI_RWCTRL (1 << 29)
  21. #define ULPI_RWRUN (1 << 30)
  22. #define ULPI_WU (1 << 31)
  23. /*
  24. * Wait for the ULPI request to complete
  25. *
  26. * @ulpi_viewport - the address of the viewport
  27. * @mask - expected value to wait for
  28. *
  29. * returns 0 on mask match, ULPI_ERROR on time out.
  30. */
  31. static int ulpi_wait(struct ulpi_viewport *ulpi_vp, u32 mask)
  32. {
  33. int timeout = CONFIG_USB_ULPI_TIMEOUT;
  34. /* Wait for the bits in mask to become zero. */
  35. while (--timeout) {
  36. if ((readl(ulpi_vp->viewport_addr) & mask) == 0)
  37. return 0;
  38. udelay(1);
  39. }
  40. return ULPI_ERROR;
  41. }
  42. /*
  43. * Wake the ULPI PHY up for communication
  44. *
  45. * returns 0 on success.
  46. */
  47. static int ulpi_wakeup(struct ulpi_viewport *ulpi_vp)
  48. {
  49. int err;
  50. if (readl(ulpi_vp->viewport_addr) & ULPI_SS)
  51. return 0; /* already awake */
  52. writel(ULPI_WU, ulpi_vp->viewport_addr);
  53. err = ulpi_wait(ulpi_vp, ULPI_WU);
  54. if (err)
  55. printf("ULPI wakeup timed out\n");
  56. return err;
  57. }
  58. /*
  59. * Issue a ULPI read/write request
  60. *
  61. * @value - the ULPI request
  62. */
  63. static int ulpi_request(struct ulpi_viewport *ulpi_vp, u32 value)
  64. {
  65. int err;
  66. err = ulpi_wakeup(ulpi_vp);
  67. if (err)
  68. return err;
  69. writel(value, ulpi_vp->viewport_addr);
  70. err = ulpi_wait(ulpi_vp, ULPI_RWRUN);
  71. if (err)
  72. printf("ULPI request timed out\n");
  73. return err;
  74. }
  75. int ulpi_write(struct ulpi_viewport *ulpi_vp, u8 *reg, u32 value)
  76. {
  77. u32 addr = (uintptr_t)reg & 0xFF;
  78. u32 val = ULPI_RWRUN | ULPI_RWCTRL | addr << 16 | (value & 0xff);
  79. val |= (ulpi_vp->port_num & 0x7) << 24;
  80. return ulpi_request(ulpi_vp, val);
  81. }
  82. u32 ulpi_read(struct ulpi_viewport *ulpi_vp, u8 *reg)
  83. {
  84. int err;
  85. u32 val = ULPI_RWRUN | ((uintptr_t)reg & 0xFF) << 16;
  86. val |= (ulpi_vp->port_num & 0x7) << 24;
  87. err = ulpi_request(ulpi_vp, val);
  88. if (err)
  89. return err;
  90. return (readl(ulpi_vp->viewport_addr) >> 8) & 0xff;
  91. }