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