km83xx_i2c.c 1.7 KB

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