ulpi-viewport.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <linux/delay.h>
  18. #include <usb/ulpi.h>
  19. /* ULPI viewport control bits */
  20. #define ULPI_SS (1 << 27)
  21. #define ULPI_RWCTRL (1 << 29)
  22. #define ULPI_RWRUN (1 << 30)
  23. #define ULPI_WU (1 << 31)
  24. /*
  25. * Wait for the ULPI request to complete
  26. *
  27. * @ulpi_viewport - the address of the viewport
  28. * @mask - expected value to wait for
  29. *
  30. * returns 0 on mask match, ULPI_ERROR on time out.
  31. */
  32. static int ulpi_wait(struct ulpi_viewport *ulpi_vp, u32 mask)
  33. {
  34. int timeout = CONFIG_USB_ULPI_TIMEOUT;
  35. /* Wait for the bits in mask to become zero. */
  36. while (--timeout) {
  37. if ((readl(ulpi_vp->viewport_addr) & mask) == 0)
  38. return 0;
  39. udelay(1);
  40. }
  41. return ULPI_ERROR;
  42. }
  43. /*
  44. * Wake the ULPI PHY up for communication
  45. *
  46. * returns 0 on success.
  47. */
  48. static int ulpi_wakeup(struct ulpi_viewport *ulpi_vp)
  49. {
  50. int err;
  51. if (readl(ulpi_vp->viewport_addr) & ULPI_SS)
  52. return 0; /* already awake */
  53. writel(ULPI_WU, ulpi_vp->viewport_addr);
  54. err = ulpi_wait(ulpi_vp, ULPI_WU);
  55. if (err)
  56. printf("ULPI wakeup timed out\n");
  57. return err;
  58. }
  59. /*
  60. * Issue a ULPI read/write request
  61. *
  62. * @value - the ULPI request
  63. */
  64. static int ulpi_request(struct ulpi_viewport *ulpi_vp, u32 value)
  65. {
  66. int err;
  67. err = ulpi_wakeup(ulpi_vp);
  68. if (err)
  69. return err;
  70. writel(value, ulpi_vp->viewport_addr);
  71. err = ulpi_wait(ulpi_vp, ULPI_RWRUN);
  72. if (err)
  73. printf("ULPI request timed out\n");
  74. return err;
  75. }
  76. int ulpi_write(struct ulpi_viewport *ulpi_vp, u8 *reg, u32 value)
  77. {
  78. u32 addr = (uintptr_t)reg & 0xFF;
  79. u32 val = ULPI_RWRUN | ULPI_RWCTRL | addr << 16 | (value & 0xff);
  80. val |= (ulpi_vp->port_num & 0x7) << 24;
  81. return ulpi_request(ulpi_vp, val);
  82. }
  83. u32 ulpi_read(struct ulpi_viewport *ulpi_vp, u8 *reg)
  84. {
  85. int err;
  86. u32 val = ULPI_RWRUN | ((uintptr_t)reg & 0xFF) << 16;
  87. val |= (ulpi_vp->port_num & 0x7) << 24;
  88. err = ulpi_request(ulpi_vp, val);
  89. if (err)
  90. return err;
  91. return (readl(ulpi_vp->viewport_addr) >> 8) & 0xff;
  92. }