blob: 1a5e379c2f14313a554cc5e06b4f97f042b2950c [file] [log] [blame]
Sascha Hauer15ea70f2008-03-26 20:40:49 +01001/*
Marek Vasut94cb8422011-09-22 09:22:12 +00002 * i2c driver for Freescale i.MX series
Sascha Hauer15ea70f2008-03-26 20:40:49 +01003 *
4 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Marek Vasut94cb8422011-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 Hauer15ea70f2008-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-R64343447beb12011-01-03 22:27:39 +000034#include <asm/arch/clock.h>
Stefano Babic78129d92011-03-14 15:43:56 +010035#include <asm/arch/imx-regs.h>
Troy Kisky752ac8f2012-07-19 08:18:04 +000036#include <asm/errno.h>
Troy Kisky2254b7f2012-07-19 08:18:03 +000037#include <asm/io.h>
Marek Vasut5f1291e2011-10-26 00:05:44 +000038#include <i2c.h>
Troy Kiskyf024a3b2012-07-19 08:18:09 +000039#include <watchdog.h>
Sascha Hauer15ea70f2008-03-26 20:40:49 +010040
Marek Vasut94cb8422011-09-22 09:22:12 +000041struct mxc_i2c_regs {
42 uint32_t iadr;
43 uint32_t ifdr;
44 uint32_t i2cr;
45 uint32_t i2sr;
46 uint32_t i2dr;
47};
Sascha Hauer15ea70f2008-03-26 20:40:49 +010048
49#define I2CR_IEN (1 << 7)
50#define I2CR_IIEN (1 << 6)
51#define I2CR_MSTA (1 << 5)
52#define I2CR_MTX (1 << 4)
53#define I2CR_TX_NO_AK (1 << 3)
54#define I2CR_RSTA (1 << 2)
55
56#define I2SR_ICF (1 << 7)
57#define I2SR_IBB (1 << 5)
58#define I2SR_IIF (1 << 1)
59#define I2SR_RX_NO_AK (1 << 0)
60
Troy Kisky8462c632012-04-24 17:33:25 +000061#ifdef CONFIG_SYS_I2C_BASE
62#define I2C_BASE CONFIG_SYS_I2C_BASE
Sascha Hauer15ea70f2008-03-26 20:40:49 +010063#else
Troy Kisky8462c632012-04-24 17:33:25 +000064#error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver"
Sascha Hauer15ea70f2008-03-26 20:40:49 +010065#endif
66
Marek Vasut94cb8422011-09-22 09:22:12 +000067static u16 i2c_clk_div[50][2] = {
68 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
69 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
70 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
71 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
72 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
73 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
74 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
75 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
76 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
77 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
78 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
79 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
80 { 3072, 0x1E }, { 3840, 0x1F }
81};
Sascha Hauer15ea70f2008-03-26 20:40:49 +010082
Marek Vasut94cb8422011-09-22 09:22:12 +000083/*
84 * Calculate and set proper clock divider
85 */
Marek Vasut5f1291e2011-10-26 00:05:44 +000086static uint8_t i2c_imx_get_clk(unsigned int rate)
Sascha Hauer15ea70f2008-03-26 20:40:49 +010087{
Marek Vasut94cb8422011-09-22 09:22:12 +000088 unsigned int i2c_clk_rate;
89 unsigned int div;
Marek Vasut5f1291e2011-10-26 00:05:44 +000090 u8 clk_div;
Sascha Hauer15ea70f2008-03-26 20:40:49 +010091
Liu Hui-R64343447beb12011-01-03 22:27:39 +000092#if defined(CONFIG_MX31)
Stefano Babic22121722011-01-20 07:50:44 +000093 struct clock_control_regs *sc_regs =
94 (struct clock_control_regs *)CCM_BASE;
Marek Vasut94cb8422011-09-22 09:22:12 +000095
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +010096 /* start the required I2C clock */
Troy Kisky8462c632012-04-24 17:33:25 +000097 writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
Stefano Babic22121722011-01-20 07:50:44 +000098 &sc_regs->cgr0);
Liu Hui-R64343447beb12011-01-03 22:27:39 +000099#endif
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +0100100
Marek Vasut94cb8422011-09-22 09:22:12 +0000101 /* Divider value calculation */
102 i2c_clk_rate = mxc_get_clock(MXC_IPG_PERCLK);
103 div = (i2c_clk_rate + rate - 1) / rate;
104 if (div < i2c_clk_div[0][0])
Marek Vasut4f274442011-09-27 06:34:11 +0000105 clk_div = 0;
Marek Vasut94cb8422011-09-22 09:22:12 +0000106 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
Marek Vasut4f274442011-09-27 06:34:11 +0000107 clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
Marek Vasut94cb8422011-09-22 09:22:12 +0000108 else
Marek Vasut4f274442011-09-27 06:34:11 +0000109 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
Marek Vasut94cb8422011-09-22 09:22:12 +0000110 ;
111
112 /* Store divider value */
Marek Vasut5f1291e2011-10-26 00:05:44 +0000113 return clk_div;
Marek Vasut94cb8422011-09-22 09:22:12 +0000114}
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100115
Marek Vasut94cb8422011-09-22 09:22:12 +0000116/*
Marek Vasut94cb8422011-09-22 09:22:12 +0000117 * Init I2C Bus
118 */
119void i2c_init(int speed, int unused)
120{
Marek Vasut5f1291e2011-10-26 00:05:44 +0000121 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
122 u8 clk_idx = i2c_imx_get_clk(speed);
123 u8 idx = i2c_clk_div[clk_idx][1];
124
125 /* Store divider value */
126 writeb(idx, &i2c_regs->ifdr);
127
Troy Kiskye6fa4d72012-07-19 08:18:12 +0000128 /* Reset module */
129 writeb(0, &i2c_regs->i2cr);
130 writeb(0, &i2c_regs->i2sr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100131}
132
Marek Vasut94cb8422011-09-22 09:22:12 +0000133/*
Marek Vasut4f274442011-09-27 06:34:11 +0000134 * Set I2C Speed
135 */
136int i2c_set_bus_speed(unsigned int speed)
137{
138 i2c_init(speed, 0);
139 return 0;
140}
141
142/*
143 * Get I2C Speed
144 */
145unsigned int i2c_get_bus_speed(void)
146{
Marek Vasut5f1291e2011-10-26 00:05:44 +0000147 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
148 u8 clk_idx = readb(&i2c_regs->ifdr);
149 u8 clk_div;
150
151 for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++)
152 ;
153
Marek Vasut4f274442011-09-27 06:34:11 +0000154 return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0];
155}
156
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000157#define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
158#define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
159#define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
Stefano Babic848bb992011-01-20 07:51:31 +0000160
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000161static int wait_for_sr_state(struct mxc_i2c_regs *i2c_regs, unsigned state)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100162{
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000163 unsigned sr;
164 ulong elapsed;
165 ulong start_time = get_timer(0);
166 for (;;) {
167 sr = readb(&i2c_regs->i2sr);
168 if ((sr & (state >> 8)) == (unsigned char)state)
169 return sr;
170 WATCHDOG_RESET();
171 elapsed = get_timer(start_time);
172 if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
173 break;
Marek Vasut94cb8422011-09-22 09:22:12 +0000174 }
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000175 printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
176 sr, readb(&i2c_regs->i2cr), state);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000177 return -ETIMEDOUT;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100178}
179
Troy Kisky752ac8f2012-07-19 08:18:04 +0000180static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte)
Stefano Babic848bb992011-01-20 07:51:31 +0000181{
Troy Kisky752ac8f2012-07-19 08:18:04 +0000182 int ret;
Stefano Babic848bb992011-01-20 07:51:31 +0000183
Troy Kisky30fa77c2012-07-19 08:18:05 +0000184 writeb(0, &i2c_regs->i2sr);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000185 writeb(byte, &i2c_regs->i2dr);
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000186 ret = wait_for_sr_state(i2c_regs, ST_IIF);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000187 if (ret < 0)
188 return ret;
Troy Kisky752ac8f2012-07-19 08:18:04 +0000189 if (ret & I2SR_RX_NO_AK)
190 return -ENODEV;
191 return 0;
Marek Vasut94cb8422011-09-22 09:22:12 +0000192}
Stefano Babic848bb992011-01-20 07:51:31 +0000193
Marek Vasut94cb8422011-09-22 09:22:12 +0000194/*
Troy Kiskyfef163f2012-07-19 08:18:13 +0000195 * Stop I2C transaction
Marek Vasut94cb8422011-09-22 09:22:12 +0000196 */
197void i2c_imx_stop(void)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100198{
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000199 int ret;
Marek Vasut94cb8422011-09-22 09:22:12 +0000200 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Troy Kiskyfef163f2012-07-19 08:18:13 +0000201 unsigned int temp = readb(&i2c_regs->i2cr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100202
Troy Kisky1ac1e452012-07-19 08:18:02 +0000203 temp &= ~(I2CR_MSTA | I2CR_MTX);
Marek Vasut94cb8422011-09-22 09:22:12 +0000204 writeb(temp, &i2c_regs->i2cr);
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000205 ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE);
206 if (ret < 0)
207 printf("%s:trigger stop failed\n", __func__);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100208}
209
Marek Vasut94cb8422011-09-22 09:22:12 +0000210/*
Troy Kisky14db6f22012-07-19 08:18:06 +0000211 * Send start signal, chip address and
212 * write register address
Marek Vasut94cb8422011-09-22 09:22:12 +0000213 */
Troy Kisky14db6f22012-07-19 08:18:06 +0000214static int i2c_init_transfer(struct mxc_i2c_regs *i2c_regs,
215 uchar chip, uint addr, int alen)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100216{
Troy Kiskya974bcc2012-07-19 08:18:11 +0000217 unsigned int temp;
218 int ret;
219
220 /* Enable I2C controller */
Troy Kiskyfef163f2012-07-19 08:18:13 +0000221 if (!(readb(&i2c_regs->i2cr) & I2CR_IEN)) {
222 writeb(I2CR_IEN, &i2c_regs->i2cr);
223 /* Wait for controller to be stable */
224 udelay(50);
225 }
Troy Kiskya974bcc2012-07-19 08:18:11 +0000226 writeb(0, &i2c_regs->i2sr);
Troy Kiskyfef163f2012-07-19 08:18:13 +0000227 ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE);
228 if (ret < 0)
229 goto exit;
Troy Kiskya974bcc2012-07-19 08:18:11 +0000230
231 /* Start I2C transaction */
232 temp = readb(&i2c_regs->i2cr);
233 temp |= I2CR_MSTA;
234 writeb(temp, &i2c_regs->i2cr);
235
236 ret = wait_for_sr_state(i2c_regs, ST_BUS_BUSY);
237 if (ret < 0)
Troy Kisky14db6f22012-07-19 08:18:06 +0000238 goto exit;
239
Troy Kiskya974bcc2012-07-19 08:18:11 +0000240 temp |= I2CR_MTX | I2CR_TX_NO_AK;
241 writeb(temp, &i2c_regs->i2cr);
242
Troy Kisky14db6f22012-07-19 08:18:06 +0000243 /* write slave address */
244 ret = tx_byte(i2c_regs, chip << 1);
245 if (ret < 0)
246 goto exit;
Marek Vasut94cb8422011-09-22 09:22:12 +0000247
Marek Vasut5f1291e2011-10-26 00:05:44 +0000248 while (alen--) {
Troy Kisky752ac8f2012-07-19 08:18:04 +0000249 ret = tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff);
250 if (ret < 0)
Troy Kisky14db6f22012-07-19 08:18:06 +0000251 goto exit;
Stefano Babic848bb992011-01-20 07:51:31 +0000252 }
Troy Kisky14db6f22012-07-19 08:18:06 +0000253 return 0;
254exit:
255 i2c_imx_stop();
Troy Kiskyfef163f2012-07-19 08:18:13 +0000256 /* Disable I2C controller */
257 writeb(0, &i2c_regs->i2cr);
Marek Vasut94cb8422011-09-22 09:22:12 +0000258 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100259}
260
Marek Vasut94cb8422011-09-22 09:22:12 +0000261/*
Marek Vasut94cb8422011-09-22 09:22:12 +0000262 * Read data from I2C device
263 */
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100264int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
265{
Marek Vasut94cb8422011-09-22 09:22:12 +0000266 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100267 int ret;
Marek Vasut94cb8422011-09-22 09:22:12 +0000268 unsigned int temp;
269 int i;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100270
Troy Kisky14db6f22012-07-19 08:18:06 +0000271 ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000272 if (ret < 0)
Marek Vasut94cb8422011-09-22 09:22:12 +0000273 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100274
Marek Vasut94cb8422011-09-22 09:22:12 +0000275 temp = readb(&i2c_regs->i2cr);
276 temp |= I2CR_RSTA;
277 writeb(temp, &i2c_regs->i2cr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100278
Troy Kisky752ac8f2012-07-19 08:18:04 +0000279 ret = tx_byte(i2c_regs, (chip << 1) | 1);
Troy Kisky0ce898d2012-07-19 08:18:07 +0000280 if (ret < 0) {
281 i2c_imx_stop();
Marek Vasut94cb8422011-09-22 09:22:12 +0000282 return ret;
Troy Kisky0ce898d2012-07-19 08:18:07 +0000283 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100284
Marek Vasut94cb8422011-09-22 09:22:12 +0000285 /* setup bus to read data */
286 temp = readb(&i2c_regs->i2cr);
287 temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
288 if (len == 1)
289 temp |= I2CR_TX_NO_AK;
290 writeb(temp, &i2c_regs->i2cr);
Troy Kisky30fa77c2012-07-19 08:18:05 +0000291 writeb(0, &i2c_regs->i2sr);
292 readb(&i2c_regs->i2dr); /* dummy read to clear ICF */
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100293
Marek Vasut94cb8422011-09-22 09:22:12 +0000294 /* read data */
295 for (i = 0; i < len; i++) {
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000296 ret = wait_for_sr_state(i2c_regs, ST_IIF);
297 if (ret < 0) {
Troy Kisky0ce898d2012-07-19 08:18:07 +0000298 i2c_imx_stop();
Marek Vasut94cb8422011-09-22 09:22:12 +0000299 return ret;
Troy Kisky0ce898d2012-07-19 08:18:07 +0000300 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100301
Marek Vasut94cb8422011-09-22 09:22:12 +0000302 /*
303 * It must generate STOP before read I2DR to prevent
304 * controller from generating another clock cycle
305 */
306 if (i == (len - 1)) {
Troy Kiskyfef163f2012-07-19 08:18:13 +0000307 i2c_imx_stop();
Marek Vasut94cb8422011-09-22 09:22:12 +0000308 } else if (i == (len - 2)) {
309 temp = readb(&i2c_regs->i2cr);
310 temp |= I2CR_TX_NO_AK;
311 writeb(temp, &i2c_regs->i2cr);
312 }
Troy Kisky30fa77c2012-07-19 08:18:05 +0000313 writeb(0, &i2c_regs->i2sr);
Marek Vasut94cb8422011-09-22 09:22:12 +0000314 buf[i] = readb(&i2c_regs->i2dr);
315 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100316
Marek Vasut94cb8422011-09-22 09:22:12 +0000317 i2c_imx_stop();
318
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000319 return 0;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100320}
321
Marek Vasut94cb8422011-09-22 09:22:12 +0000322/*
323 * Write data to I2C device
324 */
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100325int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
326{
Marek Vasut94cb8422011-09-22 09:22:12 +0000327 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
328 int ret;
329 int i;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100330
Troy Kisky14db6f22012-07-19 08:18:06 +0000331 ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000332 if (ret < 0)
Marek Vasut94cb8422011-09-22 09:22:12 +0000333 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100334
Marek Vasut94cb8422011-09-22 09:22:12 +0000335 for (i = 0; i < len; i++) {
Troy Kisky752ac8f2012-07-19 08:18:04 +0000336 ret = tx_byte(i2c_regs, buf[i]);
337 if (ret < 0)
Troy Kisky0ce898d2012-07-19 08:18:07 +0000338 break;
Marek Vasut94cb8422011-09-22 09:22:12 +0000339 }
340
341 i2c_imx_stop();
342
343 return ret;
344}
Troy Kisky321a42b2012-07-19 08:18:08 +0000345
346/*
347 * Test if a chip at a given address responds (probe the chip)
348 */
349int i2c_probe(uchar chip)
350{
351 return i2c_write(chip, 0, 0, NULL, 0);
352}