blob: be177753c5906bc7f46c23ffca8bf357849f9422 [file] [log] [blame]
Holger Brunck015a60f2011-09-14 10:54:12 +02001/*
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
30static 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
40int i2c_make_abort(void)
41{
42 struct fsl_i2c *dev;
43 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
44 uchar dummy;
45 uchar last;
46 int nbr_read = 0;
47 int i = 0;
48 int ret = 0;
49
50 /* wait after each operation to finsh with a delay */
51 out_8(&dev->cr, (I2C_CR_MSTA));
52 udelay(DELAY_ABORT_SEQ);
53 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
54 udelay(DELAY_ABORT_SEQ);
55 dummy = in_8(&dev->dr);
56 udelay(DELAY_ABORT_SEQ);
57 last = in_8(&dev->dr);
58 nbr_read++;
59
60 /*
61 * do read until the last bit is 1, but stop if the full eeprom is
62 * read.
63 */
64 while (((last & 0x01) != 0x01) &&
65 (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
66 udelay(DELAY_ABORT_SEQ);
67 last = in_8(&dev->dr);
68 nbr_read++;
69 }
70 if ((last & 0x01) != 0x01)
71 ret = -2;
72 if ((last != 0xff) || (nbr_read > 1))
73 printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
74 nbr_read, last);
75 udelay(DELAY_ABORT_SEQ);
76 out_8(&dev->cr, (I2C_CR_MEN));
77 udelay(DELAY_ABORT_SEQ);
78 /* clear status reg */
79 out_8(&dev->sr, 0);
80
81 for (i = 0; i < 5; i++)
82 i2c_write_start_seq();
83 if (ret != 0)
84 printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
85 nbr_read, last);
86
87 return ret;
88}