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");
115 static __inline__ int
116 i2c_write_addr (u8 dev, u8 dir, int rsta)
118 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
119 (rsta?I2C_CR_RSTA:0),
122 writeb((dev << 1) | dir, &I2C->dr);
124 if (i2c_wait (I2C_WRITE) < 0)
129 static __inline__ int
130 __i2c_write (u8 *data, int length)
134 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
137 for (i=0; i < length; i++) {
138 writeb(data[i], &I2C->dr);
140 if (i2c_wait (I2C_WRITE) < 0)
146 static __inline__ int
147 __i2c_read (u8 *data, int length)
151 writeb(I2C_CR_MEN | I2C_CR_MSTA |
152 ((length == 1) ? I2C_CR_TXAK : 0),
158 for (i=0; i < length; i++) {
159 if (i2c_wait (I2C_READ) < 0)
162 /* Generate ack on last next to last byte */
164 writeb(I2C_CR_MEN | I2C_CR_MSTA |
168 /* Generate stop on last byte */
170 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
172 data[i] = readb(&I2C->dr);
178 i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
183 if (i2c_wait4bus () < 0)
186 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
189 if (__i2c_write (&a[4 - alen], alen) != alen)
192 if (i2c_write_addr (dev, I2C_READ, 1) == 0)
195 i = __i2c_read (data, length);
198 writeb(I2C_CR_MEN, &I2C->cr);
199 return !(i == length);
203 i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
208 if (i2c_wait4bus () < 0)
211 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
214 if (__i2c_write (&a[4 - alen], alen) != alen)
217 i = __i2c_write (data, length);
220 writeb(I2C_CR_MEN, &I2C->cr);
221 return !(i == length);
224 int i2c_probe (uchar chip)
229 * Try to read the first location of the chip. The underlying
230 * driver doesn't appear to support sending just the chip address
231 * and looking for an <ACK> back.
234 return i2c_read (chip, 0, 1, (char *)&tmp, 1);
237 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
241 i2c_read (i2c_addr, reg, 1, buf, 1);
246 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
248 i2c_write (i2c_addr, reg, 1, &val, 1);
251 #endif /* CONFIG_HARD_I2C */