Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * TI DaVinci (TMS320DM644x) I2C driver. |
| 3 | * |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 4 | * (C) Copyright 2012-2014 |
| 5 | * Texas Instruments Incorporated, <www.ti.com> |
| 6 | * (C) Copyright 2007 Sergey Kubushyn <ksi@koi8.net> |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 7 | * -------------------------------------------------------- |
| 8 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2852709 | 2016-11-23 06:34:44 -0700 | [diff] [blame] | 10 | * |
| 11 | * NOTE: This driver should be converted to driver model before June 2017. |
| 12 | * Please see doc/driver-model/i2c-howto.txt for instructions. |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <common.h> |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 16 | #include <i2c.h> |
| 17 | #include <asm/arch/hardware.h> |
| 18 | #include <asm/arch/i2c_defs.h> |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 19 | #include <asm/io.h> |
Karicheri, Muralidharan | 356d15e | 2014-04-04 13:16:51 -0400 | [diff] [blame] | 20 | #include "davinci_i2c.h" |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 21 | |
| 22 | #define CHECK_NACK() \ |
| 23 | do {\ |
| 24 | if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\ |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 25 | REG(&(i2c_base->i2c_con)) = 0;\ |
| 26 | return 1;\ |
| 27 | } \ |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 28 | } while (0) |
| 29 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 30 | static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 31 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 32 | static int _wait_for_bus(struct i2c_regs *i2c_base) |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 33 | { |
| 34 | int stat, timeout; |
| 35 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 36 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 37 | |
| 38 | for (timeout = 0; timeout < 10; timeout++) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 39 | stat = REG(&(i2c_base->i2c_stat)); |
| 40 | if (!((stat) & I2C_STAT_BB)) { |
| 41 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
| 42 | return 0; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 45 | REG(&(i2c_base->i2c_stat)) = stat; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 46 | udelay(50000); |
| 47 | } |
| 48 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 49 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
| 50 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 53 | static int _poll_i2c_irq(struct i2c_regs *i2c_base, int mask) |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 54 | { |
| 55 | int stat, timeout; |
| 56 | |
| 57 | for (timeout = 0; timeout < 10; timeout++) { |
| 58 | udelay(1000); |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 59 | stat = REG(&(i2c_base->i2c_stat)); |
| 60 | if (stat & mask) |
| 61 | return stat; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 64 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
| 65 | return stat | I2C_TIMEOUT; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 68 | static void _flush_rx(struct i2c_regs *i2c_base) |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 69 | { |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 70 | while (1) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 71 | if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_RRDY)) |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 72 | break; |
| 73 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 74 | REG(&(i2c_base->i2c_drr)); |
| 75 | REG(&(i2c_base->i2c_stat)) = I2C_STAT_RRDY; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 76 | udelay(1000); |
| 77 | } |
| 78 | } |
| 79 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 80 | static uint _davinci_i2c_setspeed(struct i2c_regs *i2c_base, |
| 81 | uint speed) |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 82 | { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 83 | uint32_t div, psc; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 84 | |
| 85 | psc = 2; |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 86 | /* SCLL + SCLH */ |
| 87 | div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10; |
| 88 | REG(&(i2c_base->i2c_psc)) = psc; /* 27MHz / (2 + 1) = 9MHz */ |
| 89 | REG(&(i2c_base->i2c_scll)) = (div * 50) / 100; /* 50% Duty */ |
| 90 | REG(&(i2c_base->i2c_sclh)) = div - REG(&(i2c_base->i2c_scll)); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 91 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 95 | static void _davinci_i2c_init(struct i2c_regs *i2c_base, |
| 96 | uint speed, int slaveadd) |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 97 | { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 98 | if (REG(&(i2c_base->i2c_con)) & I2C_CON_EN) { |
| 99 | REG(&(i2c_base->i2c_con)) = 0; |
| 100 | udelay(50000); |
| 101 | } |
| 102 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 103 | _davinci_i2c_setspeed(i2c_base, speed); |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 104 | |
| 105 | REG(&(i2c_base->i2c_oa)) = slaveadd; |
| 106 | REG(&(i2c_base->i2c_cnt)) = 0; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 107 | |
| 108 | /* Interrupts must be enabled or I2C module won't work */ |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 109 | REG(&(i2c_base->i2c_ie)) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE | |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 110 | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE; |
| 111 | |
| 112 | /* Now enable I2C controller (get it out of reset) */ |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 113 | REG(&(i2c_base->i2c_con)) = I2C_CON_EN; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 114 | |
| 115 | udelay(1000); |
| 116 | } |
| 117 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 118 | static int _davinci_i2c_read(struct i2c_regs *i2c_base, uint8_t chip, |
| 119 | uint32_t addr, int alen, uint8_t *buf, int len) |
Heiko Schocher | 49d6da6 | 2011-09-14 19:25:12 +0000 | [diff] [blame] | 120 | { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 121 | uint32_t tmp; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 122 | int i; |
| 123 | |
| 124 | if ((alen < 0) || (alen > 2)) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 125 | printf("%s(): bogus address length %x\n", __func__, alen); |
| 126 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 129 | if (_wait_for_bus(i2c_base)) |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 130 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 131 | |
| 132 | if (alen != 0) { |
| 133 | /* Start address phase */ |
| 134 | tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX; |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 135 | REG(&(i2c_base->i2c_cnt)) = alen; |
| 136 | REG(&(i2c_base->i2c_sa)) = chip; |
| 137 | REG(&(i2c_base->i2c_con)) = tmp; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 138 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 139 | tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 140 | |
| 141 | CHECK_NACK(); |
| 142 | |
| 143 | switch (alen) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 144 | case 2: |
| 145 | /* Send address MSByte */ |
| 146 | if (tmp & I2C_STAT_XRDY) { |
| 147 | REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff; |
| 148 | } else { |
| 149 | REG(&(i2c_base->i2c_con)) = 0; |
| 150 | return 1; |
| 151 | } |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 152 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 153 | tmp = _poll_i2c_irq(i2c_base, |
| 154 | I2C_STAT_XRDY | I2C_STAT_NACK); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 155 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 156 | CHECK_NACK(); |
| 157 | /* No break, fall through */ |
| 158 | case 1: |
| 159 | /* Send address LSByte */ |
| 160 | if (tmp & I2C_STAT_XRDY) { |
| 161 | REG(&(i2c_base->i2c_dxr)) = addr & 0xff; |
| 162 | } else { |
| 163 | REG(&(i2c_base->i2c_con)) = 0; |
| 164 | return 1; |
| 165 | } |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 166 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 167 | tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | |
| 168 | I2C_STAT_NACK | I2C_STAT_ARDY); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 169 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 170 | CHECK_NACK(); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 171 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 172 | if (!(tmp & I2C_STAT_ARDY)) { |
| 173 | REG(&(i2c_base->i2c_con)) = 0; |
| 174 | return 1; |
| 175 | } |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | /* Address phase is over, now read 'len' bytes and stop */ |
| 180 | tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP; |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 181 | REG(&(i2c_base->i2c_cnt)) = len & 0xffff; |
| 182 | REG(&(i2c_base->i2c_sa)) = chip; |
| 183 | REG(&(i2c_base->i2c_con)) = tmp; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 184 | |
| 185 | for (i = 0; i < len; i++) { |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 186 | tmp = _poll_i2c_irq(i2c_base, I2C_STAT_RRDY | I2C_STAT_NACK | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 187 | I2C_STAT_ROVR); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 188 | |
| 189 | CHECK_NACK(); |
| 190 | |
| 191 | if (tmp & I2C_STAT_RRDY) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 192 | buf[i] = REG(&(i2c_base->i2c_drr)); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 193 | } else { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 194 | REG(&(i2c_base->i2c_con)) = 0; |
| 195 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 199 | tmp = _poll_i2c_irq(i2c_base, I2C_STAT_SCD | I2C_STAT_NACK); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 200 | |
| 201 | CHECK_NACK(); |
| 202 | |
| 203 | if (!(tmp & I2C_STAT_SCD)) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 204 | REG(&(i2c_base->i2c_con)) = 0; |
| 205 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 208 | _flush_rx(i2c_base); |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 209 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
| 210 | REG(&(i2c_base->i2c_cnt)) = 0; |
| 211 | REG(&(i2c_base->i2c_con)) = 0; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 212 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 213 | return 0; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 216 | static int _davinci_i2c_write(struct i2c_regs *i2c_base, uint8_t chip, |
| 217 | uint32_t addr, int alen, uint8_t *buf, int len) |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 218 | { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 219 | uint32_t tmp; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 220 | int i; |
| 221 | |
| 222 | if ((alen < 0) || (alen > 2)) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 223 | printf("%s(): bogus address length %x\n", __func__, alen); |
| 224 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 225 | } |
| 226 | if (len < 0) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 227 | printf("%s(): bogus length %x\n", __func__, len); |
| 228 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 231 | if (_wait_for_bus(i2c_base)) |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 232 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 233 | |
| 234 | /* Start address phase */ |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 235 | tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | |
| 236 | I2C_CON_TRX | I2C_CON_STP; |
| 237 | REG(&(i2c_base->i2c_cnt)) = (alen == 0) ? |
| 238 | len & 0xffff : (len & 0xffff) + alen; |
| 239 | REG(&(i2c_base->i2c_sa)) = chip; |
| 240 | REG(&(i2c_base->i2c_con)) = tmp; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 241 | |
| 242 | switch (alen) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 243 | case 2: |
| 244 | /* Send address MSByte */ |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 245 | tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 246 | |
| 247 | CHECK_NACK(); |
| 248 | |
| 249 | if (tmp & I2C_STAT_XRDY) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 250 | REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 251 | } else { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 252 | REG(&(i2c_base->i2c_con)) = 0; |
| 253 | return 1; |
| 254 | } |
| 255 | /* No break, fall through */ |
| 256 | case 1: |
| 257 | /* Send address LSByte */ |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 258 | tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK); |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 259 | |
| 260 | CHECK_NACK(); |
| 261 | |
| 262 | if (tmp & I2C_STAT_XRDY) { |
| 263 | REG(&(i2c_base->i2c_dxr)) = addr & 0xff; |
| 264 | } else { |
| 265 | REG(&(i2c_base->i2c_con)) = 0; |
| 266 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 270 | for (i = 0; i < len; i++) { |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 271 | tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK); |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 272 | |
| 273 | CHECK_NACK(); |
| 274 | |
| 275 | if (tmp & I2C_STAT_XRDY) |
| 276 | REG(&(i2c_base->i2c_dxr)) = buf[i]; |
| 277 | else |
| 278 | return 1; |
| 279 | } |
| 280 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 281 | tmp = _poll_i2c_irq(i2c_base, I2C_STAT_SCD | I2C_STAT_NACK); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 282 | |
| 283 | CHECK_NACK(); |
| 284 | |
| 285 | if (!(tmp & I2C_STAT_SCD)) { |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 286 | REG(&(i2c_base->i2c_con)) = 0; |
| 287 | return 1; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 290 | _flush_rx(i2c_base); |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 291 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
| 292 | REG(&(i2c_base->i2c_cnt)) = 0; |
| 293 | REG(&(i2c_base->i2c_con)) = 0; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 294 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 295 | return 0; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 296 | } |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 297 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 298 | static int _davinci_i2c_probe_chip(struct i2c_regs *i2c_base, uint8_t chip) |
| 299 | { |
| 300 | int rc = 1; |
| 301 | |
| 302 | if (chip == REG(&(i2c_base->i2c_oa))) |
| 303 | return rc; |
| 304 | |
| 305 | REG(&(i2c_base->i2c_con)) = 0; |
| 306 | if (_wait_for_bus(i2c_base)) |
| 307 | return 1; |
| 308 | |
| 309 | /* try to read one byte from current (or only) address */ |
| 310 | REG(&(i2c_base->i2c_cnt)) = 1; |
| 311 | REG(&(i2c_base->i2c_sa)) = chip; |
| 312 | REG(&(i2c_base->i2c_con)) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | |
| 313 | I2C_CON_STP); |
| 314 | udelay(50000); |
| 315 | |
| 316 | if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_NACK)) { |
| 317 | rc = 0; |
| 318 | _flush_rx(i2c_base); |
| 319 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
| 320 | } else { |
| 321 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
| 322 | REG(&(i2c_base->i2c_con)) |= I2C_CON_STP; |
| 323 | udelay(20000); |
| 324 | if (_wait_for_bus(i2c_base)) |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | _flush_rx(i2c_base); |
| 329 | REG(&(i2c_base->i2c_stat)) = 0xffff; |
| 330 | REG(&(i2c_base->i2c_cnt)) = 0; |
| 331 | return rc; |
| 332 | } |
| 333 | |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 334 | static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap) |
| 335 | { |
| 336 | switch (adap->hwadapnr) { |
| 337 | #if I2C_BUS_MAX >= 3 |
| 338 | case 2: |
| 339 | return (struct i2c_regs *)I2C2_BASE; |
| 340 | #endif |
| 341 | #if I2C_BUS_MAX >= 2 |
| 342 | case 1: |
| 343 | return (struct i2c_regs *)I2C1_BASE; |
| 344 | #endif |
| 345 | case 0: |
| 346 | return (struct i2c_regs *)I2C_BASE; |
| 347 | |
| 348 | default: |
| 349 | printf("wrong hwadapnr: %d\n", adap->hwadapnr); |
| 350 | } |
| 351 | |
| 352 | return NULL; |
| 353 | } |
| 354 | |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 355 | static uint davinci_i2c_setspeed(struct i2c_adapter *adap, uint speed) |
| 356 | { |
| 357 | struct i2c_regs *i2c_base = davinci_get_base(adap); |
| 358 | uint ret; |
| 359 | |
| 360 | adap->speed = speed; |
| 361 | ret = _davinci_i2c_setspeed(i2c_base, speed); |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static void davinci_i2c_init(struct i2c_adapter *adap, int speed, |
| 367 | int slaveadd) |
| 368 | { |
| 369 | struct i2c_regs *i2c_base = davinci_get_base(adap); |
| 370 | |
| 371 | adap->speed = speed; |
| 372 | _davinci_i2c_init(i2c_base, speed, slaveadd); |
| 373 | |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | static int davinci_i2c_read(struct i2c_adapter *adap, uint8_t chip, |
| 378 | uint32_t addr, int alen, uint8_t *buf, int len) |
| 379 | { |
| 380 | struct i2c_regs *i2c_base = davinci_get_base(adap); |
| 381 | return _davinci_i2c_read(i2c_base, chip, addr, alen, buf, len); |
| 382 | } |
| 383 | |
| 384 | static int davinci_i2c_write(struct i2c_adapter *adap, uint8_t chip, |
| 385 | uint32_t addr, int alen, uint8_t *buf, int len) |
| 386 | { |
| 387 | struct i2c_regs *i2c_base = davinci_get_base(adap); |
| 388 | |
| 389 | return _davinci_i2c_write(i2c_base, chip, addr, alen, buf, len); |
| 390 | } |
| 391 | |
| 392 | static int davinci_i2c_probe_chip(struct i2c_adapter *adap, uint8_t chip) |
| 393 | { |
| 394 | struct i2c_regs *i2c_base = davinci_get_base(adap); |
| 395 | |
| 396 | return _davinci_i2c_probe_chip(i2c_base, chip); |
| 397 | } |
| 398 | |
| 399 | U_BOOT_I2C_ADAP_COMPLETE(davinci_0, davinci_i2c_init, davinci_i2c_probe_chip, |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 400 | davinci_i2c_read, davinci_i2c_write, |
| 401 | davinci_i2c_setspeed, |
| 402 | CONFIG_SYS_DAVINCI_I2C_SPEED, |
| 403 | CONFIG_SYS_DAVINCI_I2C_SLAVE, |
| 404 | 0) |
| 405 | |
| 406 | #if I2C_BUS_MAX >= 2 |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 407 | U_BOOT_I2C_ADAP_COMPLETE(davinci_1, davinci_i2c_init, davinci_i2c_probe_chip, |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 408 | davinci_i2c_read, davinci_i2c_write, |
| 409 | davinci_i2c_setspeed, |
| 410 | CONFIG_SYS_DAVINCI_I2C_SPEED1, |
| 411 | CONFIG_SYS_DAVINCI_I2C_SLAVE1, |
| 412 | 1) |
| 413 | #endif |
| 414 | |
| 415 | #if I2C_BUS_MAX >= 3 |
Cooper Jr., Franklin | 6021910 | 2017-04-20 10:25:42 -0500 | [diff] [blame^] | 416 | U_BOOT_I2C_ADAP_COMPLETE(davinci_2, davinci_i2c_init, davinci_i2c_probe_chip, |
Vitaly Andrianov | e8459dc | 2014-04-04 13:16:52 -0400 | [diff] [blame] | 417 | davinci_i2c_read, davinci_i2c_write, |
| 418 | davinci_i2c_setspeed, |
| 419 | CONFIG_SYS_DAVINCI_I2C_SPEED2, |
| 420 | CONFIG_SYS_DAVINCI_I2C_SLAVE2, |
| 421 | 2) |
| 422 | #endif |