2 * (C) Copyright 2006 Freescale Semiconductor, Inc.
4 * (C) Copyright 2003,Motorola Inc.
5 * Xianghua Xiao <x.xiao@motorola.com>
6 * Adapted for Motorola 85xx chip.
9 * Gleb Natapov <gnatapov@mrv.com>
10 * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
12 * Hardware I2C driver for MPC107 PCI bridge.
14 * See file CREDITS for list of people who contributed to this
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.
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.
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,
34 * 20050101: Eran Liberty (liberty@freescale.com)
35 * Initial file creating (porting from 85XX & 8260)
36 * 20060601: Dave Liu (daveliu@freescale.com)
37 * Unified variable names for mpc83xx
44 #ifdef CONFIG_HARD_I2C
48 #if defined(CONFIG_MPC8349EMDS) || defined(CONFIG_TQM834X)
49 i2c_t * mpc83xx_i2c = (i2c_t*)(CFG_IMMRBAR + CFG_I2C_OFFSET);
53 i2c_init(int speed, int slaveadd)
55 /* stop I2C controller */
56 writeb(0x00 , &I2C->cr);
59 writeb(speed, &I2C->fdr);
61 /* set default filter */
62 writeb(0x10,&I2C->dfsrr);
64 /* write slave address */
65 writeb(slaveadd, &I2C->adr);
67 /* clear status register */
68 writeb(0x00, &I2C->sr);
70 /* start I2C controller */
71 writeb(I2C_CR_MEN, &I2C->cr);
77 ulong timeval = get_timer (0);
78 while (readb(&I2C->sr) & I2C_SR_MBB) {
79 if (get_timer (timeval) > I2C_TIMEOUT) {
90 ulong timeval = get_timer(0);
92 csr = readb(&I2C->sr);
94 if (!(csr & I2C_SR_MIF))
97 writeb(0x0, &I2C->sr);
99 if (csr & I2C_SR_MAL) {
100 debug("i2c_wait: MAL\n");
104 if (!(csr & I2C_SR_MCF)) {
105 debug("i2c_wait: unfinished\n");
109 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
110 debug("i2c_wait: No RXACK\n");
115 } while (get_timer (timeval) < I2C_TIMEOUT);
117 debug("i2c_wait: timed out\n");
121 static __inline__ int
122 i2c_write_addr (u8 dev, u8 dir, int rsta)
124 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
125 (rsta?I2C_CR_RSTA:0),
128 writeb((dev << 1) | dir, &I2C->dr);
130 if (i2c_wait (I2C_WRITE) < 0)
135 static __inline__ int
136 __i2c_write (u8 *data, int length)
140 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
143 for (i=0; i < length; i++) {
144 writeb(data[i], &I2C->dr);
146 if (i2c_wait (I2C_WRITE) < 0)
152 static __inline__ int
153 __i2c_read (u8 *data, int length)
157 writeb(I2C_CR_MEN | I2C_CR_MSTA |
158 ((length == 1) ? I2C_CR_TXAK : 0),
164 for (i=0; i < length; i++) {
165 if (i2c_wait (I2C_READ) < 0)
168 /* Generate ack on last next to last byte */
170 writeb(I2C_CR_MEN | I2C_CR_MSTA |
174 /* Generate stop on last byte */
176 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
178 data[i] = readb(&I2C->dr);
184 i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
189 if (i2c_wait4bus () < 0)
192 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
195 if (__i2c_write (&a[4 - alen], alen) != alen)
198 if (i2c_write_addr (dev, I2C_READ, 1) == 0)
201 i = __i2c_read (data, length);
204 writeb(I2C_CR_MEN, &I2C->cr);
205 return !(i == length);
209 i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
214 if (i2c_wait4bus () < 0)
217 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
220 if (__i2c_write (&a[4 - alen], alen) != alen)
223 i = __i2c_write (data, length);
226 writeb(I2C_CR_MEN, &I2C->cr);
227 return !(i == length);
230 int i2c_probe (uchar chip)
235 * Try to read the first location of the chip. The underlying
236 * driver doesn't appear to support sending just the chip address
237 * and looking for an <ACK> back.
240 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
243 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
247 i2c_read (i2c_addr, reg, 1, buf, 1);
252 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
254 i2c_write (i2c_addr, reg, 1, &val, 1);
257 #endif /* CONFIG_HARD_I2C */