km83xx_i2c.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * (C) Copyright 2011
  3. * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <i2c.h>
  25. #include <asm/io.h>
  26. #include <linux/ctype.h>
  27. #include "../common/common.h"
  28. static void i2c_write_start_seq(void)
  29. {
  30. struct fsl_i2c *dev;
  31. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  32. udelay(DELAY_ABORT_SEQ);
  33. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  34. udelay(DELAY_ABORT_SEQ);
  35. out_8(&dev->cr, (I2C_CR_MEN));
  36. }
  37. int i2c_make_abort(void)
  38. {
  39. struct fsl_i2c *dev;
  40. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  41. uchar dummy;
  42. uchar last;
  43. int nbr_read = 0;
  44. int i = 0;
  45. int ret = 0;
  46. /* wait after each operation to finsh with a delay */
  47. out_8(&dev->cr, (I2C_CR_MSTA));
  48. udelay(DELAY_ABORT_SEQ);
  49. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  50. udelay(DELAY_ABORT_SEQ);
  51. dummy = in_8(&dev->dr);
  52. udelay(DELAY_ABORT_SEQ);
  53. last = in_8(&dev->dr);
  54. nbr_read++;
  55. /*
  56. * do read until the last bit is 1, but stop if the full eeprom is
  57. * read.
  58. */
  59. while (((last & 0x01) != 0x01) &&
  60. (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
  61. udelay(DELAY_ABORT_SEQ);
  62. last = in_8(&dev->dr);
  63. nbr_read++;
  64. }
  65. if ((last & 0x01) != 0x01)
  66. ret = -2;
  67. if ((last != 0xff) || (nbr_read > 1))
  68. printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
  69. nbr_read, last);
  70. udelay(DELAY_ABORT_SEQ);
  71. out_8(&dev->cr, (I2C_CR_MEN));
  72. udelay(DELAY_ABORT_SEQ);
  73. /* clear status reg */
  74. out_8(&dev->sr, 0);
  75. for (i = 0; i < 5; i++)
  76. i2c_write_start_seq();
  77. if (ret != 0)
  78. printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
  79. nbr_read, last);
  80. return ret;
  81. }