blob: 8eb30e7082e9e2b682efd9b960376bd3bf859d0c [file] [log] [blame]
wdenk43d96162003-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 Wen3df619e2011-04-13 23:48:31 +053011 * (C) Copyright 2011 Marvell Inc.
12 * Lei Wen <leiwen@marvell.com>
13 *
wdenk43d96162003-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
wdenk43d96162003-03-06 00:02:04 +000036#include <common.h>
Marek Vasut3ba8bf72010-09-09 09:50:39 +020037#include <asm/io.h>
wdenk43d96162003-03-06 00:02:04 +000038
39#ifdef CONFIG_HARD_I2C
wdenk43d96162003-03-06 00:02:04 +000040#include <i2c.h>
Lei Wen3df619e2011-04-13 23:48:31 +053041#include "mv_i2c.h"
wdenk43d96162003-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
wdenk43d96162003-03-06 00:02:04 +000049/* All transfers are described by this data structure */
50struct i2c_msg {
51 u8 condition;
wdenk8bde7f72003-06-27 21:31:46 +000052 u8 acknack;
53 u8 direction;
wdenk43d96162003-03-06 00:02:04 +000054 u8 data;
55};
56
Lei Wen3df619e2011-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
Lei Wenadb00bb2011-04-13 23:48:39 +053069static struct mv_i2c *base;
70#ifdef CONFIG_I2C_MULTI_BUS
71static u32 i2c_regs[CONFIG_MV_I2C_NUM] = CONFIG_MV_I2C_REG;
72static unsigned int bus_initialized[CONFIG_MV_I2C_NUM];
73static unsigned int current_bus;
74
75int i2c_set_bus_num(unsigned int bus)
76{
77 if ((bus < 0) || (bus >= CONFIG_MV_I2C_NUM)) {
78 printf("Bad bus: %d\n", bus);
79 return -1;
80 }
81
82 base = (struct mv_i2c *)i2c_regs[bus];
83 current_bus = bus;
84
85 if (!bus_initialized[current_bus]) {
86 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
87 bus_initialized[current_bus] = 1;
88 }
89
90 return 0;
91}
92
93unsigned int i2c_get_bus_num(void)
94{
95 return current_bus;
96}
97#endif
Lei Wen3df619e2011-04-13 23:48:31 +053098
Lei Wen68432c22011-04-13 23:48:16 +053099/*
Lei Wen3df619e2011-04-13 23:48:31 +0530100 * i2c_reset: - reset the host controller
wdenk43d96162003-03-06 00:02:04 +0000101 *
102 */
Lei Wen68432c22011-04-13 23:48:16 +0530103static void i2c_reset(void)
wdenk43d96162003-03-06 00:02:04 +0000104{
Lei Wen3df619e2011-04-13 23:48:31 +0530105 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
106 writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */
wdenk8bde7f72003-06-27 21:31:46 +0000107 udelay(100);
Lei Wen3df619e2011-04-13 23:48:31 +0530108 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
109
110 i2c_clk_enable();
111
112 writel(CONFIG_SYS_I2C_SLAVE, &base->isar); /* set our slave address */
113 writel(I2C_ICR_INIT, &base->icr); /* set control reg values */
114 writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */
115 writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */
wdenk8bde7f72003-06-27 21:31:46 +0000116 udelay(100);
wdenk43d96162003-03-06 00:02:04 +0000117}
118
Lei Wen68432c22011-04-13 23:48:16 +0530119/*
wdenk8bde7f72003-06-27 21:31:46 +0000120 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
wdenk43d96162003-03-06 00:02:04 +0000121 * are set and cleared
122 *
Markus Klotzbuecherba70d6a2006-03-24 12:23:27 +0100123 * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
wdenk43d96162003-03-06 00:02:04 +0000124 */
Lei Wen68432c22011-04-13 23:48:16 +0530125static int i2c_isr_set_cleared(unsigned long set_mask,
126 unsigned long cleared_mask)
wdenk43d96162003-03-06 00:02:04 +0000127{
Lei Wen3df619e2011-04-13 23:48:31 +0530128 int timeout = 1000, isr;
wdenk43d96162003-03-06 00:02:04 +0000129
Lei Wen3df619e2011-04-13 23:48:31 +0530130 do {
131 isr = readl(&base->isr);
Lei Wen68432c22011-04-13 23:48:16 +0530132 udelay(10);
133 if (timeout-- < 0)
134 return 0;
Lei Wen3df619e2011-04-13 23:48:31 +0530135 } while (((isr & set_mask) != set_mask)
136 || ((isr & cleared_mask) != 0));
wdenk43d96162003-03-06 00:02:04 +0000137
wdenk8bde7f72003-06-27 21:31:46 +0000138 return 1;
wdenk43d96162003-03-06 00:02:04 +0000139}
140
Lei Wen68432c22011-04-13 23:48:16 +0530141/*
wdenk43d96162003-03-06 00:02:04 +0000142 * i2c_transfer: - Transfer one byte over the i2c bus
143 *
wdenk8bde7f72003-06-27 21:31:46 +0000144 * This function can tranfer a byte over the i2c bus in both directions.
145 * It is used by the public API functions.
wdenk43d96162003-03-06 00:02:04 +0000146 *
147 * @return: 0: transfer successful
148 * -1: message is empty
149 * -2: transmit timeout
150 * -3: ACK missing
151 * -4: receive timeout
152 * -5: illegal parameters
153 * -6: bus is busy and couldn't be aquired
wdenk8bde7f72003-06-27 21:31:46 +0000154 */
wdenk43d96162003-03-06 00:02:04 +0000155int i2c_transfer(struct i2c_msg *msg)
156{
157 int ret;
158
wdenk8bde7f72003-06-27 21:31:46 +0000159 if (!msg)
wdenk43d96162003-03-06 00:02:04 +0000160 goto transfer_error_msg_empty;
161
Lei Wen68432c22011-04-13 23:48:16 +0530162 switch (msg->direction) {
wdenk43d96162003-03-06 00:02:04 +0000163 case I2C_WRITE:
wdenk43d96162003-03-06 00:02:04 +0000164 /* check if bus is not busy */
Lei Wen68432c22011-04-13 23:48:16 +0530165 if (!i2c_isr_set_cleared(0, ISR_IBB))
wdenk43d96162003-03-06 00:02:04 +0000166 goto transfer_error_bus_busy;
167
168 /* start transmission */
Lei Wen3df619e2011-04-13 23:48:31 +0530169 writel(readl(&base->icr) & ~ICR_START, &base->icr);
170 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
171 writel(msg->data, &base->idbr);
Marek Vasut3ba8bf72010-09-09 09:50:39 +0200172 if (msg->condition == I2C_COND_START)
Lei Wen3df619e2011-04-13 23:48:31 +0530173 writel(readl(&base->icr) | ICR_START, &base->icr);
Marek Vasut3ba8bf72010-09-09 09:50:39 +0200174 if (msg->condition == I2C_COND_STOP)
Lei Wen3df619e2011-04-13 23:48:31 +0530175 writel(readl(&base->icr) | ICR_STOP, &base->icr);
Marek Vasut3ba8bf72010-09-09 09:50:39 +0200176 if (msg->acknack == I2C_ACKNAK_SENDNAK)
Lei Wen3df619e2011-04-13 23:48:31 +0530177 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
Marek Vasut3ba8bf72010-09-09 09:50:39 +0200178 if (msg->acknack == I2C_ACKNAK_SENDACK)
Lei Wen3df619e2011-04-13 23:48:31 +0530179 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
180 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
181 writel(readl(&base->icr) | ICR_TB, &base->icr);
wdenk43d96162003-03-06 00:02:04 +0000182
183 /* transmit register empty? */
Lei Wen68432c22011-04-13 23:48:16 +0530184 if (!i2c_isr_set_cleared(ISR_ITE, 0))
wdenk43d96162003-03-06 00:02:04 +0000185 goto transfer_error_transmit_timeout;
186
187 /* clear 'transmit empty' state */
Lei Wen3df619e2011-04-13 23:48:31 +0530188 writel(readl(&base->isr) | ISR_ITE, &base->isr);
wdenk43d96162003-03-06 00:02:04 +0000189
190 /* wait for ACK from slave */
191 if (msg->acknack == I2C_ACKNAK_WAITACK)
Lei Wen68432c22011-04-13 23:48:16 +0530192 if (!i2c_isr_set_cleared(0, ISR_ACKNAK))
wdenk43d96162003-03-06 00:02:04 +0000193 goto transfer_error_ack_missing;
194 break;
195
196 case I2C_READ:
197
198 /* check if bus is not busy */
Lei Wen68432c22011-04-13 23:48:16 +0530199 if (!i2c_isr_set_cleared(0, ISR_IBB))
wdenk43d96162003-03-06 00:02:04 +0000200 goto transfer_error_bus_busy;
201
202 /* start receive */
Lei Wen3df619e2011-04-13 23:48:31 +0530203 writel(readl(&base->icr) & ~ICR_START, &base->icr);
204 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
Marek Vasut3ba8bf72010-09-09 09:50:39 +0200205 if (msg->condition == I2C_COND_START)
Lei Wen3df619e2011-04-13 23:48:31 +0530206 writel(readl(&base->icr) | ICR_START, &base->icr);
Marek Vasut3ba8bf72010-09-09 09:50:39 +0200207 if (msg->condition == I2C_COND_STOP)
Lei Wen3df619e2011-04-13 23:48:31 +0530208 writel(readl(&base->icr) | ICR_STOP, &base->icr);
Marek Vasut3ba8bf72010-09-09 09:50:39 +0200209 if (msg->acknack == I2C_ACKNAK_SENDNAK)
Lei Wen3df619e2011-04-13 23:48:31 +0530210 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
Marek Vasut3ba8bf72010-09-09 09:50:39 +0200211 if (msg->acknack == I2C_ACKNAK_SENDACK)
Lei Wen3df619e2011-04-13 23:48:31 +0530212 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
213 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
214 writel(readl(&base->icr) | ICR_TB, &base->icr);
wdenk43d96162003-03-06 00:02:04 +0000215
216 /* receive register full? */
Lei Wen68432c22011-04-13 23:48:16 +0530217 if (!i2c_isr_set_cleared(ISR_IRF, 0))
wdenk8bde7f72003-06-27 21:31:46 +0000218 goto transfer_error_receive_timeout;
wdenk43d96162003-03-06 00:02:04 +0000219
Lei Wen3df619e2011-04-13 23:48:31 +0530220 msg->data = readl(&base->idbr);
wdenk43d96162003-03-06 00:02:04 +0000221
222 /* clear 'receive empty' state */
Lei Wen3df619e2011-04-13 23:48:31 +0530223 writel(readl(&base->isr) | ISR_IRF, &base->isr);
wdenk43d96162003-03-06 00:02:04 +0000224 break;
wdenk43d96162003-03-06 00:02:04 +0000225 default:
wdenk43d96162003-03-06 00:02:04 +0000226 goto transfer_error_illegal_param;
wdenk43d96162003-03-06 00:02:04 +0000227 }
228
wdenk8bde7f72003-06-27 21:31:46 +0000229 return 0;
wdenk43d96162003-03-06 00:02:04 +0000230
wdenk8bde7f72003-06-27 21:31:46 +0000231transfer_error_msg_empty:
wdenk43d96162003-03-06 00:02:04 +0000232 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
233 ret = -1; goto i2c_transfer_finish;
234
235transfer_error_transmit_timeout:
236 PRINTD(("i2c_transfer: error: transmit timeout\n"));
237 ret = -2; goto i2c_transfer_finish;
238
239transfer_error_ack_missing:
240 PRINTD(("i2c_transfer: error: ACK missing\n"));
241 ret = -3; goto i2c_transfer_finish;
242
243transfer_error_receive_timeout:
244 PRINTD(("i2c_transfer: error: receive timeout\n"));
245 ret = -4; goto i2c_transfer_finish;
246
247transfer_error_illegal_param:
248 PRINTD(("i2c_transfer: error: illegal parameters\n"));
249 ret = -5; goto i2c_transfer_finish;
250
251transfer_error_bus_busy:
252 PRINTD(("i2c_transfer: error: bus is busy\n"));
253 ret = -6; goto i2c_transfer_finish;
254
255i2c_transfer_finish:
Lei Wen2be1ed32011-09-30 05:43:48 +0000256 PRINTD(("i2c_transfer: ISR: 0x%04x\n", readl(&base->isr)));
wdenk43d96162003-03-06 00:02:04 +0000257 i2c_reset();
258 return ret;
wdenk43d96162003-03-06 00:02:04 +0000259}
260
261/* ------------------------------------------------------------------------ */
262/* API Functions */
263/* ------------------------------------------------------------------------ */
wdenk43d96162003-03-06 00:02:04 +0000264void i2c_init(int speed, int slaveaddr)
265{
Lei Wenadb00bb2011-04-13 23:48:39 +0530266#ifdef CONFIG_I2C_MULTI_BUS
267 base = (struct mv_i2c *)i2c_regs[current_bus];
268#else
269 base = (struct mv_i2c *)CONFIG_MV_I2C_REG;
270#endif
271
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200272#ifdef CONFIG_SYS_I2C_INIT_BOARD
Lei Wen3df619e2011-04-13 23:48:31 +0530273 u32 icr;
274 /*
275 * call board specific i2c bus reset routine before accessing the
276 * environment, which might be in a chip on that bus. For details
277 * about this problem see doc/I2C_Edge_Conditions.
278 *
279 * disable I2C controller first, otherwhise it thinks we want to
280 * talk to the slave port...
281 */
282 icr = readl(&base->icr);
283 writel(readl(&base->icr) & ~(ICR_SCLE | ICR_IUE), &base->icr);
284
wdenk47cd00f2003-03-06 13:39:27 +0000285 i2c_init_board();
Lei Wen3df619e2011-04-13 23:48:31 +0530286
287 writel(icr, &base->icr);
wdenk47cd00f2003-03-06 13:39:27 +0000288#endif
wdenk43d96162003-03-06 00:02:04 +0000289}
290
Lei Wen68432c22011-04-13 23:48:16 +0530291/*
wdenk43d96162003-03-06 00:02:04 +0000292 * i2c_probe: - Test if a chip answers for a given i2c address
293 *
wdenk8bde7f72003-06-27 21:31:46 +0000294 * @chip: address of the chip which is searched for
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200295 * @return: 0 if a chip was found, -1 otherwhise
wdenk43d96162003-03-06 00:02:04 +0000296 */
wdenk43d96162003-03-06 00:02:04 +0000297int i2c_probe(uchar chip)
298{
299 struct i2c_msg msg;
300
301 i2c_reset();
302
303 msg.condition = I2C_COND_START;
304 msg.acknack = I2C_ACKNAK_WAITACK;
305 msg.direction = I2C_WRITE;
306 msg.data = (chip << 1) + 1;
Lei Wen68432c22011-04-13 23:48:16 +0530307 if (i2c_transfer(&msg))
308 return -1;
wdenk43d96162003-03-06 00:02:04 +0000309
310 msg.condition = I2C_COND_STOP;
311 msg.acknack = I2C_ACKNAK_SENDNAK;
312 msg.direction = I2C_READ;
313 msg.data = 0x00;
Lei Wen68432c22011-04-13 23:48:16 +0530314 if (i2c_transfer(&msg))
315 return -1;
wdenk43d96162003-03-06 00:02:04 +0000316
317 return 0;
318}
319
Lei Wen68432c22011-04-13 23:48:16 +0530320/*
wdenk43d96162003-03-06 00:02:04 +0000321 * i2c_read: - Read multiple bytes from an i2c device
322 *
323 * The higher level routines take into account that this function is only
wdenk8bde7f72003-06-27 21:31:46 +0000324 * called with len < page length of the device (see configuration file)
wdenk43d96162003-03-06 00:02:04 +0000325 *
326 * @chip: address of the chip which is to be read
327 * @addr: i2c data address within the chip
328 * @alen: length of the i2c data address (1..2 bytes)
329 * @buffer: where to write the data
330 * @len: how much byte do we want to read
331 * @return: 0 in case of success
332 */
wdenk43d96162003-03-06 00:02:04 +0000333int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
334{
335 struct i2c_msg msg;
336 u8 addr_bytes[3]; /* lowest...highest byte of data address */
wdenk43d96162003-03-06 00:02:04 +0000337
Lei Wen68432c22011-04-13 23:48:16 +0530338 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
339 "len=0x%02x)\n", chip, addr, alen, len));
wdenk43d96162003-03-06 00:02:04 +0000340
341 i2c_reset();
342
343 /* dummy chip address write */
344 PRINTD(("i2c_read: dummy chip address write\n"));
345 msg.condition = I2C_COND_START;
346 msg.acknack = I2C_ACKNAK_WAITACK;
347 msg.direction = I2C_WRITE;
Lei Wen68432c22011-04-13 23:48:16 +0530348 msg.data = (chip << 1);
349 msg.data &= 0xFE;
350 if (i2c_transfer(&msg))
351 return -1;
wdenk8bde7f72003-06-27 21:31:46 +0000352
wdenk43d96162003-03-06 00:02:04 +0000353 /*
wdenk8bde7f72003-06-27 21:31:46 +0000354 * send memory address bytes;
355 * alen defines how much bytes we have to send.
wdenk43d96162003-03-06 00:02:04 +0000356 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200357 /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */
wdenk43d96162003-03-06 00:02:04 +0000358 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
359 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
360 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
361
362 while (--alen >= 0) {
Lei Wen68432c22011-04-13 23:48:16 +0530363 PRINTD(("i2c_read: send memory word address byte %1d\n", alen));
wdenk43d96162003-03-06 00:02:04 +0000364 msg.condition = I2C_COND_NORMAL;
365 msg.acknack = I2C_ACKNAK_WAITACK;
366 msg.direction = I2C_WRITE;
367 msg.data = addr_bytes[alen];
Lei Wen68432c22011-04-13 23:48:16 +0530368 if (i2c_transfer(&msg))
369 return -1;
wdenk43d96162003-03-06 00:02:04 +0000370 }
wdenk8bde7f72003-06-27 21:31:46 +0000371
wdenk43d96162003-03-06 00:02:04 +0000372 /* start read sequence */
373 PRINTD(("i2c_read: start read sequence\n"));
374 msg.condition = I2C_COND_START;
375 msg.acknack = I2C_ACKNAK_WAITACK;
376 msg.direction = I2C_WRITE;
377 msg.data = (chip << 1);
378 msg.data |= 0x01;
Lei Wen68432c22011-04-13 23:48:16 +0530379 if (i2c_transfer(&msg))
380 return -1;
wdenk43d96162003-03-06 00:02:04 +0000381
382 /* read bytes; send NACK at last byte */
383 while (len--) {
Lei Wen68432c22011-04-13 23:48:16 +0530384 if (len == 0) {
wdenk43d96162003-03-06 00:02:04 +0000385 msg.condition = I2C_COND_STOP;
386 msg.acknack = I2C_ACKNAK_SENDNAK;
387 } else {
388 msg.condition = I2C_COND_NORMAL;
389 msg.acknack = I2C_ACKNAK_SENDACK;
390 }
391
392 msg.direction = I2C_READ;
393 msg.data = 0x00;
Lei Wen68432c22011-04-13 23:48:16 +0530394 if (i2c_transfer(&msg))
395 return -1;
wdenk43d96162003-03-06 00:02:04 +0000396
Markus Klotzbuecherba70d6a2006-03-24 12:23:27 +0100397 *buffer = msg.data;
Lei Wen68432c22011-04-13 23:48:16 +0530398 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",
399 (unsigned int)buffer, *buffer));
Markus Klotzbuecherba70d6a2006-03-24 12:23:27 +0100400 buffer++;
wdenk43d96162003-03-06 00:02:04 +0000401 }
402
403 i2c_reset();
404
405 return 0;
406}
407
Lei Wen68432c22011-04-13 23:48:16 +0530408/*
wdenk43d96162003-03-06 00:02:04 +0000409 * i2c_write: - Write multiple bytes to an i2c device
410 *
411 * The higher level routines take into account that this function is only
wdenk8bde7f72003-06-27 21:31:46 +0000412 * called with len < page length of the device (see configuration file)
wdenk43d96162003-03-06 00:02:04 +0000413 *
414 * @chip: address of the chip which is to be written
415 * @addr: i2c data address within the chip
416 * @alen: length of the i2c data address (1..2 bytes)
wdenk8bde7f72003-06-27 21:31:46 +0000417 * @buffer: where to find the data to be written
wdenk43d96162003-03-06 00:02:04 +0000418 * @len: how much byte do we want to read
419 * @return: 0 in case of success
420 */
wdenk43d96162003-03-06 00:02:04 +0000421int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
422{
423 struct i2c_msg msg;
424 u8 addr_bytes[3]; /* lowest...highest byte of data address */
425
Lei Wen68432c22011-04-13 23:48:16 +0530426 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
427 "len=0x%02x)\n", chip, addr, alen, len));
wdenk43d96162003-03-06 00:02:04 +0000428
429 i2c_reset();
430
431 /* chip address write */
432 PRINTD(("i2c_write: chip address write\n"));
433 msg.condition = I2C_COND_START;
434 msg.acknack = I2C_ACKNAK_WAITACK;
435 msg.direction = I2C_WRITE;
Lei Wen68432c22011-04-13 23:48:16 +0530436 msg.data = (chip << 1);
437 msg.data &= 0xFE;
438 if (i2c_transfer(&msg))
439 return -1;
wdenk8bde7f72003-06-27 21:31:46 +0000440
wdenk43d96162003-03-06 00:02:04 +0000441 /*
wdenk8bde7f72003-06-27 21:31:46 +0000442 * send memory address bytes;
443 * alen defines how much bytes we have to send.
wdenk43d96162003-03-06 00:02:04 +0000444 */
445 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
446 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
447 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
448
449 while (--alen >= 0) {
wdenk43d96162003-03-06 00:02:04 +0000450 PRINTD(("i2c_write: send memory word address\n"));
451 msg.condition = I2C_COND_NORMAL;
452 msg.acknack = I2C_ACKNAK_WAITACK;
453 msg.direction = I2C_WRITE;
454 msg.data = addr_bytes[alen];
Lei Wen68432c22011-04-13 23:48:16 +0530455 if (i2c_transfer(&msg))
456 return -1;
wdenk43d96162003-03-06 00:02:04 +0000457 }
wdenk8bde7f72003-06-27 21:31:46 +0000458
wdenk43d96162003-03-06 00:02:04 +0000459 /* write bytes; send NACK at last byte */
460 while (len--) {
Lei Wen68432c22011-04-13 23:48:16 +0530461 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",
462 (unsigned int)buffer, *buffer));
wdenk43d96162003-03-06 00:02:04 +0000463
Lei Wen68432c22011-04-13 23:48:16 +0530464 if (len == 0)
wdenk43d96162003-03-06 00:02:04 +0000465 msg.condition = I2C_COND_STOP;
466 else
467 msg.condition = I2C_COND_NORMAL;
468
469 msg.acknack = I2C_ACKNAK_WAITACK;
470 msg.direction = I2C_WRITE;
471 msg.data = *(buffer++);
wdenk8bde7f72003-06-27 21:31:46 +0000472
Lei Wen68432c22011-04-13 23:48:16 +0530473 if (i2c_transfer(&msg))
474 return -1;
wdenk43d96162003-03-06 00:02:04 +0000475 }
476
477 i2c_reset();
478
479 return 0;
wdenk43d96162003-03-06 00:02:04 +0000480}
wdenk43d96162003-03-06 00:02:04 +0000481#endif /* CONFIG_HARD_I2C */