1 /* 2 * i2c driver for Freescale i.MX series 3 * 4 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> 5 * (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 * 13 * 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> 34 #include <asm/arch/clock.h> 35 #include <asm/arch/imx-regs.h> 36 #include <asm/errno.h> 37 #include <asm/io.h> 38 #include <i2c.h> 39 40 struct mxc_i2c_regs { 41 uint32_t iadr; 42 uint32_t ifdr; 43 uint32_t i2cr; 44 uint32_t i2sr; 45 uint32_t i2dr; 46 }; 47 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 60 #ifdef CONFIG_SYS_I2C_BASE 61 #define I2C_BASE CONFIG_SYS_I2C_BASE 62 #else 63 #error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver" 64 #endif 65 66 #define I2C_MAX_TIMEOUT 10000 67 68 static 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 }; 83 84 /* 85 * Calculate and set proper clock divider 86 */ 87 static uint8_t i2c_imx_get_clk(unsigned int rate) 88 { 89 unsigned int i2c_clk_rate; 90 unsigned int div; 91 u8 clk_div; 92 93 #if defined(CONFIG_MX31) 94 struct clock_control_regs *sc_regs = 95 (struct clock_control_regs *)CCM_BASE; 96 97 /* start the required I2C clock */ 98 writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET), 99 &sc_regs->cgr0); 100 #endif 101 102 /* 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]) 106 clk_div = 0; 107 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0]) 108 clk_div = ARRAY_SIZE(i2c_clk_div) - 1; 109 else 110 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++) 111 ; 112 113 /* Store divider value */ 114 return clk_div; 115 } 116 117 /* 118 * Reset I2C Controller 119 */ 120 void 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 */ 131 void i2c_init(int speed, int unused) 132 { 133 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 140 i2c_reset(); 141 } 142 143 /* 144 * Set I2C Speed 145 */ 146 int i2c_set_bus_speed(unsigned int speed) 147 { 148 i2c_init(speed, 0); 149 return 0; 150 } 151 152 /* 153 * Get I2C Speed 154 */ 155 unsigned int i2c_get_bus_speed(void) 156 { 157 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 164 return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0]; 165 } 166 167 /* 168 * 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 */ 173 int i2c_imx_bus_busy(int for_busy) 174 { 175 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 176 unsigned int temp; 177 178 int timeout = I2C_MAX_TIMEOUT; 179 180 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 188 udelay(1); 189 } 190 191 return 1; 192 } 193 194 /* 195 * Wait for transaction to complete 196 */ 197 int i2c_imx_trx_complete(void) 198 { 199 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 200 int timeout = I2C_MAX_TIMEOUT; 201 202 while (timeout--) { 203 if (readb(&i2c_regs->i2sr) & I2SR_IIF) { 204 writeb(0, &i2c_regs->i2sr); 205 return 0; 206 } 207 208 udelay(1); 209 } 210 211 return -ETIMEDOUT; 212 } 213 214 static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte) 215 { 216 int ret; 217 218 writeb(byte, &i2c_regs->i2dr); 219 ret = i2c_imx_trx_complete(); 220 if (ret < 0) 221 return ret; 222 ret = readb(&i2c_regs->i2sr); 223 if (ret & I2SR_RX_NO_AK) 224 return -ENODEV; 225 return 0; 226 } 227 228 /* 229 * Start the controller 230 */ 231 int i2c_imx_start(void) 232 { 233 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 234 unsigned int temp = 0; 235 int result; 236 237 /* Enable I2C controller */ 238 writeb(0, &i2c_regs->i2sr); 239 writeb(I2CR_IEN, &i2c_regs->i2cr); 240 241 /* Wait controller to be stable */ 242 udelay(50); 243 244 /* Start I2C transaction */ 245 temp = readb(&i2c_regs->i2cr); 246 temp |= I2CR_MSTA; 247 writeb(temp, &i2c_regs->i2cr); 248 249 result = i2c_imx_bus_busy(1); 250 if (result) 251 return result; 252 253 temp |= I2CR_MTX | I2CR_TX_NO_AK; 254 writeb(temp, &i2c_regs->i2cr); 255 256 return 0; 257 } 258 259 /* 260 * Stop the controller 261 */ 262 void i2c_imx_stop(void) 263 { 264 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 265 unsigned int temp = 0; 266 267 /* Stop I2C transaction */ 268 temp = readb(&i2c_regs->i2cr); 269 temp &= ~(I2CR_MSTA | I2CR_MTX); 270 writeb(temp, &i2c_regs->i2cr); 271 272 i2c_imx_bus_busy(0); 273 274 /* Disable I2C controller */ 275 writeb(0, &i2c_regs->i2cr); 276 } 277 278 /* 279 * Write register address 280 */ 281 int i2c_imx_set_reg_addr(uint addr, int alen) 282 { 283 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 284 int ret = 0; 285 286 while (alen--) { 287 ret = tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff); 288 if (ret < 0) 289 break; 290 } 291 292 return ret; 293 } 294 295 /* 296 * Try if a chip add given address responds (probe the chip) 297 */ 298 int i2c_probe(uchar chip) 299 { 300 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 301 int ret; 302 303 ret = i2c_imx_start(); 304 if (ret) 305 return ret; 306 307 ret = tx_byte(i2c_regs, chip << 1); 308 i2c_imx_stop(); 309 return ret; 310 } 311 312 /* 313 * Read data from I2C device 314 */ 315 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len) 316 { 317 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 318 int ret; 319 unsigned int temp; 320 int i; 321 322 ret = i2c_imx_start(); 323 if (ret) 324 return ret; 325 326 /* write slave address */ 327 ret = tx_byte(i2c_regs, chip << 1); 328 if (ret < 0) 329 return ret; 330 331 ret = i2c_imx_set_reg_addr(addr, alen); 332 if (ret) 333 return ret; 334 335 temp = readb(&i2c_regs->i2cr); 336 temp |= I2CR_RSTA; 337 writeb(temp, &i2c_regs->i2cr); 338 339 ret = tx_byte(i2c_regs, (chip << 1) | 1); 340 if (ret < 0) 341 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); 349 readb(&i2c_regs->i2dr); 350 351 /* read data */ 352 for (i = 0; i < len; i++) { 353 ret = i2c_imx_trx_complete(); 354 if (ret) 355 return ret; 356 357 /* 358 * It must generate STOP before read I2DR to prevent 359 * controller from generating another clock cycle 360 */ 361 if (i == (len - 1)) { 362 temp = readb(&i2c_regs->i2cr); 363 temp &= ~(I2CR_MSTA | I2CR_MTX); 364 writeb(temp, &i2c_regs->i2cr); 365 i2c_imx_bus_busy(0); 366 } else if (i == (len - 2)) { 367 temp = readb(&i2c_regs->i2cr); 368 temp |= I2CR_TX_NO_AK; 369 writeb(temp, &i2c_regs->i2cr); 370 } 371 372 buf[i] = readb(&i2c_regs->i2dr); 373 } 374 375 i2c_imx_stop(); 376 377 return ret; 378 } 379 380 /* 381 * Write data to I2C device 382 */ 383 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len) 384 { 385 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 386 int ret; 387 int i; 388 389 ret = i2c_imx_start(); 390 if (ret) 391 return ret; 392 393 /* write slave address */ 394 ret = tx_byte(i2c_regs, chip << 1); 395 if (ret < 0) 396 return ret; 397 398 ret = i2c_imx_set_reg_addr(addr, alen); 399 if (ret) 400 return ret; 401 402 for (i = 0; i < len; i++) { 403 ret = tx_byte(i2c_regs, buf[i]); 404 if (ret < 0) 405 return ret; 406 } 407 408 i2c_imx_stop(); 409 410 return ret; 411 } 412