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