blob: 94da4a90a958224482ae2d09553fe6a96896cdb4 [file] [log] [blame]
Eran Liberty9095d4a2005-07-28 10:08:46 -05001/*
2 * (C) Copyright 2003,Motorola Inc.
3 * Xianghua Xiao <x.xiao@motorola.com>
4 * Adapted for Motorola 85xx chip.
5 *
6 * (C) Copyright 2003
7 * Gleb Natapov <gnatapov@mrv.com>
8 * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
9 *
10 * Hardware I2C driver for MPC107 PCI bridge.
11 *
12 * See file CREDITS for list of people who contributed to this
13 * project.
14 *
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.
19 *
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.
24 *
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,
28 * MA 02111-1307 USA
29 *
30 * Change log:
31 *
32 * 20050101: Eran Liberty (liberty@freescale.com)
33 * Initial file creating (porting from 85XX & 8260)
34 */
35
36#include <common.h>
37#include <command.h>
38#include <asm/io.h>
39
40#ifdef CONFIG_HARD_I2C
41#include <i2c.h>
42#include <asm/i2c.h>
43
44#ifdef CONFIG_MPC8349ADS
45i2c_t * mpc8349_i2c = (i2c_t*)(CFG_IMMRBAR + CFG_I2C_OFFSET);
46#endif
47
48void
49i2c_init(int speed, int slaveadd)
50{
51 /* stop I2C controller */
52 writeb(0x00 , &I2C->cr);
53
54 /* set clock */
55 writeb(0x3f, &I2C->fdr);
56
57 /* set default filter */
58 writeb(0x10,&I2C->dfsrr);
59
60 /* write slave address */
61 writeb(slaveadd, &I2C->adr);
62
63 /* clear status register */
64 writeb(0x00, &I2C->sr);
65
66 /* start I2C controller */
67 writeb(I2C_CR_MEN, &I2C->cr);
68}
69
70static __inline__ int
71i2c_wait4bus (void)
72{
73 ulong timeval = get_timer (0);
74 while (readb(&I2C->sr) & I2C_SR_MBB) {
75 if (get_timer (timeval) > I2C_TIMEOUT) {
76 return -1;
77 }
78 }
79 return 0;
80}
81
82static __inline__ int
83i2c_wait (int write)
84{
85 u32 csr;
86 ulong timeval = get_timer(0);
87 do {
88 csr = readb(&I2C->sr);
89
90 if (!(csr & I2C_SR_MIF))
91 continue;
92
93 writeb(0x0, &I2C->sr);
94
95 if (csr & I2C_SR_MAL) {
96 debug("i2c_wait: MAL\n");
97 return -1;
98 }
99
100 if (!(csr & I2C_SR_MCF)) {
101 debug("i2c_wait: unfinished\n");
102 return -1;
103 }
104
105 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
106 debug("i2c_wait: No RXACK\n");
107 return -1;
108 }
109
110 return 0;
111 } while (get_timer (timeval) < I2C_TIMEOUT);
112 debug("i2c_wait: timed out\n");
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200113 return -1;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500114}
115
116static __inline__ int
117i2c_write_addr (u8 dev, u8 dir, int rsta)
118{
119 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
120 (rsta?I2C_CR_RSTA:0),
121 &I2C->cr);
122
123 writeb((dev << 1) | dir, &I2C->dr);
124
125 if (i2c_wait (I2C_WRITE) < 0)
126 return 0;
127 return 1;
128}
129
130static __inline__ int
131__i2c_write (u8 *data, int length)
132{
133 int i;
134
135 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
136 &I2C->cr);
137
138 for (i=0; i < length; i++) {
139 writeb(data[i], &I2C->dr);
140
141 if (i2c_wait (I2C_WRITE) < 0)
142 break;
143 }
144 return i;
145}
146
147static __inline__ int
148__i2c_read (u8 *data, int length)
149{
150 int i;
151
152 writeb(I2C_CR_MEN | I2C_CR_MSTA |
153 ((length == 1) ? I2C_CR_TXAK : 0),
154 &I2C->cr);
155
156 /* dummy read */
157 readb(&I2C->dr);
158
159 for (i=0; i < length; i++) {
160 if (i2c_wait (I2C_READ) < 0)
161 break;
162
163 /* Generate ack on last next to last byte */
164 if (i == length - 2)
165 writeb(I2C_CR_MEN | I2C_CR_MSTA |
166 I2C_CR_TXAK,
167 &I2C->cr);
168
169 /* Generate stop on last byte */
170 if (i == length - 1)
171 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
172
173 data[i] = readb(&I2C->dr);
174 }
175 return i;
176}
177
178int
179i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
180{
181 int i = 0;
182 u8 *a = (u8*)&addr;
183
184 if (i2c_wait4bus () < 0)
185 goto exit;
186
187 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
188 goto exit;
189
190 if (__i2c_write (&a[4 - alen], alen) != alen)
191 goto exit;
192
193 if (i2c_write_addr (dev, I2C_READ, 1) == 0)
194 goto exit;
195
196 i = __i2c_read (data, length);
197
198 exit:
199 writeb(I2C_CR_MEN, &I2C->cr);
200 return !(i == length);
201}
202
203int
204i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
205{
206 int i = 0;
207 u8 *a = (u8*)&addr;
208
209 if (i2c_wait4bus () < 0)
210 goto exit;
211
212 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
213 goto exit;
214
215 if (__i2c_write (&a[4 - alen], alen) != alen)
216 goto exit;
217
218 i = __i2c_write (data, length);
219
220 exit:
221 writeb(I2C_CR_MEN, &I2C->cr);
222 return !(i == length);
223}
224
225int i2c_probe (uchar chip)
226{
227 int tmp;
228
229 /*
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.
233 */
234 udelay(10000);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200235 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
Eran Liberty9095d4a2005-07-28 10:08:46 -0500236}
237
238uchar i2c_reg_read (uchar i2c_addr, uchar reg)
239{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200240 uchar buf[1];
Eran Liberty9095d4a2005-07-28 10:08:46 -0500241
242 i2c_read (i2c_addr, reg, 1, buf, 1);
243
244 return (buf[0]);
245}
246
247void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
248{
249 i2c_write (i2c_addr, reg, 1, &val, 1);
250}
251
252#endif /* CONFIG_HARD_I2C */