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