blob: 4ce695a52d0d9a93d213f3a6f3e572e6ead6bd3b [file] [log] [blame]
Sascha Hauercdace062008-03-26 20:40:49 +01001/*
Marek Vasutdb841402011-09-22 09:22:12 +00002 * i2c driver for Freescale i.MX series
Sascha Hauercdace062008-03-26 20:40:49 +01003 *
4 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Marek Vasutdb841402011-09-22 09:22:12 +00005 * (c) 2011 Marek Vasut <marek.vasut@gmail.com>
6 *
7 * Based on i2c-imx.c from linux kernel:
8 * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de>
9 * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de>
10 * Copyright (C) 2007 RightHand Technologies, Inc.
11 * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
12 *
Sascha Hauercdace062008-03-26 20:40:49 +010013 *
14 * See file CREDITS for list of people who contributed to this
15 * project.
16 *
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.
21 *
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.
26 *
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,
30 * MA 02111-1307 USA
31 */
32
33#include <common.h>
Liu Hui-R64343127cec12011-01-03 22:27:39 +000034#include <asm/arch/clock.h>
Stefano Babic86271112011-03-14 15:43:56 +010035#include <asm/arch/imx-regs.h>
Troy Kiskycea60b02012-07-19 08:18:04 +000036#include <asm/errno.h>
Troy Kisky24cd7382012-07-19 08:18:03 +000037#include <asm/io.h>
Marek Vasutbf0783d2011-10-26 00:05:44 +000038#include <i2c.h>
Sascha Hauercdace062008-03-26 20:40:49 +010039
Marek Vasutdb841402011-09-22 09:22:12 +000040struct mxc_i2c_regs {
41 uint32_t iadr;
42 uint32_t ifdr;
43 uint32_t i2cr;
44 uint32_t i2sr;
45 uint32_t i2dr;
46};
Sascha Hauercdace062008-03-26 20:40:49 +010047
48#define I2CR_IEN (1 << 7)
49#define I2CR_IIEN (1 << 6)
50#define I2CR_MSTA (1 << 5)
51#define I2CR_MTX (1 << 4)
52#define I2CR_TX_NO_AK (1 << 3)
53#define I2CR_RSTA (1 << 2)
54
55#define I2SR_ICF (1 << 7)
56#define I2SR_IBB (1 << 5)
57#define I2SR_IIF (1 << 1)
58#define I2SR_RX_NO_AK (1 << 0)
59
Troy Kiskyde6f6042012-04-24 17:33:25 +000060#ifdef CONFIG_SYS_I2C_BASE
61#define I2C_BASE CONFIG_SYS_I2C_BASE
Sascha Hauercdace062008-03-26 20:40:49 +010062#else
Troy Kiskyde6f6042012-04-24 17:33:25 +000063#error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver"
Sascha Hauercdace062008-03-26 20:40:49 +010064#endif
65
Stefano Babic81687212011-01-20 07:51:31 +000066#define I2C_MAX_TIMEOUT 10000
Sascha Hauercdace062008-03-26 20:40:49 +010067
Marek Vasutdb841402011-09-22 09:22:12 +000068static u16 i2c_clk_div[50][2] = {
69 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
70 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
71 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
72 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
73 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
74 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
75 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
76 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
77 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
78 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
79 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
80 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
81 { 3072, 0x1E }, { 3840, 0x1F }
82};
Sascha Hauercdace062008-03-26 20:40:49 +010083
Marek Vasutdb841402011-09-22 09:22:12 +000084/*
85 * Calculate and set proper clock divider
86 */
Marek Vasutbf0783d2011-10-26 00:05:44 +000087static uint8_t i2c_imx_get_clk(unsigned int rate)
Stefano Babic1d549ad2011-01-20 07:50:44 +000088{
Marek Vasutdb841402011-09-22 09:22:12 +000089 unsigned int i2c_clk_rate;
90 unsigned int div;
Marek Vasutbf0783d2011-10-26 00:05:44 +000091 u8 clk_div;
Sascha Hauercdace062008-03-26 20:40:49 +010092
Liu Hui-R64343127cec12011-01-03 22:27:39 +000093#if defined(CONFIG_MX31)
Stefano Babic1d549ad2011-01-20 07:50:44 +000094 struct clock_control_regs *sc_regs =
95 (struct clock_control_regs *)CCM_BASE;
Marek Vasutdb841402011-09-22 09:22:12 +000096
Guennadi Liakhovetskie7de18a2009-02-13 09:23:36 +010097 /* start the required I2C clock */
Troy Kiskyde6f6042012-04-24 17:33:25 +000098 writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
Stefano Babic1d549ad2011-01-20 07:50:44 +000099 &sc_regs->cgr0);
Liu Hui-R64343127cec12011-01-03 22:27:39 +0000100#endif
Guennadi Liakhovetskie7de18a2009-02-13 09:23:36 +0100101
Marek Vasutdb841402011-09-22 09:22:12 +0000102 /* Divider value calculation */
103 i2c_clk_rate = mxc_get_clock(MXC_IPG_PERCLK);
104 div = (i2c_clk_rate + rate - 1) / rate;
105 if (div < i2c_clk_div[0][0])
Marek Vasutb567b8f2011-09-27 06:34:11 +0000106 clk_div = 0;
Marek Vasutdb841402011-09-22 09:22:12 +0000107 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
Marek Vasutb567b8f2011-09-27 06:34:11 +0000108 clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
Marek Vasutdb841402011-09-22 09:22:12 +0000109 else
Marek Vasutb567b8f2011-09-27 06:34:11 +0000110 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
Marek Vasutdb841402011-09-22 09:22:12 +0000111 ;
Sascha Hauercdace062008-03-26 20:40:49 +0100112
Marek Vasutdb841402011-09-22 09:22:12 +0000113 /* Store divider value */
Marek Vasutbf0783d2011-10-26 00:05:44 +0000114 return clk_div;
Marek Vasutdb841402011-09-22 09:22:12 +0000115}
Sascha Hauercdace062008-03-26 20:40:49 +0100116
Marek Vasutdb841402011-09-22 09:22:12 +0000117/*
118 * Reset I2C Controller
119 */
120void i2c_reset(void)
121{
122 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
123
124 writeb(0, &i2c_regs->i2cr); /* Reset module */
125 writeb(0, &i2c_regs->i2sr);
126}
127
128/*
129 * Init I2C Bus
130 */
131void i2c_init(int speed, int unused)
132{
Marek Vasutbf0783d2011-10-26 00:05:44 +0000133 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
134 u8 clk_idx = i2c_imx_get_clk(speed);
135 u8 idx = i2c_clk_div[clk_idx][1];
136
137 /* Store divider value */
138 writeb(idx, &i2c_regs->ifdr);
139
Stefano Babic1d549ad2011-01-20 07:50:44 +0000140 i2c_reset();
Sascha Hauercdace062008-03-26 20:40:49 +0100141}
142
Marek Vasutdb841402011-09-22 09:22:12 +0000143/*
Marek Vasutb567b8f2011-09-27 06:34:11 +0000144 * Set I2C Speed
145 */
146int i2c_set_bus_speed(unsigned int speed)
147{
148 i2c_init(speed, 0);
149 return 0;
150}
151
152/*
153 * Get I2C Speed
154 */
155unsigned int i2c_get_bus_speed(void)
156{
Marek Vasutbf0783d2011-10-26 00:05:44 +0000157 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
158 u8 clk_idx = readb(&i2c_regs->ifdr);
159 u8 clk_div;
160
161 for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++)
162 ;
163
Marek Vasutb567b8f2011-09-27 06:34:11 +0000164 return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0];
165}
166
167/*
Marek Vasutdb841402011-09-22 09:22:12 +0000168 * Wait for bus to be busy (or free if for_busy = 0)
169 *
170 * for_busy = 1: Wait for IBB to be asserted
171 * for_busy = 0: Wait for IBB to be de-asserted
172 */
173int i2c_imx_bus_busy(int for_busy)
Stefano Babic81687212011-01-20 07:51:31 +0000174{
Marek Vasutdb841402011-09-22 09:22:12 +0000175 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
176 unsigned int temp;
177
Stefano Babic81687212011-01-20 07:51:31 +0000178 int timeout = I2C_MAX_TIMEOUT;
179
Marek Vasutdb841402011-09-22 09:22:12 +0000180 while (timeout--) {
181 temp = readb(&i2c_regs->i2sr);
182
183 if (for_busy && (temp & I2SR_IBB))
184 return 0;
185 if (!for_busy && !(temp & I2SR_IBB))
186 return 0;
187
Stefano Babic81687212011-01-20 07:51:31 +0000188 udelay(1);
189 }
Marek Vasutdb841402011-09-22 09:22:12 +0000190
191 return 1;
Stefano Babic81687212011-01-20 07:51:31 +0000192}
193
Marek Vasutdb841402011-09-22 09:22:12 +0000194/*
195 * Wait for transaction to complete
196 */
197int i2c_imx_trx_complete(void)
Sascha Hauercdace062008-03-26 20:40:49 +0100198{
Marek Vasutdb841402011-09-22 09:22:12 +0000199 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Stefano Babic81687212011-01-20 07:51:31 +0000200 int timeout = I2C_MAX_TIMEOUT;
Sascha Hauercdace062008-03-26 20:40:49 +0100201
Marek Vasutdb841402011-09-22 09:22:12 +0000202 while (timeout--) {
Troy Kiskyea572d82012-07-19 08:18:05 +0000203 if (readb(&i2c_regs->i2sr) & I2SR_IIF)
Marek Vasutdb841402011-09-22 09:22:12 +0000204 return 0;
Sascha Hauercdace062008-03-26 20:40:49 +0100205
Stefano Babic81687212011-01-20 07:51:31 +0000206 udelay(1);
207 }
Stefano Babic81687212011-01-20 07:51:31 +0000208
Troy Kiskycea60b02012-07-19 08:18:04 +0000209 return -ETIMEDOUT;
Stefano Babic81687212011-01-20 07:51:31 +0000210}
211
Troy Kiskycea60b02012-07-19 08:18:04 +0000212static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte)
Sascha Hauercdace062008-03-26 20:40:49 +0100213{
Troy Kiskycea60b02012-07-19 08:18:04 +0000214 int ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100215
Troy Kiskyea572d82012-07-19 08:18:05 +0000216 writeb(0, &i2c_regs->i2sr);
Troy Kiskycea60b02012-07-19 08:18:04 +0000217 writeb(byte, &i2c_regs->i2dr);
218 ret = i2c_imx_trx_complete();
219 if (ret < 0)
220 return ret;
221 ret = readb(&i2c_regs->i2sr);
222 if (ret & I2SR_RX_NO_AK)
223 return -ENODEV;
224 return 0;
Marek Vasutdb841402011-09-22 09:22:12 +0000225}
226
227/*
228 * Start the controller
229 */
230int i2c_imx_start(void)
231{
232 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
233 unsigned int temp = 0;
234 int result;
Marek Vasutdb841402011-09-22 09:22:12 +0000235
236 /* Enable I2C controller */
237 writeb(0, &i2c_regs->i2sr);
238 writeb(I2CR_IEN, &i2c_regs->i2cr);
239
240 /* Wait controller to be stable */
241 udelay(50);
242
243 /* Start I2C transaction */
244 temp = readb(&i2c_regs->i2cr);
245 temp |= I2CR_MSTA;
246 writeb(temp, &i2c_regs->i2cr);
247
248 result = i2c_imx_bus_busy(1);
249 if (result)
250 return result;
251
252 temp |= I2CR_MTX | I2CR_TX_NO_AK;
253 writeb(temp, &i2c_regs->i2cr);
254
Sascha Hauercdace062008-03-26 20:40:49 +0100255 return 0;
256}
257
Marek Vasutdb841402011-09-22 09:22:12 +0000258/*
259 * Stop the controller
260 */
261void i2c_imx_stop(void)
Sascha Hauercdace062008-03-26 20:40:49 +0100262{
Marek Vasutdb841402011-09-22 09:22:12 +0000263 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
264 unsigned int temp = 0;
Sascha Hauercdace062008-03-26 20:40:49 +0100265
Marek Vasutdb841402011-09-22 09:22:12 +0000266 /* Stop I2C transaction */
267 temp = readb(&i2c_regs->i2cr);
Troy Kisky1c076db2012-07-19 08:18:02 +0000268 temp &= ~(I2CR_MSTA | I2CR_MTX);
Marek Vasutdb841402011-09-22 09:22:12 +0000269 writeb(temp, &i2c_regs->i2cr);
Stefano Babic81687212011-01-20 07:51:31 +0000270
Marek Vasutdb841402011-09-22 09:22:12 +0000271 i2c_imx_bus_busy(0);
272
273 /* Disable I2C controller */
274 writeb(0, &i2c_regs->i2cr);
Sascha Hauercdace062008-03-26 20:40:49 +0100275}
276
Marek Vasutdb841402011-09-22 09:22:12 +0000277/*
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000278 * Send start signal, chip address and
279 * write register address
Marek Vasutdb841402011-09-22 09:22:12 +0000280 */
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000281static int i2c_init_transfer(struct mxc_i2c_regs *i2c_regs,
282 uchar chip, uint addr, int alen)
Sascha Hauercdace062008-03-26 20:40:49 +0100283{
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000284 int ret = i2c_imx_start();
285 if (ret)
286 goto exit;
287
288 /* write slave address */
289 ret = tx_byte(i2c_regs, chip << 1);
290 if (ret < 0)
291 goto exit;
Marek Vasutdb841402011-09-22 09:22:12 +0000292
Marek Vasutbf0783d2011-10-26 00:05:44 +0000293 while (alen--) {
Troy Kiskycea60b02012-07-19 08:18:04 +0000294 ret = tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff);
295 if (ret < 0)
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000296 goto exit;
Stefano Babic81687212011-01-20 07:51:31 +0000297 }
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000298 return 0;
299exit:
300 i2c_imx_stop();
Marek Vasutdb841402011-09-22 09:22:12 +0000301 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100302}
303
Marek Vasutdb841402011-09-22 09:22:12 +0000304/*
305 * Try if a chip add given address responds (probe the chip)
306 */
307int i2c_probe(uchar chip)
Sascha Hauercdace062008-03-26 20:40:49 +0100308{
Troy Kiskycea60b02012-07-19 08:18:04 +0000309 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Sascha Hauercdace062008-03-26 20:40:49 +0100310 int ret;
311
Marek Vasutdb841402011-09-22 09:22:12 +0000312 ret = i2c_imx_start();
313 if (ret)
314 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100315
Troy Kiskycea60b02012-07-19 08:18:04 +0000316 ret = tx_byte(i2c_regs, chip << 1);
Marek Vasutdb841402011-09-22 09:22:12 +0000317 i2c_imx_stop();
Marek Vasutdb841402011-09-22 09:22:12 +0000318 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100319}
320
Marek Vasutdb841402011-09-22 09:22:12 +0000321/*
322 * Read data from I2C device
323 */
324int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
325{
326 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
327 int ret;
328 unsigned int temp;
329 int i;
330
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000331 ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
Troy Kiskycea60b02012-07-19 08:18:04 +0000332 if (ret < 0)
Marek Vasutdb841402011-09-22 09:22:12 +0000333 return ret;
334
Marek Vasutdb841402011-09-22 09:22:12 +0000335 temp = readb(&i2c_regs->i2cr);
336 temp |= I2CR_RSTA;
337 writeb(temp, &i2c_regs->i2cr);
338
Troy Kiskycea60b02012-07-19 08:18:04 +0000339 ret = tx_byte(i2c_regs, (chip << 1) | 1);
340 if (ret < 0)
Marek Vasutdb841402011-09-22 09:22:12 +0000341 return ret;
342
343 /* setup bus to read data */
344 temp = readb(&i2c_regs->i2cr);
345 temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
346 if (len == 1)
347 temp |= I2CR_TX_NO_AK;
348 writeb(temp, &i2c_regs->i2cr);
Troy Kiskyea572d82012-07-19 08:18:05 +0000349 writeb(0, &i2c_regs->i2sr);
350 readb(&i2c_regs->i2dr); /* dummy read to clear ICF */
Marek Vasutdb841402011-09-22 09:22:12 +0000351
352 /* read data */
353 for (i = 0; i < len; i++) {
354 ret = i2c_imx_trx_complete();
355 if (ret)
356 return ret;
357
358 /*
359 * It must generate STOP before read I2DR to prevent
360 * controller from generating another clock cycle
361 */
362 if (i == (len - 1)) {
363 temp = readb(&i2c_regs->i2cr);
364 temp &= ~(I2CR_MSTA | I2CR_MTX);
365 writeb(temp, &i2c_regs->i2cr);
366 i2c_imx_bus_busy(0);
367 } else if (i == (len - 2)) {
368 temp = readb(&i2c_regs->i2cr);
369 temp |= I2CR_TX_NO_AK;
370 writeb(temp, &i2c_regs->i2cr);
371 }
372
Troy Kiskyea572d82012-07-19 08:18:05 +0000373 writeb(0, &i2c_regs->i2sr);
Marek Vasutdb841402011-09-22 09:22:12 +0000374 buf[i] = readb(&i2c_regs->i2dr);
375 }
376
377 i2c_imx_stop();
378
379 return ret;
380}
381
382/*
383 * Write data to I2C device
384 */
Sascha Hauercdace062008-03-26 20:40:49 +0100385int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
386{
Marek Vasutdb841402011-09-22 09:22:12 +0000387 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
388 int ret;
389 int i;
Sascha Hauercdace062008-03-26 20:40:49 +0100390
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000391 ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
Troy Kiskycea60b02012-07-19 08:18:04 +0000392 if (ret < 0)
Marek Vasutdb841402011-09-22 09:22:12 +0000393 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100394
Marek Vasutdb841402011-09-22 09:22:12 +0000395 for (i = 0; i < len; i++) {
Troy Kiskycea60b02012-07-19 08:18:04 +0000396 ret = tx_byte(i2c_regs, buf[i]);
397 if (ret < 0)
Marek Vasutdb841402011-09-22 09:22:12 +0000398 return ret;
399 }
400
401 i2c_imx_stop();
402
403 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100404}