blob: 3db7d2cbcdca6297b6231ff6e0469669b33056b3 [file] [log] [blame]
Eran Libertyf046ccd2005-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");
113}
114
115static __inline__ int
116i2c_write_addr (u8 dev, u8 dir, int rsta)
117{
118 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
119 (rsta?I2C_CR_RSTA:0),
120 &I2C->cr);
121
122 writeb((dev << 1) | dir, &I2C->dr);
123
124 if (i2c_wait (I2C_WRITE) < 0)
125 return 0;
126 return 1;
127}
128
129static __inline__ int
130__i2c_write (u8 *data, int length)
131{
132 int i;
133
134 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
135 &I2C->cr);
136
137 for (i=0; i < length; i++) {
138 writeb(data[i], &I2C->dr);
139
140 if (i2c_wait (I2C_WRITE) < 0)
141 break;
142 }
143 return i;
144}
145
146static __inline__ int
147__i2c_read (u8 *data, int length)
148{
149 int i;
150
151 writeb(I2C_CR_MEN | I2C_CR_MSTA |
152 ((length == 1) ? I2C_CR_TXAK : 0),
153 &I2C->cr);
154
155 /* dummy read */
156 readb(&I2C->dr);
157
158 for (i=0; i < length; i++) {
159 if (i2c_wait (I2C_READ) < 0)
160 break;
161
162 /* Generate ack on last next to last byte */
163 if (i == length - 2)
164 writeb(I2C_CR_MEN | I2C_CR_MSTA |
165 I2C_CR_TXAK,
166 &I2C->cr);
167
168 /* Generate stop on last byte */
169 if (i == length - 1)
170 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
171
172 data[i] = readb(&I2C->dr);
173 }
174 return i;
175}
176
177int
178i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
179{
180 int i = 0;
181 u8 *a = (u8*)&addr;
182
183 if (i2c_wait4bus () < 0)
184 goto exit;
185
186 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
187 goto exit;
188
189 if (__i2c_write (&a[4 - alen], alen) != alen)
190 goto exit;
191
192 if (i2c_write_addr (dev, I2C_READ, 1) == 0)
193 goto exit;
194
195 i = __i2c_read (data, length);
196
197 exit:
198 writeb(I2C_CR_MEN, &I2C->cr);
199 return !(i == length);
200}
201
202int
203i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
204{
205 int i = 0;
206 u8 *a = (u8*)&addr;
207
208 if (i2c_wait4bus () < 0)
209 goto exit;
210
211 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
212 goto exit;
213
214 if (__i2c_write (&a[4 - alen], alen) != alen)
215 goto exit;
216
217 i = __i2c_write (data, length);
218
219 exit:
220 writeb(I2C_CR_MEN, &I2C->cr);
221 return !(i == length);
222}
223
224int i2c_probe (uchar chip)
225{
226 int tmp;
227
228 /*
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.
232 */
233 udelay(10000);
234 return i2c_read (chip, 0, 1, (char *)&tmp, 1);
235}
236
237uchar i2c_reg_read (uchar i2c_addr, uchar reg)
238{
239 char buf[1];
240
241 i2c_read (i2c_addr, reg, 1, buf, 1);
242
243 return (buf[0]);
244}
245
246void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
247{
248 i2c_write (i2c_addr, reg, 1, &val, 1);
249}
250
251#endif /* CONFIG_HARD_I2C */