2 * i2c driver for Freescale mx31
4 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6 * (C) Copyright 2008-2010 Freescale Semiconductor, Inc.
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #if defined(CONFIG_HARD_I2C)
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)
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)
49 #ifdef CONFIG_SYS_I2C_PORT
50 # define I2C_BASE CONFIG_SYS_I2C_PORT
52 # error "define CONFIG_SYS_I2C_PORT(I2C base address) to use the I2C driver"
55 #define I2C_MAX_TIMEOUT 100000
56 #define I2C_TIMEOUT_TICKET 1
61 #define DPRINTF(args...) printf(args)
63 #define DPRINTF(args...)
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
71 static inline void i2c_reset(void)
73 __REG16(I2C_BASE + I2CR) = 0; /* Reset module */
74 __REG16(I2C_BASE + I2SR) = 0;
75 __REG16(I2C_BASE + I2CR) = I2CR_IEN;
78 void i2c_init(int speed, int unused)
84 freq = mx31_get_ipg_clk();
86 freq = mxc_get_clock(MXC_IPG_PERCLK);
88 for (i = 0; i < 0x1f; i++)
89 if (freq / div[i] <= speed)
92 DPRINTF("%s: root clock: %d, speed: %d div: %x\n",
93 __func__, freq, speed, i);
95 __REG16(I2C_BASE + IFDR) = i;
99 static int wait_idle(void)
101 int timeout = I2C_MAX_TIMEOUT;
103 while ((__REG16(I2C_BASE + I2SR) & I2SR_IBB) && --timeout) {
104 __REG16(I2C_BASE + I2SR) = 0;
105 udelay(I2C_TIMEOUT_TICKET);
107 DPRINTF("%s:%x\n", __func__, __REG16(I2C_BASE + I2SR));
108 return timeout ? timeout : (!(__REG16(I2C_BASE + I2SR) & I2SR_IBB));
111 static int wait_busy(void)
113 int timeout = I2C_MAX_TIMEOUT;
115 while ((!(__REG16(I2C_BASE + I2SR) & I2SR_IBB) && (--timeout))) {
116 __REG16(I2C_BASE + I2SR) = 0;
117 udelay(I2C_TIMEOUT_TICKET);
119 return timeout ? timeout : (__REG16(I2C_BASE + I2SR) & I2SR_IBB);
122 static int wait_complete(void)
124 int timeout = I2C_MAX_TIMEOUT;
126 while ((!(__REG16(I2C_BASE + I2SR) & I2SR_ICF)) && (--timeout)) {
127 __REG16(I2C_BASE + I2SR) = 0;
128 udelay(I2C_TIMEOUT_TICKET);
130 DPRINTF("%s:%x\n", __func__, __REG16(I2C_BASE + I2SR));
133 for (i = 0; i < 200; i++)
137 __REG16(I2C_BASE + I2SR) = 0; /* clear interrupt */
142 static int tx_byte(u8 byte)
144 __REG16(I2C_BASE + I2DR) = byte;
146 if (!wait_complete() || __REG16(I2C_BASE + I2SR) & I2SR_RX_NO_AK) {
147 DPRINTF("%s:%x <= %x\n", __func__, __REG16(I2C_BASE + I2SR),
151 DPRINTF("%s:%x\n", __func__, byte);
155 static int rx_byte(u32 *pdata, int last)
157 if (!wait_complete())
161 __REG16(I2C_BASE + I2CR) = I2CR_IEN;
163 *pdata = __REG16(I2C_BASE + I2DR);
164 DPRINTF("%s:%x\n", __func__, *pdata);
168 int i2c_probe(uchar chip)
172 __REG16(I2C_BASE + I2CR) = 0; /* Reset module */
173 __REG16(I2C_BASE + I2CR) = I2CR_IEN;
174 for (ret = 0; ret < 1000; ret++)
176 __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA | I2CR_MTX;
177 ret = tx_byte(chip << 1);
178 __REG16(I2C_BASE + I2CR) = I2CR_IEN;
183 static int i2c_addr(uchar chip, uint addr, int alen)
186 for (retry = 0; retry < 3; retry++) {
190 for (i = 0; i < I2C_MAX_TIMEOUT; i++)
191 udelay(I2C_TIMEOUT_TICKET);
194 printf("%s:bus is busy(%x)\n",
195 __func__, __REG16(I2C_BASE + I2SR));
198 __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA | I2CR_MTX;
200 printf("%s:trigger start fail(%x)\n",
201 __func__, __REG16(I2C_BASE + I2SR));
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));
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));
219 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
221 int timeout = I2C_MAX_TIMEOUT;
224 DPRINTF("%s chip: 0x%02x addr: 0x%04x alen: %d len: %d\n",
225 __func__, chip, addr, alen, len);
227 if (i2c_addr(chip, addr, alen)) {
228 printf("i2c_addr failed\n");
232 __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA | I2CR_MTX | I2CR_RSTA;
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));
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);
247 __REG16(I2C_BASE + I2CR) = I2CR_IEN | I2CR_MSTA |
250 if (rx_byte(&ret, len == 0) < 0) {
251 printf("Read: rx_byte fail\n");
257 while (__REG16(I2C_BASE + I2SR) & I2SR_IBB && --timeout) {
258 __REG16(I2C_BASE + I2SR) = 0;
259 udelay(I2C_TIMEOUT_TICKET);
262 printf("%s:trigger stop fail(%x)\n",
263 __func__, __REG16(I2C_BASE + I2SR));
268 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
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);
274 if (i2c_addr(chip, addr, alen))
281 __REG16(I2C_BASE + I2CR) = I2CR_IEN;
283 while (__REG16(I2C_BASE + I2SR) & I2SR_IBB && --timeout)
284 udelay(I2C_TIMEOUT_TICKET);
289 #endif /* CONFIG_HARD_I2C */