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