blob: a73b10b9c49f662c167276f3d97abcc7517e75dd [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)
Troy Kisky8ff683a2012-07-19 08:18:15 +000058#define I2SR_IAL (1 << 4)
Sascha Hauer15ea70f2008-03-26 20:40:49 +010059#define I2SR_IIF (1 << 1)
60#define I2SR_RX_NO_AK (1 << 0)
61
Troy Kiskyae447602012-07-19 08:18:18 +000062#if defined(CONFIG_HARD_I2C) && !defined(CONFIG_SYS_I2C_BASE)
Troy Kisky8462c632012-04-24 17:33:25 +000063#error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver"
Sascha Hauer15ea70f2008-03-26 20:40:49 +010064#endif
65
Marek Vasut94cb8422011-09-22 09:22:12 +000066static u16 i2c_clk_div[50][2] = {
67 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
68 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
69 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
70 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
71 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
72 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
73 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
74 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
75 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
76 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
77 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
78 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
79 { 3072, 0x1E }, { 3840, 0x1F }
80};
Sascha Hauer15ea70f2008-03-26 20:40:49 +010081
Marek Vasut94cb8422011-09-22 09:22:12 +000082/*
83 * Calculate and set proper clock divider
84 */
Marek Vasut5f1291e2011-10-26 00:05:44 +000085static uint8_t i2c_imx_get_clk(unsigned int rate)
Sascha Hauer15ea70f2008-03-26 20:40:49 +010086{
Marek Vasut94cb8422011-09-22 09:22:12 +000087 unsigned int i2c_clk_rate;
88 unsigned int div;
Marek Vasut5f1291e2011-10-26 00:05:44 +000089 u8 clk_div;
Sascha Hauer15ea70f2008-03-26 20:40:49 +010090
Liu Hui-R64343447beb12011-01-03 22:27:39 +000091#if defined(CONFIG_MX31)
Stefano Babic22121722011-01-20 07:50:44 +000092 struct clock_control_regs *sc_regs =
93 (struct clock_control_regs *)CCM_BASE;
Marek Vasut94cb8422011-09-22 09:22:12 +000094
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +010095 /* start the required I2C clock */
Troy Kisky8462c632012-04-24 17:33:25 +000096 writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
Stefano Babic22121722011-01-20 07:50:44 +000097 &sc_regs->cgr0);
Liu Hui-R64343447beb12011-01-03 22:27:39 +000098#endif
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +010099
Marek Vasut94cb8422011-09-22 09:22:12 +0000100 /* Divider value calculation */
Matthias Weisser99ba3422012-09-24 02:46:53 +0000101 i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK);
Marek Vasut94cb8422011-09-22 09:22:12 +0000102 div = (i2c_clk_rate + rate - 1) / rate;
103 if (div < i2c_clk_div[0][0])
Marek Vasut4f274442011-09-27 06:34:11 +0000104 clk_div = 0;
Marek Vasut94cb8422011-09-22 09:22:12 +0000105 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
Marek Vasut4f274442011-09-27 06:34:11 +0000106 clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
Marek Vasut94cb8422011-09-22 09:22:12 +0000107 else
Marek Vasut4f274442011-09-27 06:34:11 +0000108 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
Marek Vasut94cb8422011-09-22 09:22:12 +0000109 ;
110
111 /* Store divider value */
Marek Vasut5f1291e2011-10-26 00:05:44 +0000112 return clk_div;
Marek Vasut94cb8422011-09-22 09:22:12 +0000113}
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100114
Marek Vasut94cb8422011-09-22 09:22:12 +0000115/*
Troy Kiskyae447602012-07-19 08:18:18 +0000116 * Set I2C Bus speed
Marek Vasut94cb8422011-09-22 09:22:12 +0000117 */
Marek Vasutae611462012-11-12 14:34:26 +0000118static int bus_i2c_set_bus_speed(void *base, int speed)
Marek Vasut94cb8422011-09-22 09:22:12 +0000119{
Troy Kiskyae447602012-07-19 08:18:18 +0000120 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)base;
Marek Vasut5f1291e2011-10-26 00:05:44 +0000121 u8 clk_idx = i2c_imx_get_clk(speed);
122 u8 idx = i2c_clk_div[clk_idx][1];
123
124 /* Store divider value */
125 writeb(idx, &i2c_regs->ifdr);
126
Troy Kiskye6fa4d72012-07-19 08:18:12 +0000127 /* Reset module */
128 writeb(0, &i2c_regs->i2cr);
129 writeb(0, &i2c_regs->i2sr);
Marek Vasut4f274442011-09-27 06:34:11 +0000130 return 0;
131}
132
133/*
134 * Get I2C Speed
135 */
Marek Vasutae611462012-11-12 14:34:26 +0000136static unsigned int bus_i2c_get_bus_speed(void *base)
Marek Vasut4f274442011-09-27 06:34:11 +0000137{
Troy Kiskyae447602012-07-19 08:18:18 +0000138 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)base;
Marek Vasut5f1291e2011-10-26 00:05:44 +0000139 u8 clk_idx = readb(&i2c_regs->ifdr);
140 u8 clk_div;
141
142 for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++)
143 ;
144
Matthias Weisser99ba3422012-09-24 02:46:53 +0000145 return mxc_get_clock(MXC_I2C_CLK) / i2c_clk_div[clk_div][0];
Marek Vasut4f274442011-09-27 06:34:11 +0000146}
147
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000148#define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
149#define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
150#define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
Stefano Babic848bb992011-01-20 07:51:31 +0000151
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000152static int wait_for_sr_state(struct mxc_i2c_regs *i2c_regs, unsigned state)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100153{
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000154 unsigned sr;
155 ulong elapsed;
156 ulong start_time = get_timer(0);
157 for (;;) {
158 sr = readb(&i2c_regs->i2sr);
Troy Kisky8ff683a2012-07-19 08:18:15 +0000159 if (sr & I2SR_IAL) {
160 writeb(sr & ~I2SR_IAL, &i2c_regs->i2sr);
161 printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
162 __func__, sr, readb(&i2c_regs->i2cr), state);
163 return -ERESTART;
164 }
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000165 if ((sr & (state >> 8)) == (unsigned char)state)
166 return sr;
167 WATCHDOG_RESET();
168 elapsed = get_timer(start_time);
169 if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
170 break;
Marek Vasut94cb8422011-09-22 09:22:12 +0000171 }
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000172 printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
173 sr, readb(&i2c_regs->i2cr), state);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000174 return -ETIMEDOUT;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100175}
176
Troy Kisky752ac8f2012-07-19 08:18:04 +0000177static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte)
Stefano Babic848bb992011-01-20 07:51:31 +0000178{
Troy Kisky752ac8f2012-07-19 08:18:04 +0000179 int ret;
Stefano Babic848bb992011-01-20 07:51:31 +0000180
Troy Kisky30fa77c2012-07-19 08:18:05 +0000181 writeb(0, &i2c_regs->i2sr);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000182 writeb(byte, &i2c_regs->i2dr);
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000183 ret = wait_for_sr_state(i2c_regs, ST_IIF);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000184 if (ret < 0)
185 return ret;
Troy Kisky752ac8f2012-07-19 08:18:04 +0000186 if (ret & I2SR_RX_NO_AK)
187 return -ENODEV;
188 return 0;
Marek Vasut94cb8422011-09-22 09:22:12 +0000189}
Stefano Babic848bb992011-01-20 07:51:31 +0000190
Marek Vasut94cb8422011-09-22 09:22:12 +0000191/*
Troy Kiskyfef163f2012-07-19 08:18:13 +0000192 * Stop I2C transaction
Marek Vasut94cb8422011-09-22 09:22:12 +0000193 */
Troy Kiskya2d15da2012-07-19 08:18:17 +0000194static void i2c_imx_stop(struct mxc_i2c_regs *i2c_regs)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100195{
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000196 int ret;
Troy Kiskyfef163f2012-07-19 08:18:13 +0000197 unsigned int temp = readb(&i2c_regs->i2cr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100198
Troy Kisky1ac1e452012-07-19 08:18:02 +0000199 temp &= ~(I2CR_MSTA | I2CR_MTX);
Marek Vasut94cb8422011-09-22 09:22:12 +0000200 writeb(temp, &i2c_regs->i2cr);
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000201 ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE);
202 if (ret < 0)
203 printf("%s:trigger stop failed\n", __func__);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100204}
205
Marek Vasut94cb8422011-09-22 09:22:12 +0000206/*
Troy Kisky14db6f22012-07-19 08:18:06 +0000207 * Send start signal, chip address and
208 * write register address
Marek Vasut94cb8422011-09-22 09:22:12 +0000209 */
Troy Kiskyeca037a2012-07-19 08:18:16 +0000210static int i2c_init_transfer_(struct mxc_i2c_regs *i2c_regs,
Troy Kisky14db6f22012-07-19 08:18:06 +0000211 uchar chip, uint addr, int alen)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100212{
Troy Kiskya974bcc2012-07-19 08:18:11 +0000213 unsigned int temp;
214 int ret;
215
216 /* Enable I2C controller */
Troy Kiskyfef163f2012-07-19 08:18:13 +0000217 if (!(readb(&i2c_regs->i2cr) & I2CR_IEN)) {
218 writeb(I2CR_IEN, &i2c_regs->i2cr);
219 /* Wait for controller to be stable */
220 udelay(50);
221 }
Troy Kiskye203df32012-07-19 08:18:14 +0000222 if (readb(&i2c_regs->iadr) == (chip << 1))
223 writeb((chip << 1) ^ 2, &i2c_regs->iadr);
Troy Kiskya974bcc2012-07-19 08:18:11 +0000224 writeb(0, &i2c_regs->i2sr);
Troy Kiskyfef163f2012-07-19 08:18:13 +0000225 ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE);
226 if (ret < 0)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000227 return ret;
Troy Kiskya974bcc2012-07-19 08:18:11 +0000228
229 /* Start I2C transaction */
230 temp = readb(&i2c_regs->i2cr);
231 temp |= I2CR_MSTA;
232 writeb(temp, &i2c_regs->i2cr);
233
234 ret = wait_for_sr_state(i2c_regs, ST_BUS_BUSY);
235 if (ret < 0)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000236 return ret;
Troy Kisky14db6f22012-07-19 08:18:06 +0000237
Troy Kiskya974bcc2012-07-19 08:18:11 +0000238 temp |= I2CR_MTX | I2CR_TX_NO_AK;
239 writeb(temp, &i2c_regs->i2cr);
240
Troy Kisky14db6f22012-07-19 08:18:06 +0000241 /* write slave address */
242 ret = tx_byte(i2c_regs, chip << 1);
243 if (ret < 0)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000244 return ret;
Marek Vasut94cb8422011-09-22 09:22:12 +0000245
Marek Vasut5f1291e2011-10-26 00:05:44 +0000246 while (alen--) {
Troy Kisky752ac8f2012-07-19 08:18:04 +0000247 ret = tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff);
248 if (ret < 0)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000249 return ret;
Stefano Babic848bb992011-01-20 07:51:31 +0000250 }
Troy Kisky14db6f22012-07-19 08:18:06 +0000251 return 0;
Troy Kiskyeca037a2012-07-19 08:18:16 +0000252}
253
Troy Kiskya23ab222012-07-19 08:18:19 +0000254static int i2c_idle_bus(void *base);
255
Troy Kiskyeca037a2012-07-19 08:18:16 +0000256static int i2c_init_transfer(struct mxc_i2c_regs *i2c_regs,
257 uchar chip, uint addr, int alen)
258{
259 int retry;
260 int ret;
261 for (retry = 0; retry < 3; retry++) {
262 ret = i2c_init_transfer_(i2c_regs, chip, addr, alen);
263 if (ret >= 0)
264 return 0;
Troy Kiskya2d15da2012-07-19 08:18:17 +0000265 i2c_imx_stop(i2c_regs);
Troy Kiskyeca037a2012-07-19 08:18:16 +0000266 if (ret == -ENODEV)
267 return ret;
268
269 printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
270 retry);
271 if (ret != -ERESTART)
272 writeb(0, &i2c_regs->i2cr); /* Disable controller */
273 udelay(100);
Troy Kiskya23ab222012-07-19 08:18:19 +0000274 if (i2c_idle_bus(i2c_regs) < 0)
275 break;
Troy Kiskyeca037a2012-07-19 08:18:16 +0000276 }
277 printf("%s: give up i2c_regs=%p\n", __func__, i2c_regs);
Marek Vasut94cb8422011-09-22 09:22:12 +0000278 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100279}
280
Marek Vasut94cb8422011-09-22 09:22:12 +0000281/*
Marek Vasut94cb8422011-09-22 09:22:12 +0000282 * Read data from I2C device
283 */
Troy Kiskyae447602012-07-19 08:18:18 +0000284int bus_i2c_read(void *base, uchar chip, uint addr, int alen, uchar *buf,
285 int len)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100286{
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100287 int ret;
Marek Vasut94cb8422011-09-22 09:22:12 +0000288 unsigned int temp;
289 int i;
Troy Kiskyae447602012-07-19 08:18:18 +0000290 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)base;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100291
Troy Kisky14db6f22012-07-19 08:18:06 +0000292 ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000293 if (ret < 0)
Marek Vasut94cb8422011-09-22 09:22:12 +0000294 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100295
Marek Vasut94cb8422011-09-22 09:22:12 +0000296 temp = readb(&i2c_regs->i2cr);
297 temp |= I2CR_RSTA;
298 writeb(temp, &i2c_regs->i2cr);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100299
Troy Kisky752ac8f2012-07-19 08:18:04 +0000300 ret = tx_byte(i2c_regs, (chip << 1) | 1);
Troy Kisky0ce898d2012-07-19 08:18:07 +0000301 if (ret < 0) {
Troy Kiskya2d15da2012-07-19 08:18:17 +0000302 i2c_imx_stop(i2c_regs);
Marek Vasut94cb8422011-09-22 09:22:12 +0000303 return ret;
Troy Kisky0ce898d2012-07-19 08:18:07 +0000304 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100305
Marek Vasut94cb8422011-09-22 09:22:12 +0000306 /* setup bus to read data */
307 temp = readb(&i2c_regs->i2cr);
308 temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
309 if (len == 1)
310 temp |= I2CR_TX_NO_AK;
311 writeb(temp, &i2c_regs->i2cr);
Troy Kisky30fa77c2012-07-19 08:18:05 +0000312 writeb(0, &i2c_regs->i2sr);
313 readb(&i2c_regs->i2dr); /* dummy read to clear ICF */
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100314
Marek Vasut94cb8422011-09-22 09:22:12 +0000315 /* read data */
316 for (i = 0; i < len; i++) {
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000317 ret = wait_for_sr_state(i2c_regs, ST_IIF);
318 if (ret < 0) {
Troy Kiskya2d15da2012-07-19 08:18:17 +0000319 i2c_imx_stop(i2c_regs);
Marek Vasut94cb8422011-09-22 09:22:12 +0000320 return ret;
Troy Kisky0ce898d2012-07-19 08:18:07 +0000321 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100322
Marek Vasut94cb8422011-09-22 09:22:12 +0000323 /*
324 * It must generate STOP before read I2DR to prevent
325 * controller from generating another clock cycle
326 */
327 if (i == (len - 1)) {
Troy Kiskya2d15da2012-07-19 08:18:17 +0000328 i2c_imx_stop(i2c_regs);
Marek Vasut94cb8422011-09-22 09:22:12 +0000329 } else if (i == (len - 2)) {
330 temp = readb(&i2c_regs->i2cr);
331 temp |= I2CR_TX_NO_AK;
332 writeb(temp, &i2c_regs->i2cr);
333 }
Troy Kisky30fa77c2012-07-19 08:18:05 +0000334 writeb(0, &i2c_regs->i2sr);
Marek Vasut94cb8422011-09-22 09:22:12 +0000335 buf[i] = readb(&i2c_regs->i2dr);
336 }
Troy Kiskya2d15da2012-07-19 08:18:17 +0000337 i2c_imx_stop(i2c_regs);
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000338 return 0;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100339}
340
Marek Vasut94cb8422011-09-22 09:22:12 +0000341/*
342 * Write data to I2C device
343 */
Troy Kiskyae447602012-07-19 08:18:18 +0000344int bus_i2c_write(void *base, uchar chip, uint addr, int alen,
345 const uchar *buf, int len)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100346{
Marek Vasut94cb8422011-09-22 09:22:12 +0000347 int ret;
348 int i;
Troy Kiskyae447602012-07-19 08:18:18 +0000349 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)base;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100350
Troy Kisky14db6f22012-07-19 08:18:06 +0000351 ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000352 if (ret < 0)
Marek Vasut94cb8422011-09-22 09:22:12 +0000353 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100354
Marek Vasut94cb8422011-09-22 09:22:12 +0000355 for (i = 0; i < len; i++) {
Troy Kisky752ac8f2012-07-19 08:18:04 +0000356 ret = tx_byte(i2c_regs, buf[i]);
357 if (ret < 0)
Troy Kisky0ce898d2012-07-19 08:18:07 +0000358 break;
Marek Vasut94cb8422011-09-22 09:22:12 +0000359 }
Troy Kiskya2d15da2012-07-19 08:18:17 +0000360 i2c_imx_stop(i2c_regs);
Marek Vasut94cb8422011-09-22 09:22:12 +0000361 return ret;
362}
Troy Kisky321a42b2012-07-19 08:18:08 +0000363
Troy Kiskyae447602012-07-19 08:18:18 +0000364struct i2c_parms {
365 void *base;
366 void *idle_bus_data;
367 int (*idle_bus_fn)(void *p);
368};
369
370struct sram_data {
371 unsigned curr_i2c_bus;
372 struct i2c_parms i2c_data[3];
373};
374
Troy Kisky321a42b2012-07-19 08:18:08 +0000375/*
Troy Kiskyae447602012-07-19 08:18:18 +0000376 * For SPL boot some boards need i2c before SDRAM is initialized so force
377 * variables to live in SRAM
378 */
379static struct sram_data __attribute__((section(".data"))) srdata;
380
381void *get_base(void)
382{
383#ifdef CONFIG_SYS_I2C_BASE
384#ifdef CONFIG_I2C_MULTI_BUS
385 void *ret = srdata.i2c_data[srdata.curr_i2c_bus].base;
386 if (ret)
387 return ret;
388#endif
389 return (void *)CONFIG_SYS_I2C_BASE;
390#elif defined(CONFIG_I2C_MULTI_BUS)
391 return srdata.i2c_data[srdata.curr_i2c_bus].base;
392#else
393 return srdata.i2c_data[0].base;
394#endif
395}
396
Troy Kiskya23ab222012-07-19 08:18:19 +0000397static struct i2c_parms *i2c_get_parms(void *base)
398{
399 int i = 0;
400 struct i2c_parms *p = srdata.i2c_data;
401 while (i < ARRAY_SIZE(srdata.i2c_data)) {
402 if (p->base == base)
403 return p;
404 p++;
405 i++;
406 }
407 printf("Invalid I2C base: %p\n", base);
408 return NULL;
409}
410
411static int i2c_idle_bus(void *base)
412{
413 struct i2c_parms *p = i2c_get_parms(base);
414 if (p && p->idle_bus_fn)
415 return p->idle_bus_fn(p->idle_bus_data);
416 return 0;
417}
418
Troy Kiskyb6f98262012-07-19 08:18:20 +0000419#ifdef CONFIG_I2C_MULTI_BUS
420unsigned int i2c_get_bus_num(void)
421{
422 return srdata.curr_i2c_bus;
423}
424
425int i2c_set_bus_num(unsigned bus_idx)
426{
427 if (bus_idx >= ARRAY_SIZE(srdata.i2c_data))
428 return -1;
429 if (!srdata.i2c_data[bus_idx].base)
430 return -1;
431 srdata.curr_i2c_bus = bus_idx;
432 return 0;
433}
434#endif
435
Troy Kiskyae447602012-07-19 08:18:18 +0000436int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
437{
438 return bus_i2c_read(get_base(), chip, addr, alen, buf, len);
439}
440
441int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
442{
443 return bus_i2c_write(get_base(), chip, addr, alen, buf, len);
444}
445
446/*
Troy Kisky321a42b2012-07-19 08:18:08 +0000447 * Test if a chip at a given address responds (probe the chip)
448 */
449int i2c_probe(uchar chip)
450{
Troy Kiskyae447602012-07-19 08:18:18 +0000451 return bus_i2c_write(get_base(), chip, 0, 0, NULL, 0);
452}
453
454void bus_i2c_init(void *base, int speed, int unused,
455 int (*idle_bus_fn)(void *p), void *idle_bus_data)
456{
457 int i = 0;
458 struct i2c_parms *p = srdata.i2c_data;
459 if (!base)
460 return;
461 for (;;) {
462 if (!p->base || (p->base == base)) {
463 p->base = base;
464 if (idle_bus_fn) {
465 p->idle_bus_fn = idle_bus_fn;
466 p->idle_bus_data = idle_bus_data;
467 }
468 break;
469 }
470 p++;
471 i++;
472 if (i >= ARRAY_SIZE(srdata.i2c_data))
473 return;
474 }
475 bus_i2c_set_bus_speed(base, speed);
476}
477
478/*
479 * Init I2C Bus
480 */
481void i2c_init(int speed, int unused)
482{
483 bus_i2c_init(get_base(), speed, unused, NULL, NULL);
484}
485
486/*
487 * Set I2C Speed
488 */
489int i2c_set_bus_speed(unsigned int speed)
490{
491 return bus_i2c_set_bus_speed(get_base(), speed);
492}
493
494/*
495 * Get I2C Speed
496 */
497unsigned int i2c_get_bus_speed(void)
498{
499 return bus_i2c_get_bus_speed(get_base());
Troy Kisky321a42b2012-07-19 08:18:08 +0000500}