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