3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
5 * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Marius Groeger <mgroeger@sysgo.de>
8 * (C) Copyright 2003 Pengutronix e.K.
9 * Robert Schwebel <r.schwebel@pengutronix.de>
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * Back ported to the 8xx platform (from the 8260 platform) by
30 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
33 /* FIXME: this file is PXA255 specific! What about other XScales? */
37 #ifdef CONFIG_HARD_I2C
41 * - I2C_PXA_SLAVE_ADDR
44 #include <asm/arch/hardware.h>
45 #include <asm/arch/pxa-regs.h>
48 /*#define DEBUG_I2C 1 /###* activate local debugging output */
49 #define I2C_PXA_SLAVE_ADDR 0x1 /* slave pxa unit address */
51 #if (CFG_I2C_SPEED == 400000)
52 #define I2C_ICR_INIT (ICR_FM | ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
54 #define I2C_ICR_INIT (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
57 #define I2C_ISR_INIT 0x7FF
60 #define PRINTD(x) printf x
66 /* Shall the current transfer have a start/stop condition? */
67 #define I2C_COND_NORMAL 0
68 #define I2C_COND_START 1
69 #define I2C_COND_STOP 2
71 /* Shall the current transfer be ack/nacked or being waited for it? */
72 #define I2C_ACKNAK_WAITACK 1
73 #define I2C_ACKNAK_SENDACK 2
74 #define I2C_ACKNAK_SENDNAK 4
76 /* Specify who shall transfer the data (master or slave) */
80 /* All transfers are described by this data structure */
90 * i2c_pxa_reset: - reset the host controller
94 static void i2c_reset( void )
96 ICR &= ~ICR_IUE; /* disable unit */
97 ICR |= ICR_UR; /* reset the unit */
99 ICR &= ~ICR_IUE; /* disable unit */
100 #ifdef CONFIG_CPU_MONAHANS
101 CKENB |= (CKENB_4_I2C); /* | CKENB_1_PWM1 | CKENB_0_PWM0); */
102 #else /* CONFIG_CPU_MONAHANS */
103 CKEN |= CKEN14_I2C; /* set the global I2C clock on */
105 ISAR = I2C_PXA_SLAVE_ADDR; /* set our slave address */
106 ICR = I2C_ICR_INIT; /* set control register values */
107 ISR = I2C_ISR_INIT; /* set clear interrupt bits */
108 ICR |= ICR_IUE; /* enable unit */
114 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
115 * are set and cleared
117 * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
119 static int i2c_isr_set_cleared( unsigned long set_mask, unsigned long cleared_mask )
123 while( ((ISR & set_mask)!=set_mask) || ((ISR & cleared_mask)!=0) ){
125 if( timeout-- < 0 ) return 0;
133 * i2c_transfer: - Transfer one byte over the i2c bus
135 * This function can tranfer a byte over the i2c bus in both directions.
136 * It is used by the public API functions.
138 * @return: 0: transfer successful
139 * -1: message is empty
140 * -2: transmit timeout
142 * -4: receive timeout
143 * -5: illegal parameters
144 * -6: bus is busy and couldn't be aquired
146 int i2c_transfer(struct i2c_msg *msg)
151 goto transfer_error_msg_empty;
153 switch(msg->direction) {
157 /* check if bus is not busy */
158 if (!i2c_isr_set_cleared(0,ISR_IBB))
159 goto transfer_error_bus_busy;
161 /* start transmission */
165 if (msg->condition == I2C_COND_START) ICR |= ICR_START;
166 if (msg->condition == I2C_COND_STOP) ICR |= ICR_STOP;
167 if (msg->acknack == I2C_ACKNAK_SENDNAK) ICR |= ICR_ACKNAK;
168 if (msg->acknack == I2C_ACKNAK_SENDACK) ICR &= ~ICR_ACKNAK;
172 /* transmit register empty? */
173 if (!i2c_isr_set_cleared(ISR_ITE,0))
174 goto transfer_error_transmit_timeout;
176 /* clear 'transmit empty' state */
179 /* wait for ACK from slave */
180 if (msg->acknack == I2C_ACKNAK_WAITACK)
181 if (!i2c_isr_set_cleared(0,ISR_ACKNAK))
182 goto transfer_error_ack_missing;
187 /* check if bus is not busy */
188 if (!i2c_isr_set_cleared(0,ISR_IBB))
189 goto transfer_error_bus_busy;
194 if (msg->condition == I2C_COND_START) ICR |= ICR_START;
195 if (msg->condition == I2C_COND_STOP) ICR |= ICR_STOP;
196 if (msg->acknack == I2C_ACKNAK_SENDNAK) ICR |= ICR_ACKNAK;
197 if (msg->acknack == I2C_ACKNAK_SENDACK) ICR &= ~ICR_ACKNAK;
201 /* receive register full? */
202 if (!i2c_isr_set_cleared(ISR_IRF,0))
203 goto transfer_error_receive_timeout;
207 /* clear 'receive empty' state */
214 goto transfer_error_illegal_param;
220 transfer_error_msg_empty:
221 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
222 ret = -1; goto i2c_transfer_finish;
224 transfer_error_transmit_timeout:
225 PRINTD(("i2c_transfer: error: transmit timeout\n"));
226 ret = -2; goto i2c_transfer_finish;
228 transfer_error_ack_missing:
229 PRINTD(("i2c_transfer: error: ACK missing\n"));
230 ret = -3; goto i2c_transfer_finish;
232 transfer_error_receive_timeout:
233 PRINTD(("i2c_transfer: error: receive timeout\n"));
234 ret = -4; goto i2c_transfer_finish;
236 transfer_error_illegal_param:
237 PRINTD(("i2c_transfer: error: illegal parameters\n"));
238 ret = -5; goto i2c_transfer_finish;
240 transfer_error_bus_busy:
241 PRINTD(("i2c_transfer: error: bus is busy\n"));
242 ret = -6; goto i2c_transfer_finish;
245 PRINTD(("i2c_transfer: ISR: 0x%04x\n",ISR));
251 /* ------------------------------------------------------------------------ */
253 /* ------------------------------------------------------------------------ */
255 void i2c_init(int speed, int slaveaddr)
257 #ifdef CFG_I2C_INIT_BOARD
258 /* call board specific i2c bus reset routine before accessing the */
259 /* environment, which might be in a chip on that bus. For details */
260 /* about this problem see doc/I2C_Edge_Conditions. */
267 * i2c_probe: - Test if a chip answers for a given i2c address
269 * @chip: address of the chip which is searched for
270 * @return: 0 if a chip was found, -1 otherwhise
273 int i2c_probe(uchar chip)
279 msg.condition = I2C_COND_START;
280 msg.acknack = I2C_ACKNAK_WAITACK;
281 msg.direction = I2C_WRITE;
282 msg.data = (chip << 1) + 1;
283 if (i2c_transfer(&msg)) return -1;
285 msg.condition = I2C_COND_STOP;
286 msg.acknack = I2C_ACKNAK_SENDNAK;
287 msg.direction = I2C_READ;
289 if (i2c_transfer(&msg)) return -1;
296 * i2c_read: - Read multiple bytes from an i2c device
298 * The higher level routines take into account that this function is only
299 * called with len < page length of the device (see configuration file)
301 * @chip: address of the chip which is to be read
302 * @addr: i2c data address within the chip
303 * @alen: length of the i2c data address (1..2 bytes)
304 * @buffer: where to write the data
305 * @len: how much byte do we want to read
306 * @return: 0 in case of success
309 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
312 u8 addr_bytes[3]; /* lowest...highest byte of data address */
315 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, len=0x%02x)\n",chip,addr,alen,len));
319 /* dummy chip address write */
320 PRINTD(("i2c_read: dummy chip address write\n"));
321 msg.condition = I2C_COND_START;
322 msg.acknack = I2C_ACKNAK_WAITACK;
323 msg.direction = I2C_WRITE;
324 msg.data = (chip << 1);
326 if ((ret=i2c_transfer(&msg))) return -1;
329 * send memory address bytes;
330 * alen defines how much bytes we have to send.
332 /*addr &= ((1 << CFG_EEPROM_PAGE_WRITE_BITS)-1); */
333 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
334 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
335 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
337 while (--alen >= 0) {
339 PRINTD(("i2c_read: send memory word address byte %1d\n",alen));
340 msg.condition = I2C_COND_NORMAL;
341 msg.acknack = I2C_ACKNAK_WAITACK;
342 msg.direction = I2C_WRITE;
343 msg.data = addr_bytes[alen];
344 if ((ret=i2c_transfer(&msg))) return -1;
348 /* start read sequence */
349 PRINTD(("i2c_read: start read sequence\n"));
350 msg.condition = I2C_COND_START;
351 msg.acknack = I2C_ACKNAK_WAITACK;
352 msg.direction = I2C_WRITE;
353 msg.data = (chip << 1);
355 if ((ret=i2c_transfer(&msg))) return -1;
357 /* read bytes; send NACK at last byte */
361 msg.condition = I2C_COND_STOP;
362 msg.acknack = I2C_ACKNAK_SENDNAK;
364 msg.condition = I2C_COND_NORMAL;
365 msg.acknack = I2C_ACKNAK_SENDACK;
368 msg.direction = I2C_READ;
370 if ((ret=i2c_transfer(&msg))) return -1;
373 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",(unsigned int)buffer,*buffer));
385 * i2c_write: - Write multiple bytes to an i2c device
387 * The higher level routines take into account that this function is only
388 * called with len < page length of the device (see configuration file)
390 * @chip: address of the chip which is to be written
391 * @addr: i2c data address within the chip
392 * @alen: length of the i2c data address (1..2 bytes)
393 * @buffer: where to find the data to be written
394 * @len: how much byte do we want to read
395 * @return: 0 in case of success
398 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
401 u8 addr_bytes[3]; /* lowest...highest byte of data address */
403 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, len=0x%02x)\n",chip,addr,alen,len));
407 /* chip address write */
408 PRINTD(("i2c_write: chip address write\n"));
409 msg.condition = I2C_COND_START;
410 msg.acknack = I2C_ACKNAK_WAITACK;
411 msg.direction = I2C_WRITE;
412 msg.data = (chip << 1);
414 if (i2c_transfer(&msg)) return -1;
417 * send memory address bytes;
418 * alen defines how much bytes we have to send.
420 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
421 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
422 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
424 while (--alen >= 0) {
426 PRINTD(("i2c_write: send memory word address\n"));
427 msg.condition = I2C_COND_NORMAL;
428 msg.acknack = I2C_ACKNAK_WAITACK;
429 msg.direction = I2C_WRITE;
430 msg.data = addr_bytes[alen];
431 if (i2c_transfer(&msg)) return -1;
434 /* write bytes; send NACK at last byte */
437 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",(unsigned int)buffer,*buffer));
440 msg.condition = I2C_COND_STOP;
442 msg.condition = I2C_COND_NORMAL;
444 msg.acknack = I2C_ACKNAK_WAITACK;
445 msg.direction = I2C_WRITE;
446 msg.data = *(buffer++);
448 if (i2c_transfer(&msg)) return -1;
458 uchar i2c_reg_read (uchar chip, uchar reg)
462 PRINTD(("i2c_reg_read(chip=0x%02x, reg=0x%02x)\n",chip,reg));
463 i2c_read(chip, reg, 1, &buf, 1);
467 void i2c_reg_write(uchar chip, uchar reg, uchar val)
469 PRINTD(("i2c_reg_write(chip=0x%02x, reg=0x%02x, val=0x%02x)\n",chip,reg,val));
470 i2c_write(chip, reg, 1, &val, 1);
473 #endif /* CONFIG_HARD_I2C */