km83xx_i2c.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2011
  4. * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
  5. */
  6. #include <common.h>
  7. #include <i2c.h>
  8. #include <asm/io.h>
  9. #include <linux/ctype.h>
  10. #include "../common/common.h"
  11. static void i2c_write_start_seq(void)
  12. {
  13. struct fsl_i2c_base *base;
  14. base = (struct fsl_i2c_base *)(CONFIG_SYS_IMMR +
  15. CONFIG_SYS_I2C_OFFSET);
  16. udelay(DELAY_ABORT_SEQ);
  17. out_8(&base->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  18. udelay(DELAY_ABORT_SEQ);
  19. out_8(&base->cr, (I2C_CR_MEN));
  20. }
  21. int i2c_make_abort(void)
  22. {
  23. struct fsl_i2c_base *base;
  24. base = (struct fsl_i2c_base *)(CONFIG_SYS_IMMR +
  25. CONFIG_SYS_I2C_OFFSET);
  26. uchar last;
  27. int nbr_read = 0;
  28. int i = 0;
  29. int ret = 0;
  30. /* wait after each operation to finsh with a delay */
  31. out_8(&base->cr, (I2C_CR_MSTA));
  32. udelay(DELAY_ABORT_SEQ);
  33. out_8(&base->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  34. udelay(DELAY_ABORT_SEQ);
  35. in_8(&base->dr);
  36. udelay(DELAY_ABORT_SEQ);
  37. last = in_8(&base->dr);
  38. nbr_read++;
  39. /*
  40. * do read until the last bit is 1, but stop if the full eeprom is
  41. * read.
  42. */
  43. while (((last & 0x01) != 0x01) &&
  44. (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
  45. udelay(DELAY_ABORT_SEQ);
  46. last = in_8(&base->dr);
  47. nbr_read++;
  48. }
  49. if ((last & 0x01) != 0x01)
  50. ret = -2;
  51. if ((last != 0xff) || (nbr_read > 1))
  52. printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
  53. nbr_read, last);
  54. udelay(DELAY_ABORT_SEQ);
  55. out_8(&base->cr, (I2C_CR_MEN));
  56. udelay(DELAY_ABORT_SEQ);
  57. /* clear status reg */
  58. out_8(&base->sr, 0);
  59. for (i = 0; i < 5; i++)
  60. i2c_write_start_seq();
  61. if (ret != 0)
  62. printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
  63. nbr_read, last);
  64. return ret;
  65. }