i2c-mxv7.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2012 Boundary Devices Inc.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <asm/arch/clock.h>
  24. #include <asm/arch/imx-regs.h>
  25. #include <asm/errno.h>
  26. #include <asm/gpio.h>
  27. #include <asm/imx-common/mxc_i2c.h>
  28. #include <watchdog.h>
  29. static int force_idle_bus(void *priv)
  30. {
  31. int i;
  32. int sda, scl;
  33. ulong elapsed, start_time;
  34. struct i2c_pads_info *p = (struct i2c_pads_info *)priv;
  35. int ret = 0;
  36. gpio_direction_input(p->sda.gp);
  37. gpio_direction_input(p->scl.gp);
  38. imx_iomux_v3_setup_pad(p->sda.gpio_mode);
  39. imx_iomux_v3_setup_pad(p->scl.gpio_mode);
  40. sda = gpio_get_value(p->sda.gp);
  41. scl = gpio_get_value(p->scl.gp);
  42. if ((sda & scl) == 1)
  43. goto exit; /* Bus is idle already */
  44. printf("%s: sda=%d scl=%d sda.gp=0x%x scl.gp=0x%x\n", __func__,
  45. sda, scl, p->sda.gp, p->scl.gp);
  46. /* Send high and low on the SCL line */
  47. for (i = 0; i < 9; i++) {
  48. gpio_direction_output(p->scl.gp, 0);
  49. udelay(50);
  50. gpio_direction_input(p->scl.gp);
  51. udelay(50);
  52. }
  53. start_time = get_timer(0);
  54. for (;;) {
  55. sda = gpio_get_value(p->sda.gp);
  56. scl = gpio_get_value(p->scl.gp);
  57. if ((sda & scl) == 1)
  58. break;
  59. WATCHDOG_RESET();
  60. elapsed = get_timer(start_time);
  61. if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
  62. ret = -EBUSY;
  63. printf("%s: failed to clear bus, sda=%d scl=%d\n",
  64. __func__, sda, scl);
  65. break;
  66. }
  67. }
  68. exit:
  69. imx_iomux_v3_setup_pad(p->sda.i2c_mode);
  70. imx_iomux_v3_setup_pad(p->scl.i2c_mode);
  71. return ret;
  72. }
  73. static void * const i2c_bases[] = {
  74. (void *)I2C1_BASE_ADDR,
  75. (void *)I2C2_BASE_ADDR,
  76. #ifdef I2C3_BASE_ADDR
  77. (void *)I2C3_BASE_ADDR,
  78. #endif
  79. };
  80. /* i2c_index can be from 0 - 2 */
  81. void setup_i2c(unsigned i2c_index, int speed, int slave_addr,
  82. struct i2c_pads_info *p)
  83. {
  84. if (i2c_index >= ARRAY_SIZE(i2c_bases))
  85. return;
  86. /* Enable i2c clock */
  87. enable_i2c_clk(1, i2c_index);
  88. /* Make sure bus is idle */
  89. force_idle_bus(p);
  90. bus_i2c_init(i2c_bases[i2c_index], speed, slave_addr,
  91. force_idle_bus, p);
  92. }