1*e792affeSHolger Brunck /* 2*e792affeSHolger Brunck * (C) Copyright 2011 3*e792affeSHolger Brunck * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com 4*e792affeSHolger Brunck * 5*e792affeSHolger Brunck * See file CREDITS for list of people who contributed to this 6*e792affeSHolger Brunck * project. 7*e792affeSHolger Brunck * 8*e792affeSHolger Brunck * This program is free software; you can redistribute it and/or 9*e792affeSHolger Brunck * modify it under the terms of the GNU General Public License as 10*e792affeSHolger Brunck * published by the Free Software Foundation; either version 2 of 11*e792affeSHolger Brunck * the License, or (at your option) any later version. 12*e792affeSHolger Brunck * 13*e792affeSHolger Brunck * This program is distributed in the hope that it will be useful, 14*e792affeSHolger Brunck * but WITHOUT ANY WARRANTY; without even the implied warranty of 15*e792affeSHolger Brunck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*e792affeSHolger Brunck * GNU General Public License for more details. 17*e792affeSHolger Brunck * 18*e792affeSHolger Brunck * You should have received a copy of the GNU General Public License 19*e792affeSHolger Brunck * along with this program; if not, write to the Free Software 20*e792affeSHolger Brunck * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21*e792affeSHolger Brunck * MA 02111-1307 USA 22*e792affeSHolger Brunck */ 23*e792affeSHolger Brunck 24*e792affeSHolger Brunck #include <common.h> 25*e792affeSHolger Brunck #include <i2c.h> 26*e792affeSHolger Brunck #include <asm/io.h> 27*e792affeSHolger Brunck #include <linux/ctype.h> 28*e792affeSHolger Brunck #include "../common/common.h" 29*e792affeSHolger Brunck 30*e792affeSHolger Brunck static void i2c_write_start_seq(void) 31*e792affeSHolger Brunck { 32*e792affeSHolger Brunck struct fsl_i2c *dev; 33*e792affeSHolger Brunck dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET); 34*e792affeSHolger Brunck udelay(DELAY_ABORT_SEQ); 35*e792affeSHolger Brunck out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA)); 36*e792affeSHolger Brunck udelay(DELAY_ABORT_SEQ); 37*e792affeSHolger Brunck out_8(&dev->cr, (I2C_CR_MEN)); 38*e792affeSHolger Brunck } 39*e792affeSHolger Brunck 40*e792affeSHolger Brunck int i2c_make_abort(void) 41*e792affeSHolger Brunck { 42*e792affeSHolger Brunck struct fsl_i2c *dev; 43*e792affeSHolger Brunck dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET); 44*e792affeSHolger Brunck uchar dummy; 45*e792affeSHolger Brunck uchar last; 46*e792affeSHolger Brunck int nbr_read = 0; 47*e792affeSHolger Brunck int i = 0; 48*e792affeSHolger Brunck int ret = 0; 49*e792affeSHolger Brunck 50*e792affeSHolger Brunck /* wait after each operation to finsh with a delay */ 51*e792affeSHolger Brunck out_8(&dev->cr, (I2C_CR_MSTA)); 52*e792affeSHolger Brunck udelay(DELAY_ABORT_SEQ); 53*e792affeSHolger Brunck out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA)); 54*e792affeSHolger Brunck udelay(DELAY_ABORT_SEQ); 55*e792affeSHolger Brunck dummy = in_8(&dev->dr); 56*e792affeSHolger Brunck udelay(DELAY_ABORT_SEQ); 57*e792affeSHolger Brunck last = in_8(&dev->dr); 58*e792affeSHolger Brunck nbr_read++; 59*e792affeSHolger Brunck 60*e792affeSHolger Brunck /* 61*e792affeSHolger Brunck * do read until the last bit is 1, but stop if the full eeprom is 62*e792affeSHolger Brunck * read. 63*e792affeSHolger Brunck */ 64*e792affeSHolger Brunck while (((last & 0x01) != 0x01) && 65*e792affeSHolger Brunck (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) { 66*e792affeSHolger Brunck udelay(DELAY_ABORT_SEQ); 67*e792affeSHolger Brunck last = in_8(&dev->dr); 68*e792affeSHolger Brunck nbr_read++; 69*e792affeSHolger Brunck } 70*e792affeSHolger Brunck if ((last & 0x01) != 0x01) 71*e792affeSHolger Brunck ret = -2; 72*e792affeSHolger Brunck if ((last != 0xff) || (nbr_read > 1)) 73*e792affeSHolger Brunck printf("[INFO] i2c abort after %d bytes (0x%02x)\n", 74*e792affeSHolger Brunck nbr_read, last); 75*e792affeSHolger Brunck udelay(DELAY_ABORT_SEQ); 76*e792affeSHolger Brunck out_8(&dev->cr, (I2C_CR_MEN)); 77*e792affeSHolger Brunck udelay(DELAY_ABORT_SEQ); 78*e792affeSHolger Brunck /* clear status reg */ 79*e792affeSHolger Brunck out_8(&dev->sr, 0); 80*e792affeSHolger Brunck 81*e792affeSHolger Brunck for (i = 0; i < 5; i++) 82*e792affeSHolger Brunck i2c_write_start_seq(); 83*e792affeSHolger Brunck if (ret != 0) 84*e792affeSHolger Brunck printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n", 85*e792affeSHolger Brunck nbr_read, last); 86*e792affeSHolger Brunck 87*e792affeSHolger Brunck return ret; 88*e792affeSHolger Brunck } 89