i2c-mxv7.c 2.5 KB

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