2 * (C) Copyright 2003,Motorola Inc.
3 * Xianghua Xiao <x.xiao@motorola.com>
4 * Adapted for Motorola 85xx chip.
7 * Gleb Natapov <gnatapov@mrv.com>
8 * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
10 * Hardware I2C driver for MPC107 PCI bridge.
12 * See file CREDITS for list of people who contributed to this
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * 20050101: Eran Liberty (liberty@freescale.com)
33 * Initial file creating (porting from 85XX & 8260)
40 #ifdef CONFIG_HARD_I2C
44 #ifdef CONFIG_MPC8349ADS
45 i2c_t * mpc8349_i2c = (i2c_t*)(CFG_IMMRBAR + CFG_I2C_OFFSET);
49 i2c_init(int speed, int slaveadd)
51 /* stop I2C controller */
52 writeb(0x00 , &I2C->cr);
55 writeb(0x3f, &I2C->fdr);
57 /* set default filter */
58 writeb(0x10,&I2C->dfsrr);
60 /* write slave address */
61 writeb(slaveadd, &I2C->adr);
63 /* clear status register */
64 writeb(0x00, &I2C->sr);
66 /* start I2C controller */
67 writeb(I2C_CR_MEN, &I2C->cr);
73 ulong timeval = get_timer (0);
74 while (readb(&I2C->sr) & I2C_SR_MBB) {
75 if (get_timer (timeval) > I2C_TIMEOUT) {
86 ulong timeval = get_timer(0);
88 csr = readb(&I2C->sr);
90 if (!(csr & I2C_SR_MIF))
93 writeb(0x0, &I2C->sr);
95 if (csr & I2C_SR_MAL) {
96 debug("i2c_wait: MAL\n");
100 if (!(csr & I2C_SR_MCF)) {
101 debug("i2c_wait: unfinished\n");
105 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
106 debug("i2c_wait: No RXACK\n");
111 } while (get_timer (timeval) < I2C_TIMEOUT);
112 debug("i2c_wait: timed out\n");
116 static __inline__ int
117 i2c_write_addr (u8 dev, u8 dir, int rsta)
119 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
120 (rsta?I2C_CR_RSTA:0),
123 writeb((dev << 1) | dir, &I2C->dr);
125 if (i2c_wait (I2C_WRITE) < 0)
130 static __inline__ int
131 __i2c_write (u8 *data, int length)
135 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
138 for (i=0; i < length; i++) {
139 writeb(data[i], &I2C->dr);
141 if (i2c_wait (I2C_WRITE) < 0)
147 static __inline__ int
148 __i2c_read (u8 *data, int length)
152 writeb(I2C_CR_MEN | I2C_CR_MSTA |
153 ((length == 1) ? I2C_CR_TXAK : 0),
159 for (i=0; i < length; i++) {
160 if (i2c_wait (I2C_READ) < 0)
163 /* Generate ack on last next to last byte */
165 writeb(I2C_CR_MEN | I2C_CR_MSTA |
169 /* Generate stop on last byte */
171 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
173 data[i] = readb(&I2C->dr);
179 i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
184 if (i2c_wait4bus () < 0)
187 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
190 if (__i2c_write (&a[4 - alen], alen) != alen)
193 if (i2c_write_addr (dev, I2C_READ, 1) == 0)
196 i = __i2c_read (data, length);
199 writeb(I2C_CR_MEN, &I2C->cr);
200 return !(i == length);
204 i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
209 if (i2c_wait4bus () < 0)
212 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
215 if (__i2c_write (&a[4 - alen], alen) != alen)
218 i = __i2c_write (data, length);
221 writeb(I2C_CR_MEN, &I2C->cr);
222 return !(i == length);
225 int i2c_probe (uchar chip)
230 * Try to read the first location of the chip. The underlying
231 * driver doesn't appear to support sending just the chip address
232 * and looking for an <ACK> back.
235 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
238 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
242 i2c_read (i2c_addr, reg, 1, buf, 1);
247 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
249 i2c_write (i2c_addr, reg, 1, &val, 1);
252 #endif /* CONFIG_HARD_I2C */