i2c-mxv7.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012 Boundary Devices Inc.
  4. */
  5. #include <common.h>
  6. #include <malloc.h>
  7. #include <asm/arch/clock.h>
  8. #include <asm/arch/imx-regs.h>
  9. #include <linux/errno.h>
  10. #include <asm/gpio.h>
  11. #include <asm/mach-imx/mxc_i2c.h>
  12. #include <watchdog.h>
  13. int force_idle_bus(void *priv)
  14. {
  15. int i;
  16. int sda, scl;
  17. ulong elapsed, start_time;
  18. struct i2c_pads_info *p = (struct i2c_pads_info *)priv;
  19. int ret = 0;
  20. gpio_direction_input(p->sda.gp);
  21. gpio_direction_input(p->scl.gp);
  22. imx_iomux_v3_setup_pad(p->sda.gpio_mode);
  23. imx_iomux_v3_setup_pad(p->scl.gpio_mode);
  24. sda = gpio_get_value(p->sda.gp);
  25. scl = gpio_get_value(p->scl.gp);
  26. if ((sda & scl) == 1)
  27. goto exit; /* Bus is idle already */
  28. printf("%s: sda=%d scl=%d sda.gp=0x%x scl.gp=0x%x\n", __func__,
  29. sda, scl, p->sda.gp, p->scl.gp);
  30. /* Send high and low on the SCL line */
  31. for (i = 0; i < 9; i++) {
  32. gpio_direction_output(p->scl.gp, 0);
  33. udelay(50);
  34. gpio_direction_input(p->scl.gp);
  35. udelay(50);
  36. }
  37. start_time = get_timer(0);
  38. for (;;) {
  39. sda = gpio_get_value(p->sda.gp);
  40. scl = gpio_get_value(p->scl.gp);
  41. if ((sda & scl) == 1)
  42. break;
  43. WATCHDOG_RESET();
  44. elapsed = get_timer(start_time);
  45. if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
  46. ret = -EBUSY;
  47. printf("%s: failed to clear bus, sda=%d scl=%d\n",
  48. __func__, sda, scl);
  49. break;
  50. }
  51. }
  52. exit:
  53. imx_iomux_v3_setup_pad(p->sda.i2c_mode);
  54. imx_iomux_v3_setup_pad(p->scl.i2c_mode);
  55. return ret;
  56. }
  57. static void * const i2c_bases[] = {
  58. (void *)I2C1_BASE_ADDR,
  59. (void *)I2C2_BASE_ADDR,
  60. #ifdef I2C3_BASE_ADDR
  61. (void *)I2C3_BASE_ADDR,
  62. #endif
  63. #ifdef I2C4_BASE_ADDR
  64. (void *)I2C4_BASE_ADDR,
  65. #endif
  66. };
  67. /* i2c_index can be from 0 - 3 */
  68. int setup_i2c(unsigned i2c_index, int speed, int slave_addr,
  69. struct i2c_pads_info *p)
  70. {
  71. char name[9];
  72. int ret;
  73. if (i2c_index >= ARRAY_SIZE(i2c_bases))
  74. return -EINVAL;
  75. snprintf(name, sizeof(name), "i2c_sda%01d", i2c_index);
  76. ret = gpio_request(p->sda.gp, name);
  77. if (ret)
  78. return ret;
  79. snprintf(name, sizeof(name), "i2c_scl%01d", i2c_index);
  80. ret = gpio_request(p->scl.gp, name);
  81. if (ret)
  82. goto err_req;
  83. /* Enable i2c clock */
  84. ret = enable_i2c_clk(1, i2c_index);
  85. if (ret)
  86. goto err_clk;
  87. /* Make sure bus is idle */
  88. ret = force_idle_bus(p);
  89. if (ret)
  90. goto err_idle;
  91. #ifndef CONFIG_DM_I2C
  92. bus_i2c_init(i2c_index, speed, slave_addr, force_idle_bus, p);
  93. #endif
  94. return 0;
  95. err_idle:
  96. err_clk:
  97. gpio_free(p->scl.gp);
  98. err_req:
  99. gpio_free(p->sda.gp);
  100. return ret;
  101. }