Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 |
| 3 | * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 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 | { |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 16 | struct fsl_i2c_base *base; |
| 17 | base = (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + |
| 18 | CONFIG_SYS_I2C_OFFSET); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 19 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 20 | out_8(&base->cr, (I2C_CR_MEN | I2C_CR_MSTA)); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 21 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 22 | out_8(&base->cr, (I2C_CR_MEN)); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | int i2c_make_abort(void) |
| 26 | { |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 27 | struct fsl_i2c_base *base; |
| 28 | base = (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + |
| 29 | CONFIG_SYS_I2C_OFFSET); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 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 */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 36 | out_8(&base->cr, (I2C_CR_MSTA)); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 37 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 38 | out_8(&base->cr, (I2C_CR_MEN | I2C_CR_MSTA)); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 39 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 40 | in_8(&base->dr); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 41 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 42 | last = in_8(&base->dr); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 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); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 52 | last = in_8(&base->dr); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 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); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 61 | out_8(&base->cr, (I2C_CR_MEN)); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 62 | udelay(DELAY_ABORT_SEQ); |
| 63 | /* clear status reg */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 64 | out_8(&base->sr, 0); |
Holger Brunck | e792aff | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 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 | } |