3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <asm/arch/hardware.h>
27 #include "designware_i2c.h"
29 static struct i2c_regs *const i2c_regs_p =
30 (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
33 * set_speed - Set the i2c speed mode (standard, high, fast)
34 * @i2c_spd: required i2c speed mode
36 * Set the i2c speed mode (standard, high, fast)
38 static void set_speed(int i2c_spd)
41 unsigned int hcnt, lcnt;
42 unsigned int high, low;
45 /* to set speed cltr must be disabled */
46 enbl = readl(&i2c_regs_p->ic_enable);
47 enbl &= ~IC_ENABLE_0B;
48 writel(enbl, &i2c_regs_p->ic_enable);
51 cntl = (readl(&i2c_regs_p->ic_con) & (~IC_CON_SPD_MSK));
54 case IC_SPEED_MODE_MAX:
55 cntl |= IC_CON_SPD_HS;
56 high = MIN_HS_SCL_HIGHTIME;
57 low = MIN_HS_SCL_LOWTIME;
60 case IC_SPEED_MODE_STANDARD:
61 cntl |= IC_CON_SPD_SS;
62 high = MIN_SS_SCL_HIGHTIME;
63 low = MIN_SS_SCL_LOWTIME;
66 case IC_SPEED_MODE_FAST:
68 cntl |= IC_CON_SPD_FS;
69 high = MIN_FS_SCL_HIGHTIME;
70 low = MIN_FS_SCL_LOWTIME;
74 writel(cntl, &i2c_regs_p->ic_con);
76 hcnt = (IC_CLK * high) / NANO_TO_MICRO;
77 writel(hcnt, &i2c_regs_p->ic_fs_scl_hcnt);
79 lcnt = (IC_CLK * low) / NANO_TO_MICRO;
80 writel(lcnt, &i2c_regs_p->ic_fs_scl_lcnt);
82 /* re-enable i2c ctrl back now that speed is set */
84 writel(enbl, &i2c_regs_p->ic_enable);
88 * i2c_set_bus_speed - Set the i2c speed
89 * @speed: required i2c speed
93 void i2c_set_bus_speed(int speed)
95 if (speed >= I2C_MAX_SPEED)
96 set_speed(IC_SPEED_MODE_MAX);
97 else if (speed >= I2C_FAST_SPEED)
98 set_speed(IC_SPEED_MODE_FAST);
100 set_speed(IC_SPEED_MODE_STANDARD);
104 * i2c_get_bus_speed - Gets the i2c speed
106 * Gets the i2c speed.
108 int i2c_get_bus_speed(void)
112 cntl = (readl(&i2c_regs_p->ic_con) & IC_CON_SPD_MSK);
114 if (cntl == IC_CON_SPD_HS)
115 return I2C_MAX_SPEED;
116 else if (cntl == IC_CON_SPD_FS)
117 return I2C_FAST_SPEED;
118 else if (cntl == IC_CON_SPD_SS)
119 return I2C_STANDARD_SPEED;
125 * i2c_init - Init function
126 * @speed: required i2c speed
127 * @slaveadd: slave address for the device
129 * Initialization function.
131 void i2c_init(int speed, int slaveadd)
136 enbl = readl(&i2c_regs_p->ic_enable);
137 enbl &= ~IC_ENABLE_0B;
138 writel(enbl, &i2c_regs_p->ic_enable);
140 writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_regs_p->ic_con);
141 writel(IC_RX_TL, &i2c_regs_p->ic_rx_tl);
142 writel(IC_TX_TL, &i2c_regs_p->ic_tx_tl);
143 i2c_set_bus_speed(speed);
144 writel(IC_STOP_DET, &i2c_regs_p->ic_intr_mask);
145 writel(slaveadd, &i2c_regs_p->ic_sar);
148 enbl = readl(&i2c_regs_p->ic_enable);
149 enbl |= IC_ENABLE_0B;
150 writel(enbl, &i2c_regs_p->ic_enable);
154 * i2c_setaddress - Sets the target slave address
155 * @i2c_addr: target i2c address
157 * Sets the target slave address.
159 static void i2c_setaddress(unsigned int i2c_addr)
161 writel(i2c_addr, &i2c_regs_p->ic_tar);
165 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
167 * Flushes the i2c RX FIFO
169 static void i2c_flush_rxfifo(void)
171 while (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE)
172 readl(&i2c_regs_p->ic_cmd_data);
176 * i2c_wait_for_bb - Waits for bus busy
180 static int i2c_wait_for_bb(void)
182 unsigned long start_time_bb = get_timer(0);
184 while ((readl(&i2c_regs_p->ic_status) & IC_STATUS_MA) ||
185 !(readl(&i2c_regs_p->ic_status) & IC_STATUS_TFE)) {
187 /* Evaluate timeout */
188 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
195 /* check parameters for i2c_read and i2c_write */
196 static int check_params(uint addr, int alen, uchar *buffer, int len)
198 if (buffer == NULL) {
199 printf("Buffer is invalid\n");
204 printf("addr len %d not supported\n", alen);
208 if (addr + len > 256) {
209 printf("address out of range\n");
216 static int i2c_xfer_init(uchar chip, uint addr)
218 if (i2c_wait_for_bb()) {
219 printf("Timed out waiting for bus\n");
223 i2c_setaddress(chip);
224 writel(addr, &i2c_regs_p->ic_cmd_data);
229 static int i2c_xfer_finish(void)
231 ulong start_stop_det = get_timer(0);
234 if ((readl(&i2c_regs_p->ic_raw_intr_stat) & IC_STOP_DET)) {
235 readl(&i2c_regs_p->ic_clr_stop_det);
237 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
242 if (i2c_wait_for_bb()) {
243 printf("Timed out waiting for bus\n");
249 /* Wait for read/write operation to complete on actual memory */
256 * i2c_read - Read from i2c memory
257 * @chip: target i2c address
258 * @addr: address to read from
260 * @buffer: buffer for read data
261 * @len: no of bytes to be read
263 * Read from i2c memory.
265 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
267 unsigned long start_time_rx;
269 if (check_params(addr, alen, buffer, len))
272 if (i2c_xfer_init(chip, addr))
275 start_time_rx = get_timer(0);
277 writel(IC_CMD, &i2c_regs_p->ic_cmd_data);
279 if (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE) {
280 *buffer++ = (uchar)readl(&i2c_regs_p->ic_cmd_data);
282 start_time_rx = get_timer(0);
284 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
285 printf("Timed out. i2c read Failed\n");
290 return i2c_xfer_finish();
294 * i2c_write - Write to i2c memory
295 * @chip: target i2c address
296 * @addr: address to read from
298 * @buffer: buffer for read data
299 * @len: no of bytes to be read
301 * Write to i2c memory.
303 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
306 unsigned long start_time_tx;
308 if (check_params(addr, alen, buffer, len))
311 if (i2c_xfer_init(chip, addr))
314 start_time_tx = get_timer(0);
316 if (readl(&i2c_regs_p->ic_status) & IC_STATUS_TFNF) {
317 writel(*buffer, &i2c_regs_p->ic_cmd_data);
320 start_time_tx = get_timer(0);
322 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
323 printf("Timed out. i2c write Failed\n");
328 return i2c_xfer_finish();
332 * i2c_probe - Probe the i2c chip
334 int i2c_probe(uchar chip)
339 * Try to read the first location of the chip.
341 return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);