blob: 7ea66d4819fc841e7e2fd64b8049b592260f3134 [file] [log] [blame]
wdenk4a5c8a72003-03-06 00:02:04 +00001/*
2 * (C) Copyright 2000
3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4 *
5 * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Marius Groeger <mgroeger@sysgo.de>
7 *
8 * (C) Copyright 2003 Pengutronix e.K.
9 * Robert Schwebel <r.schwebel@pengutronix.de>
10 *
Lei Wena41374b42011-04-13 23:48:31 +053011 * (C) Copyright 2011 Marvell Inc.
12 * Lei Wen <leiwen@marvell.com>
13 *
wdenk4a5c8a72003-03-06 00:02:04 +000014 * 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 * Back ported to the 8xx platform (from the 8260 platform) by
33 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
34 */
35
wdenk4a5c8a72003-03-06 00:02:04 +000036#include <common.h>
Marek Vasut2db1e962010-09-09 09:50:39 +020037#include <asm/io.h>
wdenk4a5c8a72003-03-06 00:02:04 +000038
39#ifdef CONFIG_HARD_I2C
wdenk4a5c8a72003-03-06 00:02:04 +000040#include <i2c.h>
Lei Wena41374b42011-04-13 23:48:31 +053041#include "mv_i2c.h"
wdenk4a5c8a72003-03-06 00:02:04 +000042
43#ifdef DEBUG_I2C
44#define PRINTD(x) printf x
45#else
46#define PRINTD(x)
47#endif
48
wdenk4a5c8a72003-03-06 00:02:04 +000049/* All transfers are described by this data structure */
50struct i2c_msg {
51 u8 condition;
wdenk57b2d802003-06-27 21:31:46 +000052 u8 acknack;
53 u8 direction;
wdenk4a5c8a72003-03-06 00:02:04 +000054 u8 data;
55};
56
Lei Wena41374b42011-04-13 23:48:31 +053057struct mv_i2c {
58 u32 ibmr;
59 u32 pad0;
60 u32 idbr;
61 u32 pad1;
62 u32 icr;
63 u32 pad2;
64 u32 isr;
65 u32 pad3;
66 u32 isar;
67};
68
69static struct mv_i2c *base = (struct mv_i2c *)CONFIG_MV_I2C_REG;
70
Lei Wend3ae17b2011-04-13 23:48:16 +053071/*
Lei Wena41374b42011-04-13 23:48:31 +053072 * i2c_reset: - reset the host controller
wdenk4a5c8a72003-03-06 00:02:04 +000073 *
74 */
Lei Wend3ae17b2011-04-13 23:48:16 +053075static void i2c_reset(void)
wdenk4a5c8a72003-03-06 00:02:04 +000076{
Lei Wena41374b42011-04-13 23:48:31 +053077 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
78 writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */
wdenk57b2d802003-06-27 21:31:46 +000079 udelay(100);
Lei Wena41374b42011-04-13 23:48:31 +053080 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
81
82 i2c_clk_enable();
83
84 writel(CONFIG_SYS_I2C_SLAVE, &base->isar); /* set our slave address */
85 writel(I2C_ICR_INIT, &base->icr); /* set control reg values */
86 writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */
87 writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */
wdenk57b2d802003-06-27 21:31:46 +000088 udelay(100);
wdenk4a5c8a72003-03-06 00:02:04 +000089}
90
Lei Wend3ae17b2011-04-13 23:48:16 +053091/*
wdenk57b2d802003-06-27 21:31:46 +000092 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
wdenk4a5c8a72003-03-06 00:02:04 +000093 * are set and cleared
94 *
Markus Klotzbuecher7cf18be2006-03-24 12:23:27 +010095 * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
wdenk4a5c8a72003-03-06 00:02:04 +000096 */
Lei Wend3ae17b2011-04-13 23:48:16 +053097static int i2c_isr_set_cleared(unsigned long set_mask,
98 unsigned long cleared_mask)
wdenk4a5c8a72003-03-06 00:02:04 +000099{
Lei Wena41374b42011-04-13 23:48:31 +0530100 int timeout = 1000, isr;
wdenk4a5c8a72003-03-06 00:02:04 +0000101
Lei Wena41374b42011-04-13 23:48:31 +0530102 do {
103 isr = readl(&base->isr);
Lei Wend3ae17b2011-04-13 23:48:16 +0530104 udelay(10);
105 if (timeout-- < 0)
106 return 0;
Lei Wena41374b42011-04-13 23:48:31 +0530107 } while (((isr & set_mask) != set_mask)
108 || ((isr & cleared_mask) != 0));
wdenk4a5c8a72003-03-06 00:02:04 +0000109
wdenk57b2d802003-06-27 21:31:46 +0000110 return 1;
wdenk4a5c8a72003-03-06 00:02:04 +0000111}
112
Lei Wend3ae17b2011-04-13 23:48:16 +0530113/*
wdenk4a5c8a72003-03-06 00:02:04 +0000114 * i2c_transfer: - Transfer one byte over the i2c bus
115 *
wdenk57b2d802003-06-27 21:31:46 +0000116 * This function can tranfer a byte over the i2c bus in both directions.
117 * It is used by the public API functions.
wdenk4a5c8a72003-03-06 00:02:04 +0000118 *
119 * @return: 0: transfer successful
120 * -1: message is empty
121 * -2: transmit timeout
122 * -3: ACK missing
123 * -4: receive timeout
124 * -5: illegal parameters
125 * -6: bus is busy and couldn't be aquired
wdenk57b2d802003-06-27 21:31:46 +0000126 */
wdenk4a5c8a72003-03-06 00:02:04 +0000127int i2c_transfer(struct i2c_msg *msg)
128{
129 int ret;
130
wdenk57b2d802003-06-27 21:31:46 +0000131 if (!msg)
wdenk4a5c8a72003-03-06 00:02:04 +0000132 goto transfer_error_msg_empty;
133
Lei Wend3ae17b2011-04-13 23:48:16 +0530134 switch (msg->direction) {
wdenk4a5c8a72003-03-06 00:02:04 +0000135 case I2C_WRITE:
wdenk4a5c8a72003-03-06 00:02:04 +0000136 /* check if bus is not busy */
Lei Wend3ae17b2011-04-13 23:48:16 +0530137 if (!i2c_isr_set_cleared(0, ISR_IBB))
wdenk4a5c8a72003-03-06 00:02:04 +0000138 goto transfer_error_bus_busy;
139
140 /* start transmission */
Lei Wena41374b42011-04-13 23:48:31 +0530141 writel(readl(&base->icr) & ~ICR_START, &base->icr);
142 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
143 writel(msg->data, &base->idbr);
Marek Vasut2db1e962010-09-09 09:50:39 +0200144 if (msg->condition == I2C_COND_START)
Lei Wena41374b42011-04-13 23:48:31 +0530145 writel(readl(&base->icr) | ICR_START, &base->icr);
Marek Vasut2db1e962010-09-09 09:50:39 +0200146 if (msg->condition == I2C_COND_STOP)
Lei Wena41374b42011-04-13 23:48:31 +0530147 writel(readl(&base->icr) | ICR_STOP, &base->icr);
Marek Vasut2db1e962010-09-09 09:50:39 +0200148 if (msg->acknack == I2C_ACKNAK_SENDNAK)
Lei Wena41374b42011-04-13 23:48:31 +0530149 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
Marek Vasut2db1e962010-09-09 09:50:39 +0200150 if (msg->acknack == I2C_ACKNAK_SENDACK)
Lei Wena41374b42011-04-13 23:48:31 +0530151 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
152 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
153 writel(readl(&base->icr) | ICR_TB, &base->icr);
wdenk4a5c8a72003-03-06 00:02:04 +0000154
155 /* transmit register empty? */
Lei Wend3ae17b2011-04-13 23:48:16 +0530156 if (!i2c_isr_set_cleared(ISR_ITE, 0))
wdenk4a5c8a72003-03-06 00:02:04 +0000157 goto transfer_error_transmit_timeout;
158
159 /* clear 'transmit empty' state */
Lei Wena41374b42011-04-13 23:48:31 +0530160 writel(readl(&base->isr) | ISR_ITE, &base->isr);
wdenk4a5c8a72003-03-06 00:02:04 +0000161
162 /* wait for ACK from slave */
163 if (msg->acknack == I2C_ACKNAK_WAITACK)
Lei Wend3ae17b2011-04-13 23:48:16 +0530164 if (!i2c_isr_set_cleared(0, ISR_ACKNAK))
wdenk4a5c8a72003-03-06 00:02:04 +0000165 goto transfer_error_ack_missing;
166 break;
167
168 case I2C_READ:
169
170 /* check if bus is not busy */
Lei Wend3ae17b2011-04-13 23:48:16 +0530171 if (!i2c_isr_set_cleared(0, ISR_IBB))
wdenk4a5c8a72003-03-06 00:02:04 +0000172 goto transfer_error_bus_busy;
173
174 /* start receive */
Lei Wena41374b42011-04-13 23:48:31 +0530175 writel(readl(&base->icr) & ~ICR_START, &base->icr);
176 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
Marek Vasut2db1e962010-09-09 09:50:39 +0200177 if (msg->condition == I2C_COND_START)
Lei Wena41374b42011-04-13 23:48:31 +0530178 writel(readl(&base->icr) | ICR_START, &base->icr);
Marek Vasut2db1e962010-09-09 09:50:39 +0200179 if (msg->condition == I2C_COND_STOP)
Lei Wena41374b42011-04-13 23:48:31 +0530180 writel(readl(&base->icr) | ICR_STOP, &base->icr);
Marek Vasut2db1e962010-09-09 09:50:39 +0200181 if (msg->acknack == I2C_ACKNAK_SENDNAK)
Lei Wena41374b42011-04-13 23:48:31 +0530182 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
Marek Vasut2db1e962010-09-09 09:50:39 +0200183 if (msg->acknack == I2C_ACKNAK_SENDACK)
Lei Wena41374b42011-04-13 23:48:31 +0530184 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
185 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
186 writel(readl(&base->icr) | ICR_TB, &base->icr);
wdenk4a5c8a72003-03-06 00:02:04 +0000187
188 /* receive register full? */
Lei Wend3ae17b2011-04-13 23:48:16 +0530189 if (!i2c_isr_set_cleared(ISR_IRF, 0))
wdenk57b2d802003-06-27 21:31:46 +0000190 goto transfer_error_receive_timeout;
wdenk4a5c8a72003-03-06 00:02:04 +0000191
Lei Wena41374b42011-04-13 23:48:31 +0530192 msg->data = readl(&base->idbr);
wdenk4a5c8a72003-03-06 00:02:04 +0000193
194 /* clear 'receive empty' state */
Lei Wena41374b42011-04-13 23:48:31 +0530195 writel(readl(&base->isr) | ISR_IRF, &base->isr);
wdenk4a5c8a72003-03-06 00:02:04 +0000196 break;
wdenk4a5c8a72003-03-06 00:02:04 +0000197 default:
wdenk4a5c8a72003-03-06 00:02:04 +0000198 goto transfer_error_illegal_param;
wdenk4a5c8a72003-03-06 00:02:04 +0000199 }
200
wdenk57b2d802003-06-27 21:31:46 +0000201 return 0;
wdenk4a5c8a72003-03-06 00:02:04 +0000202
wdenk57b2d802003-06-27 21:31:46 +0000203transfer_error_msg_empty:
wdenk4a5c8a72003-03-06 00:02:04 +0000204 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
205 ret = -1; goto i2c_transfer_finish;
206
207transfer_error_transmit_timeout:
208 PRINTD(("i2c_transfer: error: transmit timeout\n"));
209 ret = -2; goto i2c_transfer_finish;
210
211transfer_error_ack_missing:
212 PRINTD(("i2c_transfer: error: ACK missing\n"));
213 ret = -3; goto i2c_transfer_finish;
214
215transfer_error_receive_timeout:
216 PRINTD(("i2c_transfer: error: receive timeout\n"));
217 ret = -4; goto i2c_transfer_finish;
218
219transfer_error_illegal_param:
220 PRINTD(("i2c_transfer: error: illegal parameters\n"));
221 ret = -5; goto i2c_transfer_finish;
222
223transfer_error_bus_busy:
224 PRINTD(("i2c_transfer: error: bus is busy\n"));
225 ret = -6; goto i2c_transfer_finish;
226
227i2c_transfer_finish:
Lei Wend3ae17b2011-04-13 23:48:16 +0530228 PRINTD(("i2c_transfer: ISR: 0x%04x\n", ISR));
wdenk4a5c8a72003-03-06 00:02:04 +0000229 i2c_reset();
230 return ret;
wdenk4a5c8a72003-03-06 00:02:04 +0000231}
232
233/* ------------------------------------------------------------------------ */
234/* API Functions */
235/* ------------------------------------------------------------------------ */
wdenk4a5c8a72003-03-06 00:02:04 +0000236void i2c_init(int speed, int slaveaddr)
237{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200238#ifdef CONFIG_SYS_I2C_INIT_BOARD
Lei Wena41374b42011-04-13 23:48:31 +0530239 u32 icr;
240 /*
241 * call board specific i2c bus reset routine before accessing the
242 * environment, which might be in a chip on that bus. For details
243 * about this problem see doc/I2C_Edge_Conditions.
244 *
245 * disable I2C controller first, otherwhise it thinks we want to
246 * talk to the slave port...
247 */
248 icr = readl(&base->icr);
249 writel(readl(&base->icr) & ~(ICR_SCLE | ICR_IUE), &base->icr);
250
wdenkcc1e2562003-03-06 13:39:27 +0000251 i2c_init_board();
Lei Wena41374b42011-04-13 23:48:31 +0530252
253 writel(icr, &base->icr);
wdenkcc1e2562003-03-06 13:39:27 +0000254#endif
wdenk4a5c8a72003-03-06 00:02:04 +0000255}
256
Lei Wend3ae17b2011-04-13 23:48:16 +0530257/*
wdenk4a5c8a72003-03-06 00:02:04 +0000258 * i2c_probe: - Test if a chip answers for a given i2c address
259 *
wdenk57b2d802003-06-27 21:31:46 +0000260 * @chip: address of the chip which is searched for
Wolfgang Denka1be4762008-05-20 16:00:29 +0200261 * @return: 0 if a chip was found, -1 otherwhise
wdenk4a5c8a72003-03-06 00:02:04 +0000262 */
wdenk4a5c8a72003-03-06 00:02:04 +0000263int i2c_probe(uchar chip)
264{
265 struct i2c_msg msg;
266
267 i2c_reset();
268
269 msg.condition = I2C_COND_START;
270 msg.acknack = I2C_ACKNAK_WAITACK;
271 msg.direction = I2C_WRITE;
272 msg.data = (chip << 1) + 1;
Lei Wend3ae17b2011-04-13 23:48:16 +0530273 if (i2c_transfer(&msg))
274 return -1;
wdenk4a5c8a72003-03-06 00:02:04 +0000275
276 msg.condition = I2C_COND_STOP;
277 msg.acknack = I2C_ACKNAK_SENDNAK;
278 msg.direction = I2C_READ;
279 msg.data = 0x00;
Lei Wend3ae17b2011-04-13 23:48:16 +0530280 if (i2c_transfer(&msg))
281 return -1;
wdenk4a5c8a72003-03-06 00:02:04 +0000282
283 return 0;
284}
285
Lei Wend3ae17b2011-04-13 23:48:16 +0530286/*
wdenk4a5c8a72003-03-06 00:02:04 +0000287 * i2c_read: - Read multiple bytes from an i2c device
288 *
289 * The higher level routines take into account that this function is only
wdenk57b2d802003-06-27 21:31:46 +0000290 * called with len < page length of the device (see configuration file)
wdenk4a5c8a72003-03-06 00:02:04 +0000291 *
292 * @chip: address of the chip which is to be read
293 * @addr: i2c data address within the chip
294 * @alen: length of the i2c data address (1..2 bytes)
295 * @buffer: where to write the data
296 * @len: how much byte do we want to read
297 * @return: 0 in case of success
298 */
wdenk4a5c8a72003-03-06 00:02:04 +0000299int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
300{
301 struct i2c_msg msg;
302 u8 addr_bytes[3]; /* lowest...highest byte of data address */
wdenk4a5c8a72003-03-06 00:02:04 +0000303
Lei Wend3ae17b2011-04-13 23:48:16 +0530304 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
305 "len=0x%02x)\n", chip, addr, alen, len));
wdenk4a5c8a72003-03-06 00:02:04 +0000306
307 i2c_reset();
308
309 /* dummy chip address write */
310 PRINTD(("i2c_read: dummy chip address write\n"));
311 msg.condition = I2C_COND_START;
312 msg.acknack = I2C_ACKNAK_WAITACK;
313 msg.direction = I2C_WRITE;
Lei Wend3ae17b2011-04-13 23:48:16 +0530314 msg.data = (chip << 1);
315 msg.data &= 0xFE;
316 if (i2c_transfer(&msg))
317 return -1;
wdenk57b2d802003-06-27 21:31:46 +0000318
wdenk4a5c8a72003-03-06 00:02:04 +0000319 /*
wdenk57b2d802003-06-27 21:31:46 +0000320 * send memory address bytes;
321 * alen defines how much bytes we have to send.
wdenk4a5c8a72003-03-06 00:02:04 +0000322 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200323 /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */
wdenk4a5c8a72003-03-06 00:02:04 +0000324 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
325 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
326 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
327
328 while (--alen >= 0) {
Lei Wend3ae17b2011-04-13 23:48:16 +0530329 PRINTD(("i2c_read: send memory word address byte %1d\n", alen));
wdenk4a5c8a72003-03-06 00:02:04 +0000330 msg.condition = I2C_COND_NORMAL;
331 msg.acknack = I2C_ACKNAK_WAITACK;
332 msg.direction = I2C_WRITE;
333 msg.data = addr_bytes[alen];
Lei Wend3ae17b2011-04-13 23:48:16 +0530334 if (i2c_transfer(&msg))
335 return -1;
wdenk4a5c8a72003-03-06 00:02:04 +0000336 }
wdenk57b2d802003-06-27 21:31:46 +0000337
wdenk4a5c8a72003-03-06 00:02:04 +0000338 /* start read sequence */
339 PRINTD(("i2c_read: start read sequence\n"));
340 msg.condition = I2C_COND_START;
341 msg.acknack = I2C_ACKNAK_WAITACK;
342 msg.direction = I2C_WRITE;
343 msg.data = (chip << 1);
344 msg.data |= 0x01;
Lei Wend3ae17b2011-04-13 23:48:16 +0530345 if (i2c_transfer(&msg))
346 return -1;
wdenk4a5c8a72003-03-06 00:02:04 +0000347
348 /* read bytes; send NACK at last byte */
349 while (len--) {
Lei Wend3ae17b2011-04-13 23:48:16 +0530350 if (len == 0) {
wdenk4a5c8a72003-03-06 00:02:04 +0000351 msg.condition = I2C_COND_STOP;
352 msg.acknack = I2C_ACKNAK_SENDNAK;
353 } else {
354 msg.condition = I2C_COND_NORMAL;
355 msg.acknack = I2C_ACKNAK_SENDACK;
356 }
357
358 msg.direction = I2C_READ;
359 msg.data = 0x00;
Lei Wend3ae17b2011-04-13 23:48:16 +0530360 if (i2c_transfer(&msg))
361 return -1;
wdenk4a5c8a72003-03-06 00:02:04 +0000362
Markus Klotzbuecher7cf18be2006-03-24 12:23:27 +0100363 *buffer = msg.data;
Lei Wend3ae17b2011-04-13 23:48:16 +0530364 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",
365 (unsigned int)buffer, *buffer));
Markus Klotzbuecher7cf18be2006-03-24 12:23:27 +0100366 buffer++;
wdenk4a5c8a72003-03-06 00:02:04 +0000367 }
368
369 i2c_reset();
370
371 return 0;
372}
373
Lei Wend3ae17b2011-04-13 23:48:16 +0530374/*
wdenk4a5c8a72003-03-06 00:02:04 +0000375 * i2c_write: - Write multiple bytes to an i2c device
376 *
377 * The higher level routines take into account that this function is only
wdenk57b2d802003-06-27 21:31:46 +0000378 * called with len < page length of the device (see configuration file)
wdenk4a5c8a72003-03-06 00:02:04 +0000379 *
380 * @chip: address of the chip which is to be written
381 * @addr: i2c data address within the chip
382 * @alen: length of the i2c data address (1..2 bytes)
wdenk57b2d802003-06-27 21:31:46 +0000383 * @buffer: where to find the data to be written
wdenk4a5c8a72003-03-06 00:02:04 +0000384 * @len: how much byte do we want to read
385 * @return: 0 in case of success
386 */
wdenk4a5c8a72003-03-06 00:02:04 +0000387int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
388{
389 struct i2c_msg msg;
390 u8 addr_bytes[3]; /* lowest...highest byte of data address */
391
Lei Wend3ae17b2011-04-13 23:48:16 +0530392 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
393 "len=0x%02x)\n", chip, addr, alen, len));
wdenk4a5c8a72003-03-06 00:02:04 +0000394
395 i2c_reset();
396
397 /* chip address write */
398 PRINTD(("i2c_write: chip address write\n"));
399 msg.condition = I2C_COND_START;
400 msg.acknack = I2C_ACKNAK_WAITACK;
401 msg.direction = I2C_WRITE;
Lei Wend3ae17b2011-04-13 23:48:16 +0530402 msg.data = (chip << 1);
403 msg.data &= 0xFE;
404 if (i2c_transfer(&msg))
405 return -1;
wdenk57b2d802003-06-27 21:31:46 +0000406
wdenk4a5c8a72003-03-06 00:02:04 +0000407 /*
wdenk57b2d802003-06-27 21:31:46 +0000408 * send memory address bytes;
409 * alen defines how much bytes we have to send.
wdenk4a5c8a72003-03-06 00:02:04 +0000410 */
411 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
412 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
413 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
414
415 while (--alen >= 0) {
wdenk4a5c8a72003-03-06 00:02:04 +0000416 PRINTD(("i2c_write: send memory word address\n"));
417 msg.condition = I2C_COND_NORMAL;
418 msg.acknack = I2C_ACKNAK_WAITACK;
419 msg.direction = I2C_WRITE;
420 msg.data = addr_bytes[alen];
Lei Wend3ae17b2011-04-13 23:48:16 +0530421 if (i2c_transfer(&msg))
422 return -1;
wdenk4a5c8a72003-03-06 00:02:04 +0000423 }
wdenk57b2d802003-06-27 21:31:46 +0000424
wdenk4a5c8a72003-03-06 00:02:04 +0000425 /* write bytes; send NACK at last byte */
426 while (len--) {
Lei Wend3ae17b2011-04-13 23:48:16 +0530427 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",
428 (unsigned int)buffer, *buffer));
wdenk4a5c8a72003-03-06 00:02:04 +0000429
Lei Wend3ae17b2011-04-13 23:48:16 +0530430 if (len == 0)
wdenk4a5c8a72003-03-06 00:02:04 +0000431 msg.condition = I2C_COND_STOP;
432 else
433 msg.condition = I2C_COND_NORMAL;
434
435 msg.acknack = I2C_ACKNAK_WAITACK;
436 msg.direction = I2C_WRITE;
437 msg.data = *(buffer++);
wdenk57b2d802003-06-27 21:31:46 +0000438
Lei Wend3ae17b2011-04-13 23:48:16 +0530439 if (i2c_transfer(&msg))
440 return -1;
wdenk4a5c8a72003-03-06 00:02:04 +0000441 }
442
443 i2c_reset();
444
445 return 0;
wdenk4a5c8a72003-03-06 00:02:04 +0000446}
wdenk4a5c8a72003-03-06 00:02:04 +0000447#endif /* CONFIG_HARD_I2C */