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> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 11 | #include <linux/delay.h> |
Holger Brunck | 015a60f | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 12 | #include "../common/common.h" |
| 13 | |
| 14 | static void i2c_write_start_seq(void) |
| 15 | { |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 16 | struct fsl_i2c_base *base; |
| 17 | base = (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + |
Tom Rini | be94c76 | 2021-08-18 23:12:35 -0400 | [diff] [blame] | 18 | CONFIG_SYS_FSL_I2C_OFFSET); |
Holger Brunck | 015a60f | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 19 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 20 | out_8(&base->cr, (I2C_CR_MEN | I2C_CR_MSTA)); |
Holger Brunck | 015a60f | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 21 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 22 | out_8(&base->cr, (I2C_CR_MEN)); |
Holger Brunck | 015a60f | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | int i2c_make_abort(void) |
| 26 | { |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 27 | struct fsl_i2c_base *base; |
| 28 | base = (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + |
Tom Rini | be94c76 | 2021-08-18 23:12:35 -0400 | [diff] [blame] | 29 | CONFIG_SYS_FSL_I2C_OFFSET); |
Holger Brunck | 015a60f | 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 | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 36 | out_8(&base->cr, (I2C_CR_MSTA)); |
Holger Brunck | 015a60f | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 37 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 38 | out_8(&base->cr, (I2C_CR_MEN | I2C_CR_MSTA)); |
Holger Brunck | 015a60f | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 39 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 40 | in_8(&base->dr); |
Holger Brunck | 015a60f | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 41 | udelay(DELAY_ABORT_SEQ); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 42 | last = in_8(&base->dr); |
Holger Brunck | 015a60f | 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 | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 52 | last = in_8(&base->dr); |
Holger Brunck | 015a60f | 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 | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 61 | out_8(&base->cr, (I2C_CR_MEN)); |
Holger Brunck | 015a60f | 2011-09-14 10:54:12 +0200 | [diff] [blame] | 62 | udelay(DELAY_ABORT_SEQ); |
| 63 | /* clear status reg */ |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 64 | out_8(&base->sr, 0); |
Holger Brunck | 015a60f | 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 | } |