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