Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2009 |
| 3 | * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 9 | #include <i2c.h> |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 10 | #include <asm/io.h> |
Vipin KUMAR | 3f64acb | 2012-02-26 23:13:29 +0000 | [diff] [blame] | 11 | #include "designware_i2c.h" |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 12 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 13 | static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap) |
| 14 | { |
| 15 | switch (adap->hwadapnr) { |
| 16 | #if CONFIG_SYS_I2C_BUS_MAX >= 4 |
| 17 | case 3: |
| 18 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3; |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 19 | #endif |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 20 | #if CONFIG_SYS_I2C_BUS_MAX >= 3 |
| 21 | case 2: |
| 22 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2; |
| 23 | #endif |
| 24 | #if CONFIG_SYS_I2C_BUS_MAX >= 2 |
| 25 | case 1: |
| 26 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1; |
| 27 | #endif |
| 28 | case 0: |
| 29 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE; |
| 30 | default: |
| 31 | printf("Wrong I2C-adapter number %d\n", adap->hwadapnr); |
| 32 | } |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 33 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 34 | return NULL; |
| 35 | } |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * set_speed - Set the i2c speed mode (standard, high, fast) |
| 39 | * @i2c_spd: required i2c speed mode |
| 40 | * |
| 41 | * Set the i2c speed mode (standard, high, fast) |
| 42 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 43 | static void set_speed(struct i2c_adapter *adap, int i2c_spd) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 44 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 45 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 46 | unsigned int cntl; |
| 47 | unsigned int hcnt, lcnt; |
Armando Visconti | 631e693 | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 48 | unsigned int enbl; |
| 49 | |
| 50 | /* to set speed cltr must be disabled */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 51 | enbl = readl(&i2c_base->ic_enable); |
Armando Visconti | 631e693 | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 52 | enbl &= ~IC_ENABLE_0B; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 53 | writel(enbl, &i2c_base->ic_enable); |
Armando Visconti | 631e693 | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 54 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 55 | cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK)); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 56 | |
| 57 | switch (i2c_spd) { |
| 58 | case IC_SPEED_MODE_MAX: |
| 59 | cntl |= IC_CON_SPD_HS; |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 60 | hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 61 | writel(hcnt, &i2c_base->ic_hs_scl_hcnt); |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 62 | lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 63 | writel(lcnt, &i2c_base->ic_hs_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 64 | break; |
| 65 | |
| 66 | case IC_SPEED_MODE_STANDARD: |
| 67 | cntl |= IC_CON_SPD_SS; |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 68 | hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 69 | writel(hcnt, &i2c_base->ic_ss_scl_hcnt); |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 70 | lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 71 | writel(lcnt, &i2c_base->ic_ss_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 72 | break; |
| 73 | |
| 74 | case IC_SPEED_MODE_FAST: |
| 75 | default: |
| 76 | cntl |= IC_CON_SPD_FS; |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 77 | hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 78 | writel(hcnt, &i2c_base->ic_fs_scl_hcnt); |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 79 | lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 80 | writel(lcnt, &i2c_base->ic_fs_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 81 | break; |
| 82 | } |
| 83 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 84 | writel(cntl, &i2c_base->ic_con); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 85 | |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 86 | /* Enable back i2c now speed set */ |
Armando Visconti | 631e693 | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 87 | enbl |= IC_ENABLE_0B; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 88 | writel(enbl, &i2c_base->ic_enable); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * i2c_set_bus_speed - Set the i2c speed |
| 93 | * @speed: required i2c speed |
| 94 | * |
| 95 | * Set the i2c speed. |
| 96 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 97 | static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 98 | unsigned int speed) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 99 | { |
Jeroen Hofstee | dffb827 | 2014-10-08 22:58:09 +0200 | [diff] [blame] | 100 | int i2c_spd; |
| 101 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 102 | if (speed >= I2C_MAX_SPEED) |
Jeroen Hofstee | dffb827 | 2014-10-08 22:58:09 +0200 | [diff] [blame] | 103 | i2c_spd = IC_SPEED_MODE_MAX; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 104 | else if (speed >= I2C_FAST_SPEED) |
Jeroen Hofstee | dffb827 | 2014-10-08 22:58:09 +0200 | [diff] [blame] | 105 | i2c_spd = IC_SPEED_MODE_FAST; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 106 | else |
Jeroen Hofstee | dffb827 | 2014-10-08 22:58:09 +0200 | [diff] [blame] | 107 | i2c_spd = IC_SPEED_MODE_STANDARD; |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 108 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 109 | set_speed(adap, i2c_spd); |
| 110 | adap->speed = speed; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * i2c_init - Init function |
| 117 | * @speed: required i2c speed |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 118 | * @slaveaddr: slave address for the device |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 119 | * |
| 120 | * Initialization function. |
| 121 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 122 | static void dw_i2c_init(struct i2c_adapter *adap, int speed, |
| 123 | int slaveaddr) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 124 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 125 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 126 | unsigned int enbl; |
| 127 | |
| 128 | /* Disable i2c */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 129 | enbl = readl(&i2c_base->ic_enable); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 130 | enbl &= ~IC_ENABLE_0B; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 131 | writel(enbl, &i2c_base->ic_enable); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 132 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 133 | writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_base->ic_con); |
| 134 | writel(IC_RX_TL, &i2c_base->ic_rx_tl); |
| 135 | writel(IC_TX_TL, &i2c_base->ic_tx_tl); |
| 136 | dw_i2c_set_bus_speed(adap, speed); |
| 137 | writel(IC_STOP_DET, &i2c_base->ic_intr_mask); |
| 138 | writel(slaveaddr, &i2c_base->ic_sar); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 139 | |
| 140 | /* Enable i2c */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 141 | enbl = readl(&i2c_base->ic_enable); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 142 | enbl |= IC_ENABLE_0B; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 143 | writel(enbl, &i2c_base->ic_enable); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * i2c_setaddress - Sets the target slave address |
| 148 | * @i2c_addr: target i2c address |
| 149 | * |
| 150 | * Sets the target slave address. |
| 151 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 152 | static void i2c_setaddress(struct i2c_adapter *adap, unsigned int i2c_addr) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 153 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 154 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 155 | unsigned int enbl; |
| 156 | |
| 157 | /* Disable i2c */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 158 | enbl = readl(&i2c_base->ic_enable); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 159 | enbl &= ~IC_ENABLE_0B; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 160 | writel(enbl, &i2c_base->ic_enable); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 161 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 162 | writel(i2c_addr, &i2c_base->ic_tar); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 163 | |
| 164 | /* Enable i2c */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 165 | enbl = readl(&i2c_base->ic_enable); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 166 | enbl |= IC_ENABLE_0B; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 167 | writel(enbl, &i2c_base->ic_enable); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
| 171 | * i2c_flush_rxfifo - Flushes the i2c RX FIFO |
| 172 | * |
| 173 | * Flushes the i2c RX FIFO |
| 174 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 175 | static void i2c_flush_rxfifo(struct i2c_adapter *adap) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 176 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 177 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
| 178 | |
| 179 | while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) |
| 180 | readl(&i2c_base->ic_cmd_data); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* |
| 184 | * i2c_wait_for_bb - Waits for bus busy |
| 185 | * |
| 186 | * Waits for bus busy |
| 187 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 188 | static int i2c_wait_for_bb(struct i2c_adapter *adap) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 189 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 190 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 191 | unsigned long start_time_bb = get_timer(0); |
| 192 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 193 | while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) || |
| 194 | !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) { |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 195 | |
| 196 | /* Evaluate timeout */ |
| 197 | if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB)) |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 204 | static int i2c_xfer_init(struct i2c_adapter *adap, uchar chip, uint addr, |
| 205 | int alen) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 206 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 207 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
| 208 | |
| 209 | if (i2c_wait_for_bb(adap)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 210 | return 1; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 211 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 212 | i2c_setaddress(adap, chip); |
Chin Liang See | a0c2626 | 2014-02-04 11:56:23 -0600 | [diff] [blame] | 213 | while (alen) { |
| 214 | alen--; |
| 215 | /* high byte address going out first */ |
| 216 | writel((addr >> (alen * 8)) & 0xff, |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 217 | &i2c_base->ic_cmd_data); |
Chin Liang See | a0c2626 | 2014-02-04 11:56:23 -0600 | [diff] [blame] | 218 | } |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 222 | static int i2c_xfer_finish(struct i2c_adapter *adap) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 223 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 224 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 225 | ulong start_stop_det = get_timer(0); |
| 226 | |
| 227 | while (1) { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 228 | if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) { |
| 229 | readl(&i2c_base->ic_clr_stop_det); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 230 | break; |
| 231 | } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) { |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 236 | if (i2c_wait_for_bb(adap)) { |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 237 | printf("Timed out waiting for bus\n"); |
| 238 | return 1; |
| 239 | } |
| 240 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 241 | i2c_flush_rxfifo(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 242 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * i2c_read - Read from i2c memory |
| 248 | * @chip: target i2c address |
| 249 | * @addr: address to read from |
| 250 | * @alen: |
| 251 | * @buffer: buffer for read data |
| 252 | * @len: no of bytes to be read |
| 253 | * |
| 254 | * Read from i2c memory. |
| 255 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 256 | static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, |
| 257 | int alen, u8 *buffer, int len) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 258 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 259 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 260 | unsigned long start_time_rx; |
| 261 | |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 262 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 263 | /* |
| 264 | * EEPROM chips that implement "address overflow" are ones |
| 265 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 266 | * address and the extra bits end up in the "chip address" |
| 267 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 268 | * four 256 byte chips. |
| 269 | * |
| 270 | * Note that we consider the length of the address field to |
| 271 | * still be one byte because the extra address bits are |
| 272 | * hidden in the chip address. |
| 273 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 274 | dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 275 | addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8)); |
| 276 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 277 | debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev, |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 278 | addr); |
| 279 | #endif |
| 280 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 281 | if (i2c_xfer_init(adap, dev, addr, alen)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 282 | return 1; |
| 283 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 284 | start_time_rx = get_timer(0); |
| 285 | while (len) { |
Armando Visconti | 6bc6ef7 | 2012-12-06 00:04:16 +0000 | [diff] [blame] | 286 | if (len == 1) |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 287 | writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data); |
Armando Visconti | 6bc6ef7 | 2012-12-06 00:04:16 +0000 | [diff] [blame] | 288 | else |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 289 | writel(IC_CMD, &i2c_base->ic_cmd_data); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 290 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 291 | if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) { |
| 292 | *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 293 | len--; |
| 294 | start_time_rx = get_timer(0); |
| 295 | |
| 296 | } else if (get_timer(start_time_rx) > I2C_BYTE_TO) { |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 297 | return 1; |
| 298 | } |
| 299 | } |
| 300 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 301 | return i2c_xfer_finish(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
| 305 | * i2c_write - Write to i2c memory |
| 306 | * @chip: target i2c address |
| 307 | * @addr: address to read from |
| 308 | * @alen: |
| 309 | * @buffer: buffer for read data |
| 310 | * @len: no of bytes to be read |
| 311 | * |
| 312 | * Write to i2c memory. |
| 313 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 314 | static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, |
| 315 | int alen, u8 *buffer, int len) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 316 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 317 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 318 | int nb = len; |
| 319 | unsigned long start_time_tx; |
| 320 | |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 321 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 322 | /* |
| 323 | * EEPROM chips that implement "address overflow" are ones |
| 324 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 325 | * address and the extra bits end up in the "chip address" |
| 326 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 327 | * four 256 byte chips. |
| 328 | * |
| 329 | * Note that we consider the length of the address field to |
| 330 | * still be one byte because the extra address bits are |
| 331 | * hidden in the chip address. |
| 332 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 333 | dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 334 | addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8)); |
| 335 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 336 | debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev, |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 337 | addr); |
| 338 | #endif |
| 339 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 340 | if (i2c_xfer_init(adap, dev, addr, alen)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 341 | return 1; |
| 342 | |
| 343 | start_time_tx = get_timer(0); |
| 344 | while (len) { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 345 | if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) { |
| 346 | if (--len == 0) { |
| 347 | writel(*buffer | IC_STOP, |
| 348 | &i2c_base->ic_cmd_data); |
| 349 | } else { |
| 350 | writel(*buffer, &i2c_base->ic_cmd_data); |
| 351 | } |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 352 | buffer++; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 353 | start_time_tx = get_timer(0); |
| 354 | |
| 355 | } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) { |
| 356 | printf("Timed out. i2c write Failed\n"); |
| 357 | return 1; |
| 358 | } |
| 359 | } |
| 360 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 361 | return i2c_xfer_finish(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* |
| 365 | * i2c_probe - Probe the i2c chip |
| 366 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 367 | static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 368 | { |
| 369 | u32 tmp; |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 370 | int ret; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * Try to read the first location of the chip. |
| 374 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 375 | ret = dw_i2c_read(adap, dev, 0, 1, (uchar *)&tmp, 1); |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 376 | if (ret) |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 377 | dw_i2c_init(adap, adap->speed, adap->slaveaddr); |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 378 | |
| 379 | return ret; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 380 | } |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 381 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 382 | U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 383 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 384 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0) |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 385 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 386 | #if CONFIG_SYS_I2C_BUS_MAX >= 2 |
| 387 | U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 388 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 389 | CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1) |
| 390 | #endif |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 391 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 392 | #if CONFIG_SYS_I2C_BUS_MAX >= 3 |
| 393 | U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 394 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 395 | CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2) |
| 396 | #endif |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 397 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame^] | 398 | #if CONFIG_SYS_I2C_BUS_MAX >= 4 |
| 399 | U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 400 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 401 | CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3) |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 402 | #endif |