]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/i2c/mxc_i2c.c
applied patches from Freescale and Ka-Ro
[karo-tx-uboot.git] / drivers / i2c / mxc_i2c.c
1 /*
2  * i2c driver for Freescale mx31
3  *
4  * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
5  *
6  * (C) Copyright 2008-2010 Freescale Semiconductor, Inc.
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 #include <common.h>
28
29 #if defined(CONFIG_HARD_I2C)
30
31 #define IADR    0x00
32 #define IFDR    0x04
33 #define I2CR    0x08
34 #define I2SR    0x0c
35 #define I2DR    0x10
36
37 #define I2CR_IEN        (1 << 7)
38 #define I2CR_IIEN       (1 << 6)
39 #define I2CR_MSTA       (1 << 5)
40 #define I2CR_MTX        (1 << 4)
41 #define I2CR_TX_NO_AK   (1 << 3)
42 #define I2CR_RSTA       (1 << 2)
43
44 #define I2SR_ICF        (1 << 7)
45 #define I2SR_IBB        (1 << 5)
46 #define I2SR_IIF        (1 << 1)
47 #define I2SR_RX_NO_AK   (1 << 0)
48
49 #ifdef CONFIG_SYS_I2C_PORT
50 # define I2C_BASE       CONFIG_SYS_I2C_PORT
51 #else
52 # error "define CONFIG_SYS_I2C_PORT(I2C base address) to use the I2C driver"
53 #endif
54
55 #define I2C_MAX_TIMEOUT         100000
56 #define I2C_TIMEOUT_TICKET      1
57
58 #undef DEBUG
59
60 #ifdef DEBUG
61 #define DPRINTF(args...)  printf(args)
62 #else
63 #define DPRINTF(args...)
64 #endif
65
66 static u16 div[] = { 30, 32, 36, 42, 48, 52, 60, 72, 80, 88, 104, 128, 144,
67         160, 192, 240, 288, 320, 384, 480, 576, 640, 768, 960,
68         1152, 1280, 1536, 1920, 2304, 2560, 3072, 3840
69 };
70
71 static inline void i2c_reset(void)
72 {
73         __REG16(I2C_BASE + I2CR) = 0;   /* Reset module */
74         __REG16(I2C_BASE + I2SR) = 0;
75         __REG16(I2C_BASE + I2CR) = I2CR_IEN;
76 }
77
78 void i2c_init(int speed, int unused)
79 {
80         int freq;
81         int i;
82
83 #ifdef CONFIG_MX31
84         freq = mx31_get_ipg_clk();
85 #else
86         freq = mxc_get_clock(MXC_IPG_PERCLK);
87 #endif
88         for (i = 0; i < 0x1f; i++)
89                 if (freq / div[i] <= speed)
90                         break;
91
92         DPRINTF("%s: root clock: %d, speed: %d div: %x\n",
93                 __func__, freq, speed, i);
94
95         __REG16(I2C_BASE + IFDR) = i;
96         i2c_reset();
97 }
98
99 static int wait_idle(void)
100 {
101         int timeout = I2C_MAX_TIMEOUT;
102
103         while ((__REG16(I2C_BASE + I2SR) & I2SR_IBB) && --timeout) {
104                 __REG16(I2C_BASE + I2SR) = 0;
105                 udelay(I2C_TIMEOUT_TICKET);
106         }
107         DPRINTF("%s:%x\n", __func__, __REG16(I2C_BASE + I2SR));
108         return timeout ? timeout : (!(__REG16(I2C_BASE + I2SR) & I2SR_IBB));
109 }
110
111 static int wait_busy(void)
112 {
113         int timeout = I2C_MAX_TIMEOUT;
114
115         while ((!(__REG16(I2C_BASE + I2SR) & I2SR_IBB) && (--timeout))) {
116                 __REG16(I2C_BASE + I2SR) = 0;
117                 udelay(I2C_TIMEOUT_TICKET);
118         }
119         return timeout ? timeout : (__REG16(I2C_BASE + I2SR) & I2SR_IBB);
120 }
121
122 static int wait_complete(void)
123 {
124         int timeout = I2C_MAX_TIMEOUT;
125
126         while ((!(__REG16(I2C_BASE + I2SR) & I2SR_ICF)) && (--timeout)) {
127                 __REG16(I2C_BASE + I2SR) = 0;
128                 udelay(I2C_TIMEOUT_TICKET);
129         }
130         DPRINTF("%s:%x\n", __func__, __REG16(I2C_BASE + I2SR));
131         {
132                 int i;
133                 for (i = 0; i < 200; i++)
134                         udelay(10);
135
136         }
137         __REG16(I2C_BASE + I2SR) = 0;   /* clear interrupt */
138
139         return timeout;
140 }
141
142 static int tx_byte(u8 byte)
143 {
144         __REG16(I2C_BASE + I2DR) = byte;
145
146         if (!wait_complete() || __REG16(I2C_BASE + I2SR) & I2SR_RX_NO_AK) {
147                 DPRINTF("%s:%x <= %x\n", __func__, __REG16(I2C_BASE + I2SR),
148                         byte);
149                 return -1;
150         }
151         DPRINTF("%s:%x\n", __func__, byte);
152         return 0;
153 }
154
155 static int rx_byte(u32 *pdata, int last)
156 {
157         if (!wait_complete())
158                 return -1;
159
160         if (last)
161                 __REG16(I2C_BASE + I2CR) = I2CR_IEN;
162
163         *pdata = __REG16(I2C_BASE + I2DR);
164         DPRINTF("%s:%x\n", __func__, *pdata);
165         return 0;
166 }
167
168 int i2c_probe(uchar chip)
169 {
170         int ret;
171
172         __REG16(I2C_BASE + I2CR) = 0;   /* Reset module */
173         __REG16(I2C_BASE + I2CR) = I2CR_IEN;
174         for (ret = 0; ret < 1000; ret++)
175                 udelay(1);
176         __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA | I2CR_MTX;
177         ret = tx_byte(chip << 1);
178         __REG16(I2C_BASE + I2CR) = I2CR_IEN;
179
180         return ret;
181 }
182
183 static int i2c_addr(uchar chip, uint addr, int alen)
184 {
185         int i, retry = 0;
186         for (retry = 0; retry < 3; retry++) {
187                 if (wait_idle())
188                         break;
189                 i2c_reset();
190                 for (i = 0; i < I2C_MAX_TIMEOUT; i++)
191                         udelay(I2C_TIMEOUT_TICKET);
192         }
193         if (retry >= 3) {
194                 printf("%s:bus is busy(%x)\n",
195                        __func__, __REG16(I2C_BASE + I2SR));
196                 return -1;
197         }
198         __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA | I2CR_MTX;
199         if (!wait_busy()) {
200                 printf("%s:trigger start fail(%x)\n",
201                        __func__, __REG16(I2C_BASE + I2SR));
202                 return -1;
203         }
204         if (tx_byte(chip << 1) || (__REG16(I2C_BASE + I2SR) & I2SR_RX_NO_AK)) {
205                 printf("%s:chip address cycle fail(%x)\n",
206                        __func__, __REG16(I2C_BASE + I2SR));
207                 return -1;
208         }
209         while (alen--)
210                 if (tx_byte((addr >> (alen * 8)) & 0xff) ||
211                     (__REG16(I2C_BASE + I2SR) & I2SR_RX_NO_AK)) {
212                         printf("%s:device address cycle fail(%x)\n",
213                                __func__, __REG16(I2C_BASE + I2SR));
214                         return -1;
215                 }
216         return 0;
217 }
218
219 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
220 {
221         int timeout = I2C_MAX_TIMEOUT;
222         uint ret;
223
224         DPRINTF("%s chip: 0x%02x addr: 0x%04x alen: %d len: %d\n",
225                 __func__, chip, addr, alen, len);
226
227         if (i2c_addr(chip, addr, alen)) {
228                 printf("i2c_addr failed\n");
229                 return -1;
230         }
231
232         __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA | I2CR_MTX | I2CR_RSTA;
233
234         if (tx_byte(chip << 1 | 1) ||
235             (__REG16(I2C_BASE + I2SR) & I2SR_RX_NO_AK)) {
236                 printf("%s:Send 2th chip address fail(%x)\n",
237                        __func__, __REG16(I2C_BASE + I2SR));
238                 return -1;
239         }
240         __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA |
241             ((len == 1) ? I2CR_TX_NO_AK : 0);
242         DPRINTF("CR=%x\n", __REG16(I2C_BASE + I2CR));
243         ret = __REG16(I2C_BASE + I2DR);
244
245         while (len--) {
246                 if (len == 0)
247                         __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA |
248                             I2CR_TX_NO_AK;
249
250                 if (rx_byte(&ret, len == 0) < 0) {
251                         printf("Read: rx_byte fail\n");
252                         return -1;
253                 }
254                 *buf++ = ret;
255         }
256
257         while (__REG16(I2C_BASE + I2SR) & I2SR_IBB && --timeout) {
258                 __REG16(I2C_BASE + I2SR) = 0;
259                 udelay(I2C_TIMEOUT_TICKET);
260         }
261         if (!timeout) {
262                 printf("%s:trigger stop fail(%x)\n",
263                        __func__, __REG16(I2C_BASE + I2SR));
264         }
265         return 0;
266 }
267
268 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
269 {
270         int timeout = I2C_MAX_TIMEOUT;
271         DPRINTF("%s chip: 0x%02x addr: 0x%04x alen: %d len: %d\n",
272                 __func__, chip, addr, alen, len);
273
274         if (i2c_addr(chip, addr, alen))
275                 return -1;
276
277         while (len--)
278                 if (tx_byte(*buf++))
279                         return -1;
280
281         __REG16(I2C_BASE + I2CR) = I2CR_IEN;
282
283         while (__REG16(I2C_BASE + I2SR) & I2SR_IBB && --timeout)
284                 udelay(I2C_TIMEOUT_TICKET);
285
286         return 0;
287 }
288
289 #endif                          /* CONFIG_HARD_I2C */