blob: c88ac7cf98b318e5c6706c1dbcd6febf4bce1d94 [file] [log] [blame]
Sascha Hauer15ea70f2008-03-26 20:40:49 +01001/*
Marek Vasut94cb8422011-09-22 09:22:12 +00002 * i2c driver for Freescale i.MX series
Sascha Hauer15ea70f2008-03-26 20:40:49 +01003 *
4 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Marek Vasut94cb8422011-09-22 09:22:12 +00005 * (c) 2011 Marek Vasut <marek.vasut@gmail.com>
6 *
7 * Based on i2c-imx.c from linux kernel:
8 * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de>
9 * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de>
10 * Copyright (C) 2007 RightHand Technologies, Inc.
11 * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
12 *
Sascha Hauer15ea70f2008-03-26 20:40:49 +010013 *
14 * See file CREDITS for list of people who contributed to this
15 * project.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * MA 02111-1307 USA
31 */
32
33#include <common.h>
Stefano Babic22121722011-01-20 07:50:44 +000034#include <asm/io.h>
Sascha Hauer15ea70f2008-03-26 20:40:49 +010035
Michal Simek76b8a132008-07-14 19:45:35 +020036#if defined(CONFIG_HARD_I2C)
Sascha Hauer15ea70f2008-03-26 20:40:49 +010037
Liu Hui-R64343447beb12011-01-03 22:27:39 +000038#include <asm/arch/clock.h>
Stefano Babic78129d92011-03-14 15:43:56 +010039#include <asm/arch/imx-regs.h>
Marek Vasut5f1291e2011-10-26 00:05:44 +000040#include <i2c.h>
Sascha Hauer15ea70f2008-03-26 20:40:49 +010041
Marek Vasut94cb8422011-09-22 09:22:12 +000042struct mxc_i2c_regs {
43 uint32_t iadr;
44 uint32_t ifdr;
45 uint32_t i2cr;
46 uint32_t i2sr;
47 uint32_t i2dr;
48};
Sascha Hauer15ea70f2008-03-26 20:40:49 +010049
50#define I2CR_IEN (1 << 7)
51#define I2CR_IIEN (1 << 6)
52#define I2CR_MSTA (1 << 5)
53#define I2CR_MTX (1 << 4)
54#define I2CR_TX_NO_AK (1 << 3)
55#define I2CR_RSTA (1 << 2)
56
57#define I2SR_ICF (1 << 7)
58#define I2SR_IBB (1 << 5)
59#define I2SR_IIF (1 << 1)
60#define I2SR_RX_NO_AK (1 << 0)
61
Liu Hui-R64343447beb12011-01-03 22:27:39 +000062#if defined(CONFIG_SYS_I2C_MX31_PORT1)
Sascha Hauer15ea70f2008-03-26 20:40:49 +010063#define I2C_BASE 0x43f80000
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +010064#define I2C_CLK_OFFSET 26
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020065#elif defined (CONFIG_SYS_I2C_MX31_PORT2)
Sascha Hauer15ea70f2008-03-26 20:40:49 +010066#define I2C_BASE 0x43f98000
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +010067#define I2C_CLK_OFFSET 28
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020068#elif defined (CONFIG_SYS_I2C_MX31_PORT3)
Sascha Hauer15ea70f2008-03-26 20:40:49 +010069#define I2C_BASE 0x43f84000
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +010070#define I2C_CLK_OFFSET 30
Liu Hui-R64343447beb12011-01-03 22:27:39 +000071#elif defined(CONFIG_SYS_I2C_MX53_PORT1)
72#define I2C_BASE I2C1_BASE_ADDR
73#elif defined(CONFIG_SYS_I2C_MX53_PORT2)
74#define I2C_BASE I2C2_BASE_ADDR
Stefano Babicbb16a5b2011-01-19 22:46:26 +000075#elif defined(CONFIG_SYS_I2C_MX35_PORT1)
76#define I2C_BASE I2C_BASE_ADDR
Stefano Babic32079d62011-08-05 12:19:08 +020077#elif defined(CONFIG_SYS_I2C_MX35_PORT2)
78#define I2C_BASE I2C2_BASE_ADDR
79#elif defined(CONFIG_SYS_I2C_MX35_PORT3)
80#define I2C_BASE I2C3_BASE_ADDR
Sascha Hauer15ea70f2008-03-26 20:40:49 +010081#else
Stefano Babicbb16a5b2011-01-19 22:46:26 +000082#error "define CONFIG_SYS_I2C_MX<Processor>_PORTx to use the mx I2C driver"
Sascha Hauer15ea70f2008-03-26 20:40:49 +010083#endif
84
Stefano Babic848bb992011-01-20 07:51:31 +000085#define I2C_MAX_TIMEOUT 10000
Sascha Hauer15ea70f2008-03-26 20:40:49 +010086
Marek Vasut94cb8422011-09-22 09:22:12 +000087static u16 i2c_clk_div[50][2] = {
88 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
89 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
90 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
91 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
92 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
93 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
94 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
95 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
96 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
97 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
98 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
99 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
100 { 3072, 0x1E }, { 3840, 0x1F }
101};
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100102
Marek Vasut94cb8422011-09-22 09:22:12 +0000103/*
104 * Calculate and set proper clock divider
105 */
Marek Vasut5f1291e2011-10-26 00:05:44 +0000106static uint8_t i2c_imx_get_clk(unsigned int rate)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100107{
Marek Vasut94cb8422011-09-22 09:22:12 +0000108 unsigned int i2c_clk_rate;
109 unsigned int div;
Marek Vasut5f1291e2011-10-26 00:05:44 +0000110 u8 clk_div;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100111
Liu Hui-R64343447beb12011-01-03 22:27:39 +0000112#if defined(CONFIG_MX31)
Stefano Babic22121722011-01-20 07:50:44 +0000113 struct clock_control_regs *sc_regs =
114 (struct clock_control_regs *)CCM_BASE;
Marek Vasut94cb8422011-09-22 09:22:12 +0000115
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +0100116 /* start the required I2C clock */
Stefano Babic22121722011-01-20 07:50:44 +0000117 writel(readl(&sc_regs->cgr0) | (3 << I2C_CLK_OFFSET),
118 &sc_regs->cgr0);
Liu Hui-R64343447beb12011-01-03 22:27:39 +0000119#endif
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +0100120
Marek Vasut94cb8422011-09-22 09:22:12 +0000121 /* Divider value calculation */
122 i2c_clk_rate = mxc_get_clock(MXC_IPG_PERCLK);
123 div = (i2c_clk_rate + rate - 1) / rate;
124 if (div < i2c_clk_div[0][0])
Marek Vasut4f274442011-09-27 06:34:11 +0000125 clk_div = 0;
Marek Vasut94cb8422011-09-22 09:22:12 +0000126 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
Marek Vasut4f274442011-09-27 06:34:11 +0000127 clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
Marek Vasut94cb8422011-09-22 09:22:12 +0000128 else
Marek Vasut4f274442011-09-27 06:34:11 +0000129 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
Marek Vasut94cb8422011-09-22 09:22:12 +0000130 ;
131
132 /* Store divider value */
Marek Vasut5f1291e2011-10-26 00:05:44 +0000133 return clk_div;
Marek Vasut94cb8422011-09-22 09:22:12 +0000134}
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100135
Marek Vasut94cb8422011-09-22 09:22:12 +0000136/*
137 * Reset I2C Controller
138 */
139void i2c_reset(void)
140{
141 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100142
Marek Vasut94cb8422011-09-22 09:22:12 +0000143 writeb(0, &i2c_regs->i2cr); /* Reset module */
144 writeb(0, &i2c_regs->i2sr);
145}
146
147/*
148 * Init I2C Bus
149 */
150void i2c_init(int speed, int unused)
151{
Marek Vasut5f1291e2011-10-26 00:05:44 +0000152 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
153 u8 clk_idx = i2c_imx_get_clk(speed);
154 u8 idx = i2c_clk_div[clk_idx][1];
155
156 /* Store divider value */
157 writeb(idx, &i2c_regs->ifdr);
158
Stefano Babic22121722011-01-20 07:50:44 +0000159 i2c_reset();
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100160}
161
Marek Vasut94cb8422011-09-22 09:22:12 +0000162/*
Marek Vasut4f274442011-09-27 06:34:11 +0000163 * Set I2C Speed
164 */
165int i2c_set_bus_speed(unsigned int speed)
166{
167 i2c_init(speed, 0);
168 return 0;
169}
170
171/*
172 * Get I2C Speed
173 */
174unsigned int i2c_get_bus_speed(void)
175{
Marek Vasut5f1291e2011-10-26 00:05:44 +0000176 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
177 u8 clk_idx = readb(&i2c_regs->ifdr);
178 u8 clk_div;
179
180 for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++)
181 ;
182
Marek Vasut4f274442011-09-27 06:34:11 +0000183 return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0];
184}
185
186/*
Marek Vasut94cb8422011-09-22 09:22:12 +0000187 * Wait for bus to be busy (or free if for_busy = 0)
188 *
189 * for_busy = 1: Wait for IBB to be asserted
190 * for_busy = 0: Wait for IBB to be de-asserted
191 */
192int i2c_imx_bus_busy(int for_busy)
Stefano Babic848bb992011-01-20 07:51:31 +0000193{
Marek Vasut94cb8422011-09-22 09:22:12 +0000194 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
195 unsigned int temp;
196
Stefano Babic848bb992011-01-20 07:51:31 +0000197 int timeout = I2C_MAX_TIMEOUT;
198
Marek Vasut94cb8422011-09-22 09:22:12 +0000199 while (timeout--) {
200 temp = readb(&i2c_regs->i2sr);
201
202 if (for_busy && (temp & I2SR_IBB))
203 return 0;
204 if (!for_busy && !(temp & I2SR_IBB))
205 return 0;
206
Stefano Babic848bb992011-01-20 07:51:31 +0000207 udelay(1);
208 }
Marek Vasut94cb8422011-09-22 09:22:12 +0000209
210 return 1;
Stefano Babic848bb992011-01-20 07:51:31 +0000211}
212
Marek Vasut94cb8422011-09-22 09:22:12 +0000213/*
214 * Wait for transaction to complete
215 */
216int i2c_imx_trx_complete(void)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100217{
Marek Vasut94cb8422011-09-22 09:22:12 +0000218 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Stefano Babic848bb992011-01-20 07:51:31 +0000219 int timeout = I2C_MAX_TIMEOUT;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100220
Marek Vasut94cb8422011-09-22 09:22:12 +0000221 while (timeout--) {
222 if (readb(&i2c_regs->i2sr) & I2SR_IIF) {
223 writeb(0, &i2c_regs->i2sr);
224 return 0;
225 }
226
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100227 udelay(1);
Marek Vasut94cb8422011-09-22 09:22:12 +0000228 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100229
Marek Vasut94cb8422011-09-22 09:22:12 +0000230 return 1;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100231}
232
Marek Vasut94cb8422011-09-22 09:22:12 +0000233/*
234 * Check if the transaction was ACKed
235 */
236int i2c_imx_acked(void)
Stefano Babic848bb992011-01-20 07:51:31 +0000237{
Marek Vasut94cb8422011-09-22 09:22:12 +0000238 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Stefano Babic848bb992011-01-20 07:51:31 +0000239
Marek Vasut94cb8422011-09-22 09:22:12 +0000240 return readb(&i2c_regs->i2sr) & I2SR_RX_NO_AK;
241}
Stefano Babic848bb992011-01-20 07:51:31 +0000242
Marek Vasut94cb8422011-09-22 09:22:12 +0000243/*
244 * Start the controller
245 */
246int i2c_imx_start(void)
247{
248 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
249 unsigned int temp = 0;
250 int result;
Marek Vasut5f1291e2011-10-26 00:05:44 +0000251 int speed = i2c_get_bus_speed();
252 u8 clk_idx = i2c_imx_get_clk(speed);
253 u8 idx = i2c_clk_div[clk_idx][1];
Stefano Babic848bb992011-01-20 07:51:31 +0000254
Marek Vasut5f1291e2011-10-26 00:05:44 +0000255 /* Store divider value */
256 writeb(idx, &i2c_regs->ifdr);
Stefano Babic848bb992011-01-20 07:51:31 +0000257
Marek Vasut94cb8422011-09-22 09:22:12 +0000258 /* Enable I2C controller */
259 writeb(0, &i2c_regs->i2sr);
260 writeb(I2CR_IEN, &i2c_regs->i2cr);
Stefano Babic848bb992011-01-20 07:51:31 +0000261
Marek Vasut94cb8422011-09-22 09:22:12 +0000262 /* Wait controller to be stable */
263 udelay(50);
264
265 /* Start I2C transaction */
266 temp = readb(&i2c_regs->i2cr);
267 temp |= I2CR_MSTA;
268 writeb(temp, &i2c_regs->i2cr);
269
270 result = i2c_imx_bus_busy(1);
271 if (result)
272 return result;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100273
Marek Vasut94cb8422011-09-22 09:22:12 +0000274 temp |= I2CR_MTX | I2CR_TX_NO_AK;
275 writeb(temp, &i2c_regs->i2cr);
276
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100277 return 0;
278}
279
Marek Vasut94cb8422011-09-22 09:22:12 +0000280/*
281 * Stop the controller
282 */
283void i2c_imx_stop(void)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100284{
Marek Vasut94cb8422011-09-22 09:22:12 +0000285 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
286 unsigned int temp = 0;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100287
Marek Vasut94cb8422011-09-22 09:22:12 +0000288 /* Stop I2C transaction */
289 temp = readb(&i2c_regs->i2cr);
290 temp |= ~(I2CR_MSTA | I2CR_MTX);
291 writeb(temp, &i2c_regs->i2cr);
Stefano Babic848bb992011-01-20 07:51:31 +0000292
Marek Vasut94cb8422011-09-22 09:22:12 +0000293 i2c_imx_bus_busy(0);
294
295 /* Disable I2C controller */
296 writeb(0, &i2c_regs->i2cr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100297}
298
Marek Vasut94cb8422011-09-22 09:22:12 +0000299/*
300 * Set chip address and access mode
301 *
302 * read = 1: READ access
303 * read = 0: WRITE access
304 */
305int i2c_imx_set_chip_addr(uchar chip, int read)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100306{
Marek Vasut94cb8422011-09-22 09:22:12 +0000307 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100308 int ret;
309
Marek Vasut94cb8422011-09-22 09:22:12 +0000310 writeb((chip << 1) | read, &i2c_regs->i2dr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100311
Marek Vasut94cb8422011-09-22 09:22:12 +0000312 ret = i2c_imx_trx_complete();
313 if (ret)
314 return ret;
315
316 ret = i2c_imx_acked();
317 if (ret)
318 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100319
320 return ret;
321}
322
Marek Vasut94cb8422011-09-22 09:22:12 +0000323/*
324 * Write register address
325 */
326int i2c_imx_set_reg_addr(uint addr, int alen)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100327{
Marek Vasut94cb8422011-09-22 09:22:12 +0000328 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Marek Vasut5f1291e2011-10-26 00:05:44 +0000329 int ret = 0;
Marek Vasut94cb8422011-09-22 09:22:12 +0000330
Marek Vasut5f1291e2011-10-26 00:05:44 +0000331 while (alen--) {
332 writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->i2dr);
Marek Vasut94cb8422011-09-22 09:22:12 +0000333
334 ret = i2c_imx_trx_complete();
335 if (ret)
Stefano Babic848bb992011-01-20 07:51:31 +0000336 break;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100337
Marek Vasut94cb8422011-09-22 09:22:12 +0000338 ret = i2c_imx_acked();
339 if (ret)
340 break;
Stefano Babic848bb992011-01-20 07:51:31 +0000341 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100342
Marek Vasut94cb8422011-09-22 09:22:12 +0000343 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100344}
345
Marek Vasut94cb8422011-09-22 09:22:12 +0000346/*
347 * Try if a chip add given address responds (probe the chip)
348 */
349int i2c_probe(uchar chip)
350{
351 int ret;
352
353 ret = i2c_imx_start();
354 if (ret)
355 return ret;
356
357 ret = i2c_imx_set_chip_addr(chip, 0);
358 if (ret)
359 return ret;
360
361 i2c_imx_stop();
362
363 return ret;
364}
365
366/*
367 * Read data from I2C device
368 */
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100369int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
370{
Marek Vasut94cb8422011-09-22 09:22:12 +0000371 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100372 int ret;
Marek Vasut94cb8422011-09-22 09:22:12 +0000373 unsigned int temp;
374 int i;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100375
Marek Vasut94cb8422011-09-22 09:22:12 +0000376 ret = i2c_imx_start();
377 if (ret)
378 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100379
Marek Vasut94cb8422011-09-22 09:22:12 +0000380 /* write slave address */
381 ret = i2c_imx_set_chip_addr(chip, 0);
382 if (ret)
383 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100384
Marek Vasut94cb8422011-09-22 09:22:12 +0000385 ret = i2c_imx_set_reg_addr(addr, alen);
386 if (ret)
387 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100388
Marek Vasut94cb8422011-09-22 09:22:12 +0000389 temp = readb(&i2c_regs->i2cr);
390 temp |= I2CR_RSTA;
391 writeb(temp, &i2c_regs->i2cr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100392
Marek Vasut94cb8422011-09-22 09:22:12 +0000393 ret = i2c_imx_set_chip_addr(chip, 1);
394 if (ret)
395 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100396
Marek Vasut94cb8422011-09-22 09:22:12 +0000397 /* setup bus to read data */
398 temp = readb(&i2c_regs->i2cr);
399 temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
400 if (len == 1)
401 temp |= I2CR_TX_NO_AK;
402 writeb(temp, &i2c_regs->i2cr);
403 readb(&i2c_regs->i2dr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100404
Marek Vasut94cb8422011-09-22 09:22:12 +0000405 /* read data */
406 for (i = 0; i < len; i++) {
407 ret = i2c_imx_trx_complete();
408 if (ret)
409 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100410
Marek Vasut94cb8422011-09-22 09:22:12 +0000411 /*
412 * It must generate STOP before read I2DR to prevent
413 * controller from generating another clock cycle
414 */
415 if (i == (len - 1)) {
416 temp = readb(&i2c_regs->i2cr);
417 temp &= ~(I2CR_MSTA | I2CR_MTX);
418 writeb(temp, &i2c_regs->i2cr);
419 i2c_imx_bus_busy(0);
420 } else if (i == (len - 2)) {
421 temp = readb(&i2c_regs->i2cr);
422 temp |= I2CR_TX_NO_AK;
423 writeb(temp, &i2c_regs->i2cr);
424 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100425
Marek Vasut94cb8422011-09-22 09:22:12 +0000426 buf[i] = readb(&i2c_regs->i2dr);
427 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100428
Marek Vasut94cb8422011-09-22 09:22:12 +0000429 i2c_imx_stop();
430
431 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100432}
433
Marek Vasut94cb8422011-09-22 09:22:12 +0000434/*
435 * Write data to I2C device
436 */
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100437int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
438{
Marek Vasut94cb8422011-09-22 09:22:12 +0000439 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
440 int ret;
441 int i;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100442
Marek Vasut94cb8422011-09-22 09:22:12 +0000443 ret = i2c_imx_start();
444 if (ret)
445 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100446
Marek Vasut94cb8422011-09-22 09:22:12 +0000447 /* write slave address */
448 ret = i2c_imx_set_chip_addr(chip, 0);
449 if (ret)
450 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100451
Marek Vasut94cb8422011-09-22 09:22:12 +0000452 ret = i2c_imx_set_reg_addr(addr, alen);
453 if (ret)
454 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100455
Marek Vasut94cb8422011-09-22 09:22:12 +0000456 for (i = 0; i < len; i++) {
457 writeb(buf[i], &i2c_regs->i2dr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100458
Marek Vasut94cb8422011-09-22 09:22:12 +0000459 ret = i2c_imx_trx_complete();
460 if (ret)
461 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100462
Marek Vasut94cb8422011-09-22 09:22:12 +0000463 ret = i2c_imx_acked();
464 if (ret)
465 return ret;
466 }
467
468 i2c_imx_stop();
469
470 return ret;
471}
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100472#endif /* CONFIG_HARD_I2C */