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 #if defined(CONFIG_MPC8349ADS) || defined(CONFIG_TQM834X)
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);
113 debug("i2c_wait: timed out\n");
117 static __inline__ int
118 i2c_write_addr (u8 dev, u8 dir, int rsta)
120 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
121 (rsta?I2C_CR_RSTA:0),
124 writeb((dev << 1) | dir, &I2C->dr);
126 if (i2c_wait (I2C_WRITE) < 0)
131 static __inline__ int
132 __i2c_write (u8 *data, int length)
136 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
139 for (i=0; i < length; i++) {
140 writeb(data[i], &I2C->dr);
142 if (i2c_wait (I2C_WRITE) < 0)
148 static __inline__ int
149 __i2c_read (u8 *data, int length)
153 writeb(I2C_CR_MEN | I2C_CR_MSTA |
154 ((length == 1) ? I2C_CR_TXAK : 0),
160 for (i=0; i < length; i++) {
161 if (i2c_wait (I2C_READ) < 0)
164 /* Generate ack on last next to last byte */
166 writeb(I2C_CR_MEN | I2C_CR_MSTA |
170 /* Generate stop on last byte */
172 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
174 data[i] = readb(&I2C->dr);
180 i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
185 if (i2c_wait4bus () < 0)
188 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
191 if (__i2c_write (&a[4 - alen], alen) != alen)
194 if (i2c_write_addr (dev, I2C_READ, 1) == 0)
197 i = __i2c_read (data, length);
200 writeb(I2C_CR_MEN, &I2C->cr);
201 return !(i == length);
205 i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
210 if (i2c_wait4bus () < 0)
213 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
216 if (__i2c_write (&a[4 - alen], alen) != alen)
219 i = __i2c_write (data, length);
222 writeb(I2C_CR_MEN, &I2C->cr);
223 return !(i == length);
226 int i2c_probe (uchar chip)
231 * Try to read the first location of the chip. The underlying
232 * driver doesn't appear to support sending just the chip address
233 * and looking for an <ACK> back.
236 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
239 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
243 i2c_read (i2c_addr, reg, 1, buf, 1);
248 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
250 i2c_write (i2c_addr, reg, 1, &val, 1);
253 #endif /* CONFIG_HARD_I2C */